本文整理汇总了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();
}
示例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();
}
}
示例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");
}
}
示例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);
}
示例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();
}
示例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();
}
示例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);
}
}
示例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();
}
示例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;
}
}
示例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();
}
示例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");
}
}
示例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;
}
示例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();
}
示例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();
}
示例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);
}
}