use of javax.crypto.KeyGenerator in project openzaly by akaxincom.
the class AESCrypto method generateTSKey.
/**
* 随机生成AES加密解密KEY
*
* @return
*/
public static byte[] generateTSKey() {
try {
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(128);
SecretKey secretKey = kg.generateKey();
return secretKey.getEncoded();
} catch (Exception e) {
logger.error("generate ts key error", e);
}
return null;
}
use of javax.crypto.KeyGenerator in project openzaly by akaxincom.
the class AESCrypto method generateTSKey.
/**
* 通过key生成AES加密解密key
*
* @param key
* @return
*/
public static byte[] generateTSKey(String key) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(key.getBytes());
kgen.init(128, secureRandom);
SecretKey secretKey = kgen.generateKey();
return secretKey.getEncoded();
} catch (Exception e) {
logger.error("generate ts key error by key=" + key, e);
}
return null;
}
use of javax.crypto.KeyGenerator in project openzaly by akaxincom.
the class AESCrypto method decrypt.
public static byte[] decrypt(byte[] content, String password) throws Exception {
// 创建AES的Key生产者
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
// 根据用户密码,生成一个密钥
SecretKey secretKey = kgen.generateKey();
// 返回基本编码格式的密钥
byte[] enCodeFormat = secretKey.getEncoded();
// 转换为AES专用密钥
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
// 创建密码器
Cipher cipher = Cipher.getInstance("AES");
// 初始化为解密模式的密码器
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(content);
// 明文
return result;
}
use of javax.crypto.KeyGenerator in project Signal-Android by signalapp.
the class MasterSecretUtil method generateEncryptionSecret.
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;
}
}
use of javax.crypto.KeyGenerator in project faf-java-server by FAForever.
the class UniqueIdServiceTest method toUid.
/**
* This implements the client-side, legacy UID generation.
*/
private static String toUid(String string) throws IOException, NoSuchAlgorithmException, InvalidCipherTextException {
// Step 1: Load public key
AsymmetricKeyParameter publicKey = loadPublicKey();
// Step 2: Generate 16 bytes initialization vector, encode base 64
byte[] initVector = new byte[16];
new SecureRandom().nextBytes(initVector);
byte[] base64InitVector = Base64.getEncoder().encode(initVector);
// Step 3: Generate AES key 16 bytes, encrypt it, encode base 64
KeyGenerator aesKeyGen = KeyGenerator.getInstance("AES");
aesKeyGen.init(128);
SecretKey aesKey = aesKeyGen.generateKey();
byte[] rsaEncryptedAesKey = rsaEncrypt(aesKey.getEncoded(), publicKey);
byte[] rsaEncryptedBsa64EncodedAesKey = Base64.getEncoder().encode(rsaEncryptedAesKey);
// Step 4: AES-encrypt the json string, encode base 64
// Prefix the JSON string with char '2' to indicate the new uid data format for the server
String jsonString = "2" + string;
// Insert trailing bytes to make len(json_string) a multiple of 16
int jsonLength = jsonString.length();
int trailLength = ((((jsonLength / 16) + 1) * 16) - jsonLength);
// Because someone thought manual padding would be necessary
jsonString = jsonString + new String(new char[trailLength]).replace("\0", "x");
byte[] encryptedString = aesEncrypt(initVector, jsonString.getBytes(UTF_8), aesKey.getEncoded());
byte[] encryptedStringBase64 = Base64.getEncoder().encode(encryptedString);
// Step 5: put message together
byte[] messageBytes = Bytes.concat(new byte[] { (byte) trailLength }, base64InitVector, encryptedStringBase64, rsaEncryptedBsa64EncodedAesKey);
return Base64.getEncoder().encodeToString(messageBytes);
}
Aggregations