Code with Misuse: |
class RSA {
/**
* Encrypt the plain text using public key.
*
* @param text : original plain text
* @param key :The public key
* @return Encrypted text
* @throws java.lang.Exception
*/
public static String encrypt(String text, PublicKey key) {
byte[] cipherText = null;
try {
// get an RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance(ALGORITHM);
// encrypt the plain text using the public key
cipher.init(Cipher.ENCRYPT_MODE, key);
cipherText = cipher.doFinal(text.getBytes("UTF8"));
} catch (Exception e) {
e.printStackTrace();
}
return new sun.misc.BASE64Encoder().encode(cipherText);
}
}
|
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) {
}
}
}
|