Code with Misuse: |
class AESUtils {
/**
* AES 加解密
* @param mode
* @param password
* @param textBytes
* @return
* @throws Exception
*/
public static byte[] aes(int mode, String password, byte[] textBytes) throws Exception{
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
//for windows and linux, the secure random need select a algorithm
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(password.getBytes("utf-8"));
keyGenerator.init(128, secureRandom);
SecretKey secretKey = keyGenerator.generateKey();
byte[] encoded = secretKey.getEncoded();
SecretKeySpec secretKeySpec = new SecretKeySpec(encoded, "AES");
Cipher cipher = Cipher.getInstance("AES");// 创建密码器
cipher.init(mode, secretKeySpec);// 初始化
return cipher.doFinal(textBytes);
}
}
|
Code with Pattern(s): |
public class AES {
public void encrypt(String strDataToEncrypt) {
try {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128);
SecretKey secretKey = keyGen.generateKey();
final int AES_KEYLENGTH = 128;
byte[] iv = new byte[AES_KEYLENGTH / 8];
SecureRandom prng = new SecureRandom();
prng.nextBytes(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey,
new IvParameterSpec(iv));
byte[] byteDataToEncrypt = strDataToEncrypt.getBytes();
byte[] byteCipherText = cipher.doFinal(byteDataToEncrypt);
}
catch (NoSuchAlgorithmException noSuchAlgo) {
}
catch (NoSuchPaddingException noSuchPad) {
}
catch (InvalidKeyException invalidKey) {
}
catch (BadPaddingException badPadding) {
}
catch (IllegalBlockSizeException illegalBlockSize) {
}
catch (InvalidAlgorithmParameterException invalidParam) {
}
}
public void decrypt(byte[] cipherText, SecretKey secretKey, byte[] iv){
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
byte[] byteDecryptedText = cipher.doFinal(cipherText);
String decryptedText = new String(byteDecryptedText);
}
catch (NoSuchAlgorithmException noSuchAlgo) {
}
catch (NoSuchPaddingException noSuchPad) {
}
catch (InvalidKeyException invalidKey) {
}
catch (BadPaddingException badPadding) {
}
catch (IllegalBlockSizeException illegalBlockSize) {
}
catch (InvalidAlgorithmParameterException invalidParam) {
}
}
}
|