【问题标题】:Java decryption of an encrypted file with openssl aes 256 cbc使用openssl aes 256 cbc对加密文件进行Java解密
【发布时间】:2017-08-28 14:56:49
【问题描述】:

我已经尝试了好几天在 java 中解密用 openssl 加密的消息。该消息已使用以下命令加密:

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

文件 file.key 包含 256 位的对称密钥。命令中没有指定盐,但文件以 Salted__ 开头。这是我为尝试解密文件而编写的类,但即使删除文件的 16 个字符也无法获得任何东西,即:Salted__ + 加密的盐。我知道 openssl 默认是这样做的。当我尝试破译时,会引发与加密文本相关的异常。

有人可以帮我吗?轨道 ?一个想法?

非常感谢。

代码:

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

错误:

Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313)
    at javax.crypto.Cipher.doFinal(Cipher.java:2131)
    at deciphertodeploytest6.Java.main(Java.java:52)

【问题讨论】:

标签: openssl aes java salt encryption


【解决方案1】:

不,file.key 不包含密钥。 openssl enc -kfile 读取一个 password,它不是密钥,但用于 派生 密钥和 IV(如果适用,它就在这里);请参阅手册页。默认情况下,此密钥派生使用随机盐,并且自 2016-08 以来,默认哈希取决于您未声明的 OpenSSL 版本。 此外,Java 中的 Cipher.getInstance("AES") 默认为 ECB 而不是您需要的 CBC。 (它也默认为 'PKCS5' 填充,它与 OpenSSL 匹配,尽管从技术上讲它应该被称为 PKCS7 而不是 PKCS5。)

要在 Java 中匹配 openssl enc 使用的 PBKDF(以及密钥和 IV),您可以使用 BouncyCastle 或编写相当于 OpenSSL 的 EVP_BytesToKey 的代码;见骗子或近乎骗子的问题:
Java equivalent of an OpenSSL AES CBC encryption
How to decode a string encoded with openssl aes-128-cbc using java?
How to decrypt AES encrypted file with '-nosalt' param
How to decrypt file in Java encrypted with openssl command using AES?
以及我对规范 https://crypto.stackexchange.com/questions/3298/is-there-a-standard-for-openssl-interoperable-aes-encryption/#35614 的尝试

【讨论】:

    猜你喜欢
    • 2013-08-11
    • 1970-01-01
    • 2020-01-20
    • 2020-08-20
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    相关资源
    最近更新 更多