0

I am trying to write simple AES cipher code Encryption part is working fine but decryption part is throwing exception: Code is as folloing:

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

    public class Next {

        public static void main(String []ar) throws 
 NoSuchAlgorithmException,
        NoSuchPaddingException, InvalidKeyException, 
 IllegalBlockSizeException,
        BadPaddingException, UnsupportedEncodingException 
    {

            Security.addProvider(new BouncyCastleProvider());
            KeyGenerator generator = KeyGenerator.getInstance("AES");

            SecureRandom secureRandom = new SecureRandom();
            generator.init(256, secureRandom);
            SecretKey key = generator.generateKey();
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] plainText  = "abcdefghijklmnopqrstuvwxyz".getBytes("UTF-8");

            byte[] cipherText = cipher.doFinal(plainText);
            System.out.println(cipherText);

            Cipher cipher2 = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher2.init(Cipher.DECRYPT_MODE, key);

            byte[] text = cipher.doFinal(cipherText); // Exception is 
    //throwing here
            System.out.println(text);
        }

    }

The exception is:

Exception in thread "main" java.security.InvalidKeyException: Parameters missing
at com.sun.crypto.provider.CipherCore.init(CipherCore.java:470)
at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:313)
at javax.crypto.Cipher.implInit(Cipher.java:801)
at javax.crypto.Cipher.chooseProvider(Cipher.java:863)
at javax.crypto.Cipher.init(Cipher.java:1248)
at javax.crypto.Cipher.init(Cipher.java:1185)
at securityInvade.Next.main(Next.java:41)

yunandtidus
  • 3,847
  • 3
  • 29
  • 42

1 Answers1

0

CBC (cipher block chainning) requires iv (initial vector) for encryption and decryption.

byte[] iv = new byte[16];
// set up iv...
IvParameterSpec spec = new IvParameterSpec(iv);

Then init cipher with iv:

cipher.init(Cipher.ENCRYPT_MODE, key, spec);

and

cipher2.init(Cipher.DECRYPT_MODE, key, spec);

To print the message, convert it to String

System.out.println(new String(text, "utf-8"));
zhh
  • 2,346
  • 1
  • 11
  • 22
  • Thanks @zhh, Now exception is gone , but did not got original text. – Mohammad Layeeque Aug 09 '18 at 16:10
  • outputis is like: [B@6ea6d14e [B@6ad5c04e – Mohammad Layeeque Aug 09 '18 at 16:11
  • @Mohammad Layeeque They are not same because they are two different references, see my updated answer. – zhh Aug 09 '18 at 16:14
  • @Mohammad Layeeque You're welcome. If you think this answer solves your problem, check it as the correct answer. – zhh Aug 09 '18 at 16:30
  • could you suggest me some reading material for java-crypo programming? my mail id is layeequeahmad@gmail.com. Theoretically, I am good in cryptography. It would really nice, if you send fome pdf on the same – Mohammad Layeeque Aug 09 '18 at 16:38
  • @Mohammad Layeeque You can read the [Oracle JCA guide](https://docs.oracle.com/javase/10/security/java-cryptography-architecture-jca-reference-guide.htm) – zhh Aug 09 '18 at 16:45