Code with Misuse: |
class LoginFrame {
private final static Cipher getCipher(int mode, String password) throws Exception {
Random random = new Random(43287234L);
byte[] salt = new byte[8];
random.nextBytes(salt);
PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 5);
SecretKey pbeKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(new PBEKeySpec(password.toCharArray()));
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(mode, pbeKey, pbeParamSpec);
return cipher;
}
}
|
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 aesCipherForEncryption = Cipher.getInstance("AES/CBC/PKCS7Padding");
aesCipherForEncryption.init(Cipher.ENCRYPT_MODE, secretKey,
new IvParameterSpec(iv));
byte[] byteDataToEncrypt = strDataToEncrypt.getBytes();
byte[] byteCipherText = aesCipherForEncryption.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 aesCipherForDecryption = Cipher.getInstance("AES/CBC/PKCS7Padding");
aesCipherForDecryption.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
byte[] byteDecryptedText = aesCipherForDecryption.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) {
}
}
}
|