当前位置: 首页>>代码示例>>Java>>正文


Java KeyGenerator.init方法代码示例

本文整理汇总了Java中javax.crypto.KeyGenerator.init方法的典型用法代码示例。如果您正苦于以下问题:Java KeyGenerator.init方法的具体用法?Java KeyGenerator.init怎么用?Java KeyGenerator.init使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.crypto.KeyGenerator的用法示例。


在下文中一共展示了KeyGenerator.init方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: initKey

import javax.crypto.KeyGenerator; //导入方法依赖的package包/类
/**
 * 生成密钥 <br>
 * Java 6 只支持56bit密钥 <br>
 * Bouncy Castle 支持64bit密钥 <br>
 * 
 * @return byte[] 二进制密钥
 * @throws NoSuchAlgorithmException
 * @throws Exception
 */
public static byte[] initKey() throws NoSuchAlgorithmException {
	/*
	 * 实例化密钥生成器
	 * 
	 * 若要使用64bit密钥注意替换 将下述代码中的KeyGenerator.getInstance(CIPHER_ALGORITHM);
	 * 替换为KeyGenerator.getInstance(CIPHER_ALGORITHM, "BC");
	 */
	KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
	/*
	 * 初始化密钥生成器 若要使用64bit密钥注意替换 将下述代码kg.init(56); 替换为kg.init(64);
	 */
	kg.init(56, new SecureRandom());
	// 生成秘密密钥
	SecretKey secretKey = kg.generateKey();
	// 获得密钥的二进制编码形式
	return secretKey.getEncoded();
}
 
开发者ID:tb544731152,项目名称:iBase4J,代码行数:27,代码来源:DESCoder.java

示例2: getCipher

import javax.crypto.KeyGenerator; //导入方法依赖的package包/类
public static Cipher getCipher(int mode, byte[] secret) throws NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException {
    // generate an encryption/decryption key from random data seeded with
    // our secret (i.e. password)
    SecureRandom secureRandom = SecureRandom.getInstance(RANDOM_ALGORITHM);
    secureRandom.setSeed(secret);
    KeyGenerator keyGenerator = KeyGenerator.getInstance(ENCRYPTION_ALGORITHM);
    keyGenerator.init(KEY_SIZE, secureRandom);
    Key key = new SecretKeySpec(keyGenerator.generateKey().getEncoded(), ENCRYPTION_ALGORITHM);
    // get a cipher based on the specified encryption algorithm
    Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
    // tell the cipher if it will be used for encryption or decryption
    // (i.e. cipher mode) and give it our key
    cipher.init(mode, key);
    return cipher;
}
 
开发者ID:sdrausty,项目名称:buildAPKsSamples,代码行数:17,代码来源:CryptUtil.java

示例3: aesEncryptToBytes

import javax.crypto.KeyGenerator; //导入方法依赖的package包/类
private static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {  
    KeyGenerator kgen = KeyGenerator.getInstance("AES");  
    kgen.init(128, new SecureRandom(encryptKey.getBytes()));  
  
    Cipher cipher = Cipher.getInstance("AES");  
    cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES"));  
      
    return cipher.doFinal(content.getBytes("utf-8"));  
}
 
开发者ID:onsoul,项目名称:os,代码行数:10,代码来源:AESUtils.java

示例4: generateValues

import javax.crypto.KeyGenerator; //导入方法依赖的package包/类
private void generateValues() throws Exception {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    keyGenerator.init(RandomValuesProvider.getRandomKeyLength());
    key = keyGenerator.generateKey().getEncoded();
    radix = RandomValuesProvider.getRandomRadix();
    input = RandomValuesProvider.randomPlainText(radix);
    values = RandomValuesProvider.valuesAsString(input, key, radix);
}
 
开发者ID:idealista,项目名称:format-preserving-encryption-java,代码行数:9,代码来源:FF1AlgorithmRandomPlainTextAndKeysShould.java

示例5: encrypt

import javax.crypto.KeyGenerator; //导入方法依赖的package包/类
/**
 * 加密
 *
 * @param content �?要加密的内容
 * @return
 */
public static String encrypt(String content) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance(AES);
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(KEY.getBytes());
        kgen.init(AES_LENGTH, random);
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES);
        Cipher cipher = Cipher.getInstance(AES);// 创建密码�?
        byte[] byteContent = content.getBytes(ENCODING_UTF8);
        cipher.init(Cipher.ENCRYPT_MODE, key);// 初始�?
        byte[] result = cipher.doFinal(byteContent);
        return new BASE64Encoder().encode(result); // 加密
}
 
开发者ID:Fetax,项目名称:Fetax-AI,代码行数:21,代码来源:AESUtil.java

示例6: initKey56

import javax.crypto.KeyGenerator; //导入方法依赖的package包/类
/**
 * 生成密钥
 *
 * @return
 */
public byte[] initKey56() {
    KeyGenerator keyGen = null;
    try {
        keyGen = KeyGenerator.getInstance("DES");
        keyGen.init(56);
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    SecretKey secretKey = keyGen.generateKey();
    return secretKey.getEncoded();
}
 
开发者ID:jopenbox,项目名称:android-lite-utils,代码行数:17,代码来源:DESUtils.java

示例7: initKey

import javax.crypto.KeyGenerator; //导入方法依赖的package包/类
/**
 * 生成密钥
 *
 * @param keysize
 * @return
 */
public byte[] initKey(int keysize) {
    KeyGenerator keyGen = null;
    try {
        keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(keysize);  //192 256
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    SecretKey secretKey = keyGen.generateKey();
    return secretKey.getEncoded();
}
 
开发者ID:jopenbox,项目名称:android-lite-utils,代码行数:18,代码来源:AESUtils.java

示例8: decrypt

import javax.crypto.KeyGenerator; //导入方法依赖的package包/类
/**解密
 * @param content  待解密内�?
 * @return
 */
public static String decrypt(String content) throws Exception  {
    KeyGenerator kgen = KeyGenerator.getInstance(AES);
    SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
    random.setSeed(KEY.getBytes());
    kgen.init(AES_LENGTH, random);
    SecretKey secretKey = kgen.generateKey();
    byte[] enCodeFormat = secretKey.getEncoded();
    SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES);
    Cipher cipher = Cipher.getInstance(AES);// 创建密码�?
    cipher.init(Cipher.DECRYPT_MODE, key);// 初始�?
    byte[] encrypted1 = new BASE64Decoder().decodeBuffer(content);
    byte[] result = cipher.doFinal(encrypted1);
    return new String(result,ENCODING_UTF8); // 加密
}
 
开发者ID:Fetax,项目名称:Fetax-AI,代码行数:19,代码来源:AESUtil.java

示例9: test

import javax.crypto.KeyGenerator; //导入方法依赖的package包/类
private static void test(KeyGenerator kg,
        int clientVersion, int serverVersion) throws Exception {

    System.out.printf(
            "Testing RSA pre-master secret key generation between " +
            "client (0x%04X) and server(0x%04X)%n",
            clientVersion, serverVersion);
    kg.init(new TlsRsaPremasterSecretParameterSpec(
                                clientVersion, serverVersion));

    SecretKey key = kg.generateKey();
    byte[] encoded = key.getEncoded();
    if (encoded != null) {  // raw key material may be not extractable
        if (encoded.length != 48) {
            throw new Exception("length: " + encoded.length);
        }
        int v = versionOf(encoded[0], encoded[1]);
        if (clientVersion != v) {
            if (serverVersion != v || clientVersion >= 0x0302) {
                throw new Exception(String.format(
                    "version mismatch: (0x%04X) rather than (0x%04X) " +
                    "is used in pre-master secret", v, clientVersion));
            }
            System.out.printf("Use compatible version (0x%04X)%n", v);
        }
        System.out.println("Passed, version matches!");
   } else {
        System.out.println("Raw key material is not extractable");
   }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:31,代码来源:TestPremaster.java

示例10: generate

import javax.crypto.KeyGenerator; //导入方法依赖的package包/类
public static String generate(){
    KeyGenerator generator = null;
    try {
        generator = KeyGenerator.getInstance(HMAC_SHA_256);
        generator.init(KEY_LENGTH);

        return new String(generator.generateKey().getEncoded());
    } catch (NoSuchAlgorithmException e) {
        LOGGER.error("error while generating secret key, Random secret will be generated", e);
        return UUID.randomUUID().toString();
    }
}
 
开发者ID:GwtDomino,项目名称:domino,代码行数:13,代码来源:SecretKey.java

示例11: generateEncryptionSecret

import javax.crypto.KeyGenerator; //导入方法依赖的package包/类
private static byte[] generateEncryptionSecret() {
  try {
    KeyGenerator generator = KeyGenerator.getInstance("AES");
    generator.init(128);

    SecretKey key = generator.generateKey();
    return key.getEncoded();
  } catch (NoSuchAlgorithmException ex) {
    Log.w("keyutil", ex);
    return null;
  }
}
 
开发者ID:CableIM,项目名称:Cable-Android,代码行数:13,代码来源:MasterSecretUtil.java

示例12: run

import javax.crypto.KeyGenerator; //导入方法依赖的package包/类
private void run(String keystoreType) throws Exception {
    char[] pw = "password".toCharArray();
    KeyStore ks = KeyStore.getInstance(keystoreType);
    ks.load(null, pw);

    KeyGenerator kg = KeyGenerator.getInstance("AES");
    kg.init(128);
    SecretKey key = kg.generateKey();

    KeyStore.SecretKeyEntry ske = new KeyStore.SecretKeyEntry(key);
    KeyStore.ProtectionParameter kspp = new KeyStore.PasswordProtection(pw);
    ks.setEntry(ALIAS, ske, kspp);

    File ksFile = File.createTempFile("test", ".test");
    try (FileOutputStream fos = new FileOutputStream(ksFile)) {
        ks.store(fos, pw);
        fos.flush();
    }

    // now see if we can get it back
    try (FileInputStream fis = new FileInputStream(ksFile)) {
        KeyStore ks2 = KeyStore.getInstance(keystoreType);
        ks2.load(fis, pw);
        KeyStore.Entry entry = ks2.getEntry(ALIAS, kspp);
        SecretKey keyIn = ((KeyStore.SecretKeyEntry)entry).getSecretKey();
        if (Arrays.equals(key.getEncoded(), keyIn.getEncoded())) {
            System.err.println("OK: worked just fine with " + keystoreType +
                               " keystore");
        } else {
            System.err.println("ERROR: keys are NOT equal after storing in "
                               + keystoreType + " keystore");
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:35,代码来源:P12SecretKey.java

示例13: getAESKey

import javax.crypto.KeyGenerator; //导入方法依赖的package包/类
private static SecretKey getAESKey(int bits) {
	try {
		KeyGenerator keyGen = KeyGenerator.getInstance("AES");
		keyGen.init(bits);
		return keyGen.generateKey();
	} catch (Exception e) {
		return null;
	}
}
 
开发者ID:gahana,项目名称:edge-jwt-sample,代码行数:10,代码来源:JWTUtil.java

示例14: generateKey

import javax.crypto.KeyGenerator; //导入方法依赖的package包/类
/**
 * Generates a key material.
 *
 * @param size length of the key.
 * @param algorithm algorithm to use for generating the key.
 * @return the generated key.
 * @throws NoSuchAlgorithmException
 */
protected byte[] generateKey(int size, String algorithm)
    throws NoSuchAlgorithmException {
  algorithm = getAlgorithm(algorithm);
  KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
  keyGenerator.init(size);
  byte[] key = keyGenerator.generateKey().getEncoded();
  return key;
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:17,代码来源:KeyProvider.java

示例15: AESCypher

import javax.crypto.KeyGenerator; //导入方法依赖的package包/类
public AESCypher()  throws Exception{

KeyGenerator keyGen = KeyGenerator.getInstance("AES"); 
keyGen.init(128);
key = keyGen.generateKey();
super.setCypher( Cipher.getInstance("AES/ECB/PKCS5Padding"));
}
 
开发者ID:Ryan-Gordon,项目名称:Advanced-Object-Orientated-Software-Dev,代码行数:8,代码来源:AESCypher.java


注:本文中的javax.crypto.KeyGenerator.init方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。