當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。