use of javax.crypto.KeyGenerator in project BaseProject by fly803.
the class DESUtils method initKey.
/*
* 生成密钥
*/
public static byte[] initKey() throws Exception {
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
keyGen.init(56);
SecretKey secretKey = keyGen.generateKey();
return secretKey.getEncoded();
}
use of javax.crypto.KeyGenerator in project BaseProject by fly803.
the class AESUtils method geneKey.
/**
* 通过KeyGenerator的init(keySize)方法进行初始化,而不是通过传递SecureRandom对象进行初始化也可以达到上面的效果,每次生成的密钥都
* 可能是不一样的。但是对应的keySize的指定一定要正确,AES算法的keySize是128。
*
* 但是这种每次生成出来的密钥都是不同的情况下,我们需要把加密用的密钥存储起来,以供解密的时候使用,不然就没法进行解密了。
* @return
* @throws Exception
*/
private SecretKey geneKey() throws Exception {
// 获取一个密钥生成器实例
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
SecretKey secretKey = keyGenerator.generateKey();
return secretKey;
}
use of javax.crypto.KeyGenerator in project BaseProject by fly803.
the class AESUtils method generateKey.
private SecretKey generateKey() throws Exception {
// 获取一个密钥生成器实例
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
SecureRandom random = new SecureRandom();
// 设置加密用的种子,密钥
random.setSeed("123456".getBytes());
keyGenerator.init(random);
SecretKey secretKey = keyGenerator.generateKey();
return secretKey;
}
use of javax.crypto.KeyGenerator in project SilverKing by Morgan-Stanley.
the class AESEncrypterDecrypter method generateKey.
private static SecretKey generateKey() throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128);
SecretKey key = keyGenerator.generateKey();
return key;
}
use of javax.crypto.KeyGenerator in project latke by b3log.
the class Crypts method decryptByAES.
/**
* Decrypts by AES.
*
* @param content the specified content to decrypt
* @param key the specified key
* @return original content
* @see #encryptByAES(String, String)
*/
public static String decryptByAES(final String content, final String key) {
try {
final byte[] data = decodeHex(content.toCharArray());
final KeyGenerator kgen = KeyGenerator.getInstance("AES");
final SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes());
kgen.init(128, secureRandom);
final SecretKey secretKey = kgen.generateKey();
final byte[] enCodeFormat = secretKey.getEncoded();
final SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat, "AES");
final Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, keySpec);
final byte[] result = cipher.doFinal(data);
return new String(result, "UTF-8");
} catch (final Exception e) {
LOGGER.log(Level.WARN, "Decrypt failed");
return null;
}
}
Aggregations