0

I have been trying for several days to decrypt in java a message encrypted with openssl. The message was encrypted with the following command:

openssl enc -e -aes-256-cbc -kfile $ file.key -in toto -out toto.enc.

The file file.key contains the symmetric key of 256 bits. No salt has been specified in the command and yet the file begins with Salted__. Here is the class that I coded to try to decrypt the file but impossible to get anything even by removing the 16 characters of the file namely the: Salted__ + the salt encrypted. I get the error: Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded.

Could someone help me?

Thank you very much.

  public class Java {

       private static SecretKey key = null;         
       private static Cipher cipher = null;

       public static void main(String[] args) throws Exception
       {
          String filename = RESOURCES_DIR + "toto.enc";

          byte[] key = Base64.decode("2AxIw+/AzDBj83OILV9GDpOs+izDFJEhD6pve/IPsN9=");
          SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
          cipher = Cipher.getInstance("AES");

          cipher.init(Cipher.DECRYPT_MODE, secretKey);
          byte[] test = Base64.decode(readFile(filename));
          byte[] decryptedBytes = cipher.doFinal(test);
          String decryptedText = new String(decryptedBytes, "UTF8");

          System.out.println("After decryption: " + decryptedText);
       }

        public final static String RESOURCES_DIR = "C:/Users/toto/Desktop/";

        static String readFile(String filename) throws FileNotFoundException, IOException {
            FileReader fr;
            BufferedReader br;

            fr = new FileReader(new File(filename));
            br = new BufferedReader(fr);
            String str;
            String res = "";
            while ((str = br.readLine()) != null) {
                res += str;
            }
            return res;
        }
    }
jaz
  • 23
  • 2
  • 7
  • 3
    [Java AES 128 encrypting differently to openssl](http://stackoverflow.com/q/21086103/608639), [Java equivalent of an OpenSSL AES CBC encryption](http://stackoverflow.com/q/32508961/608639), [How to decode a string encoded with openssl aes-128-cbc using java?](http://stackoverflow.com/q/31947256/608639), [Using Java to decrypt openssl aes-256-cbc using provided key and iv](http://stackoverflow.com/q/15594518/608639), etc. – jww Apr 03 '17 at 19:28
  • Exact duplicate of [Java decryption of an encrypted file with openssl aes 256 cbc](https://stackoverflow.com/questions/43189765/java-decryption-of-an-encrypted-file-with-openssl-aes-256-cbc) – Artjom B. Apr 03 '17 at 20:50

0 Answers0