當前位置: 首頁>>代碼示例>>Java>>正文


Java SecretKey.getEncoded方法代碼示例

本文整理匯總了Java中javax.crypto.SecretKey.getEncoded方法的典型用法代碼示例。如果您正苦於以下問題:Java SecretKey.getEncoded方法的具體用法?Java SecretKey.getEncoded怎麽用?Java SecretKey.getEncoded使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.crypto.SecretKey的用法示例。


在下文中一共展示了SecretKey.getEncoded方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: initKey

import javax.crypto.SecretKey; //導入方法依賴的package包/類
/**
 * 生成密鑰 <br>
 * Java 6 隻支持56bit密鑰 <br>
 * Bouncy Castle 支持64bit密鑰 <br>
 * 
 * @return byte[] 二進製密鑰
 * @throws NoSuchAlgorithmException
 */
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:iBase4J,項目名稱:iBase4J-Common,代碼行數:26,代碼來源:DESCoder.java

示例2: hash

import javax.crypto.SecretKey; //導入方法依賴的package包/類
/**
 * This method will hash the password and salt combination. Be careful, this method will erase the password after hashed it.
 * You must be sure that you do not need it after using this method.
 *
 * @param password the password who needs to be hashed
 * @param salt     some list of <code>byte</code> which will be included in the password
 * @return a hash of the password and salting combination.
 */
public static byte[] hash(char[] password, byte[] salt) {
    final PBEKeySpec spec = new PBEKeySpec(password, salt, SecurityUtils.ITERATIONS, SecurityUtils.KEY_LENGTH);

    try {
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(SecurityUtils.ALGORITHM);
        SecretKey key = secretKeyFactory.generateSecret(spec);
        return key.getEncoded();
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        LOGGER.debug(e.getMessage(), e);
        throw new SecurityException(e);
    } finally {
        // erase the password in the char array in order to not retrieve it in the java memory
        spec.clearPassword();
    }
}
 
開發者ID:Crunchy-Torch,項目名稱:coddy,代碼行數:24,代碼來源:SecurityUtils.java

示例3: testAlgorithm

import javax.crypto.SecretKey; //導入方法依賴的package包/類
private static void testAlgorithm(KeyAgreement ka1, KeyPair kp1,
        KeyAgreement ka2, KeyPair kp2, String algorithm) throws Exception {
    SecretKey key1;

    ka1.init(kp1.getPrivate());
    ka1.doPhase(kp2.getPublic(), true);
    System.out.println("Derive " + algorithm + " using SunJCE...");
    key1 = ka1.generateSecret(algorithm);

    ka2.init(kp1.getPrivate());
    ka2.doPhase(kp2.getPublic(), true);
    System.out.println("Derive " + algorithm + " using PKCS#11...");
    SecretKey key2 = ka2.generateSecret(algorithm);

    byte[] b1 = key1.getEncoded();
    byte[] b2 = key2.getEncoded();

    if (Arrays.equals(b1, b2) == false) {
        System.out.println(b1.length + " bytes: " + toString(b1));
        System.out.println(b2.length + " bytes: " + toString(b2));
        throw new Exception(algorithm + " secret mismatch");
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:24,代碼來源:TestDH.java

示例4: makeKey

import javax.crypto.SecretKey; //導入方法依賴的package包/類
private static String makeKey() throws Exception {
    KeyGenerator keyGen = KeyGenerator.getInstance("AES");
    keyGen.init(256);
    SecretKey secretKey = keyGen.generateKey();
    byte[] secretKeyEncoded = secretKey.getEncoded();
    return Base64.getEncoder().encodeToString(secretKeyEncoded);
}
 
開發者ID:NordeaOSS,項目名稱:authenticated-encryption,代碼行數:8,代碼來源:AuthenticatedEncryptionTest.java

示例5: initHmacMD2Key

import javax.crypto.SecretKey; //導入方法依賴的package包/類
/**
 * 初始化HmacMD2密鑰
 * 
 * @return byte[] 密鑰
 * @throws Exception
 */
public static byte[] initHmacMD2Key() throws Exception {
	// 初始化KeyGenerator
	KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD2");
	// 產生秘密密鑰
	SecretKey secretKey = keyGenerator.generateKey();
	// 獲得密鑰
	return secretKey.getEncoded();
}
 
開發者ID:youngMen1,項目名稱:JAVA-,代碼行數:15,代碼來源:HmacCoder.java

示例6: initHmacSHA384Key

import javax.crypto.SecretKey; //導入方法依賴的package包/類
/**
 * 初始化HmacSHA384密鑰
 * 
 * @return
 * @throws Exception
 */
public static byte[] initHmacSHA384Key() throws Exception {
	// 初始化KeyGenerator
	KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA384");
	// 產生秘密密鑰
	SecretKey secretKey = keyGenerator.generateKey();
	// 獲得密鑰
	return secretKey.getEncoded();
}
 
開發者ID:tb544731152,項目名稱:iBase4J,代碼行數:15,代碼來源:HmacCoder.java

示例7: getBytes

import javax.crypto.SecretKey; //導入方法依賴的package包/類
private static byte[] getBytes(String hmacsha1, int defaultHmacsha1Keysize) {
	try {
		KeyGenerator keyGenerator = KeyGenerator.getInstance(hmacsha1);
		keyGenerator.init(defaultHmacsha1Keysize);
		SecretKey secretKey = keyGenerator.generateKey();
		return secretKey.getEncoded();
	} catch (GeneralSecurityException e) {
		throw Exceptions.unchecked(e);
	}
}
 
開發者ID:funtl,項目名稱:framework,代碼行數:11,代碼來源:Cryptos.java

示例8: initHmacSHA512Key

import javax.crypto.SecretKey; //導入方法依賴的package包/類
/**
 * 初始化HmacSHA512密鑰
 * 
 * @return
 * @throws Exception
 */
public static byte[] initHmacSHA512Key() throws Exception {
	// 初始化KeyGenerator
	KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA512");
	// 產生秘密密鑰
	SecretKey secretKey = keyGenerator.generateKey();
	// 獲得密鑰
	return secretKey.getEncoded();
}
 
開發者ID:mumucommon,項目名稱:mumu-core,代碼行數:15,代碼來源:HmacCoder.java

示例9: getKey

import javax.crypto.SecretKey; //導入方法依賴的package包/類
/**
 * @Title: getKey
 * @Description: 自動生成AES128位密鑰:AES 加密密鑰長度可以是 128、192、256
 * 				<p>生成的密鑰為byte[]數組,轉化為16進製的字符串進行使用</p>
 * @return String    返回類型
 */
public static String getKey() {
    try {
        KeyGenerator kg = KeyGenerator.getInstance(AESTYPE);
        kg.init(KEY_LENGTH);
        SecretKey sk = kg.generateKey();
        byte[] b = sk.getEncoded();
        String s = parseByte2HexStr(b);
        return s;
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return null;
    }
}
 
開發者ID:gy-games,項目名稱:bumblebee,代碼行數:20,代碼來源:AesUtil.java

示例10: initHmacMD4Key

import javax.crypto.SecretKey; //導入方法依賴的package包/類
/**
 * 初始化HmacMD4密鑰
 * 
 * @return byte[] 密鑰
 * @throws Exception
 */
public static byte[] initHmacMD4Key() throws Exception {
	// 初始化KeyGenerator
	KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD4");
	// 產生秘密密鑰
	SecretKey secretKey = keyGenerator.generateKey();
	// 獲得密鑰
	return secretKey.getEncoded();
}
 
開發者ID:babymm,項目名稱:mumu,代碼行數:15,代碼來源:HmacCoder.java

示例11: test

import javax.crypto.SecretKey; //導入方法依賴的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);
    try {
        kg.init(new TlsRsaPremasterSecretParameterSpec(
                                clientVersion, serverVersion));
    } catch (InvalidAlgorithmParameterException iape) {
        // S12 removed support for SSL v3.0
        if (clientVersion == 0x300 || serverVersion == 0x300) {
            System.out.println("Skip testing SSLv3 due to no support");
            return;
        }
        // unexpected, pass it up
        throw iape;
    }
    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:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:40,代碼來源:TestPremaster.java

示例12: getKey

import javax.crypto.SecretKey; //導入方法依賴的package包/類
public SecretKey getKey(char[] password, byte[] salt) throws GeneralSecurityException {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
    KeySpec spec = new PBEKeySpec(password, salt, 16384, 256);
    SecretKey tmp = factory.generateSecret(spec);
    SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

    return secret;
}
 
開發者ID:DesktopRemoteManagement,項目名稱:DRM-Desktop,代碼行數:9,代碼來源:EncryptionTest.java

示例13: createKey

import javax.crypto.SecretKey; //導入方法依賴的package包/類
private byte[] createKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
	byte[] salt = new byte[1014];
	SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
	random.nextBytes(salt);
	
	SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
	KeySpec spec = new PBEKeySpec("[email protected]".toCharArray(), salt, 65536, 256);
	SecretKey tmp = factory.generateSecret(spec);
	SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
	
	return secret.getEncoded();
}
 
開發者ID:daflockinger,項目名稱:poppynotes,代碼行數:13,代碼來源:NoteEncryptionServiceTest.java

示例14: initHmacMD5Key

import javax.crypto.SecretKey; //導入方法依賴的package包/類
/**
 * 初始化HmacMD5密鑰
 * 
 * @return
 * @throws Exception
 */
public static byte[] initHmacMD5Key() throws Exception {
	// 初始化KeyGenerator
	KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");
	// 產生秘密密鑰
	SecretKey secretKey = keyGenerator.generateKey();
	// 獲得密鑰
	return secretKey.getEncoded();
}
 
開發者ID:iBase4J,項目名稱:iBase4J-Common,代碼行數:15,代碼來源:HmacCoder.java

示例15: generateSecretKey

import javax.crypto.SecretKey; //導入方法依賴的package包/類
/**
 * 生成AES密鑰
 * 
 * @return Secret key
 * @throws AESException AES exception
 */
public byte[] generateSecretKey() throws AESException {
    try {
        // Get the KeyGenerator
        KeyGenerator kgen = KeyGenerator.getInstance(ENCRYPTION_ALGORITHM);
        kgen.init(KEY_SIZE); // 192 and 256 bits may not be available
        // Generate the secret key specs.
        SecretKey skey = kgen.generateKey();
        secretKey = skey.getEncoded();
        return secretKey;
    } catch (NoSuchAlgorithmException e) {
        throw new AESException(e);
    }
}
 
開發者ID:luoyaogui,項目名稱:otter-G,代碼行數:20,代碼來源:AESUtils.java


注:本文中的javax.crypto.SecretKey.getEncoded方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。