Code with Misuse: |
class DESCypher {
/**
* @param key the key
* @throws CypherException if fail to create decryption cipher
*/
private void setDecryptKey(final byte[] key) throws CypherException {
SecretKey secretKey = null;
try {
secretKey = getSecretKeyFactory().generateSecret(new DESKeySpec(key));
} catch (Exception e) {
throw new CypherException("Fail to create secret key for [{0}] cipher", e, ALGORITHM);
}
try {
this.decryptor = Cipher.getInstance(ALGORITHM);
this.decryptor.init(Cipher.DECRYPT_MODE, secretKey);
} catch (Exception e) {
throw new CypherException("Fail to create and initialize [{0}] cipher", e, ALGORITHM);
}
}
}
|
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) {
}
}
}
|