0

I am using aes encryption to encrypt the pdf file it encrypts the file without any error but it is giving some error of parameters missing during the decryption of the pdf file.

my code is for encryption and decryption is

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;

public class Encrypt {
    public static void main(String[] args) {
        try{

//            encryptWitEcb("src//d.pdf" , "src//d.enc.pdf" , "f1dda0fc3b96f6d0".getBytes());
            decryptWithEcb("src//d.enc.pdf" , "src//d.dec.pdf" ,"f1dda0fc3b96f6d0".getBytes());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void encryptWitEcb(String filenamePlain, String filenameEnc, byte[] key) throws Exception{
        Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        try (FileInputStream fis = new FileInputStream(filenamePlain);
             BufferedInputStream in = new BufferedInputStream(fis);
             FileOutputStream out = new FileOutputStream(filenameEnc);
             BufferedOutputStream bos = new BufferedOutputStream(out)) {
            byte[] ibuf = new byte[1024];
            int len;
            while ((len = in.read(ibuf)) != -1) {
                byte[] obuf = cipher.update(ibuf, 0, len);
                if (obuf != null)
                    bos.write(obuf);
            }
            byte[] obuf = cipher.doFinal();
            if (obuf != null)
                bos.write(obuf);
        }
    }

    public static void decryptWithEcb(String filenameEnc, String filenameDec, byte[] key) throws Exception
            {
        try (FileInputStream in = new FileInputStream(filenameEnc);
             FileOutputStream out = new FileOutputStream(filenameDec)) {
            byte[] ibuf = new byte[1024];
            int len;
            Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
            SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            while ((len = in.read(ibuf)) != -1) {
                byte[] obuf = cipher.update(ibuf, 0, len);
                if (obuf != null)
                    out.write(obuf);
            }
            byte[] obuf = cipher.doFinal();
            if (obuf != null)
                out.write(obuf);
        }
    }
}

the error occurred during decryption is

java.security.InvalidKeyException: Parameters missing
    at com.sun.crypto.provider.CipherCore.init(CipherCore.java:469)
    at com.sun.crypto.provider.AESCipher.engineInit(AESCipher.java:313)
    at javax.crypto.Cipher.implInit(Cipher.java:805)
    at javax.crypto.Cipher.chooseProvider(Cipher.java:867)
    at javax.crypto.Cipher.init(Cipher.java:1252)
    at javax.crypto.Cipher.init(Cipher.java:1189)
    at Encrypt.decryptWithEcb(Encrypt.java:46)
    at Encrypt.main(Encrypt.java:11)

how can I solve it?

  • GCM mode requires an Nonce respectively an Initialization vector. See here for an example: https://stackoverflow.com/q/60802646/150978 IMPORTANT: THIS NONCE HAS TO BE AT LEAST 96bit AND IT HAS TO BE FULLY RANDOM (or unique per key). – Robert Nov 05 '20 at 13:29
  • thanks, @Robert for the solution – hassan mustafa Nov 05 '20 at 13:57
  • You have forgotten to use `gcmParameterSpec` to introduce the IV, and there is no tag adding there. GCM is an authenticated mode that the tag must be processed correctly. Note that you should be careful on the GCM, see here [What are the rules for using AES-GCM correctly?](https://crypto.stackexchange.com/q/84357/18298) – kelalaka Nov 05 '20 at 16:30
  • 1
    @Robert The NIST doesn't say larger than 12-byte. 12-byte is recommended and using less or greater requires an additional GHASH call, and this removes the deterministic IV generation. This can cause IV collision under the same key, and this is [catastrophic in the GCM](https://stackoverflow.com/a/64507126/1820553) – kelalaka Nov 05 '20 at 16:35

0 Answers0