Barrier Barrier - 1 year ago 127
Java Question

Java Encryption: NoSuchAlgorithmException error

I am trying to encrypt some data using AES Encryption and Decryption . In there, I created a

with following paramters,

Algorithm name - AES

Mode - CBC mode

Padding - PKCS7

Cipher aesCipherForEncryption = Cipher.getInstance("AES/CBC/PKCS7PADDING");

Following exception is throwing when I run my encryption code.

No Such Algorithm exists Cannot find any provider supporting DES/CBC/PKCS7Padding.

This is my source code,

String strDataToEncrypt = new String();
String strCipherText = new String();
String strDecryptedText = new String();

try {
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
SecretKey secretKey = keyGen.generateKey();

final int AES_KEYLENGTH = 128;
byte[] iv = new byte[AES_KEYLENGTH / 8];
SecureRandom prng = new SecureRandom();

Cipher aesCipherForEncryption = Cipher.getInstance("AES/CBC/PKCS7PADDING");

aesCipherForEncryption.init(Cipher.ENCRYPT_MODE, secretKey, new IvParameterSpec(iv));

strDataToEncrypt = "Hello World of Encryption using AES ";
byte[] byteDataToEncrypt = strDataToEncrypt.getBytes();
byte[] byteCipherText = aesCipherForEncryption.doFinal(byteDataToEncrypt);

strCipherText = new BASE64Encoder().encode(byteCipherText);
System.out.println("Cipher Text generated using AES is " + strCipherText);

Cipher aesCipherForDecryption = Cipher.getInstance("AES/CBC/PKCS7PADDING");

aesCipherForDecryption.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
byte[] byteDecryptedText = aesCipherForDecryption.doFinal(byteCipherText);
strDecryptedText = new String(byteDecryptedText);

System.out.println(" Decrypted Text message is " + strDecryptedText);

} catch (NoSuchAlgorithmException noSuchAlgo) {
System.out.println(" No Such Algorithm exists " + noSuchAlgo);
} catch (NoSuchPaddingException noSuchPad) {
System.out.println(" No Such Padding exists " + noSuchPad);
} catch (InvalidKeyException invalidKey) {
System.out.println(" Invalid Key " + invalidKey);
} catch (BadPaddingException badPadding) {
System.out.println(" Bad Padding " + badPadding);
} catch (IllegalBlockSizeException illegalBlockSize) {
System.out.println(" Illegal Block Size " + illegalBlockSize);
} catch (InvalidAlgorithmParameterException invalidParam) {
System.out.println(" Invalid Parameter " + invalidParam);

But this is working with PKCS5 with same parameters in

Have any ideas ?

Answer Source

From the documentation, the mentioned padding PKCS7is not supported.

You may refer to this answer for more information

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download