Openssl AES 256 CBC Java Decrypt File with salt

本小妞迷上赌 提交于 2019-12-22 00:31:42

问题


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;
        }
    }

来源:https://stackoverflow.com/questions/43186915/openssl-aes-256-cbc-java-decrypt-file-with-salt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!