Java decryption of an encrypted file with openssl aes 256 cbc

痴心易碎 提交于 2019-11-30 16:43:50
dave_thompson_085

No, file.key does not contain the key. openssl enc -kfile reads a password which is NOT the key but is used to derive the key and also IV (when applicable, and it is here); see the man page. This key derivation uses random salt by default, and since 2016-08 the default hash depends on OpenSSL version which you didn't state. Also, Cipher.getInstance("AES") in Java defaults to ECB not CBC as you need. (It also defaults to 'PKCS5' padding, which does match OpenSSL, even though technically it should be called PKCS7 not PKCS5.)

To match in Java the PBKDF (and thus key and IV) used by openssl enc you can either use BouncyCastle or code the equivalent of OpenSSL's EVP_BytesToKey; see dupe or near-dupe Qs:
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?
and my attempt at canonical https://crypto.stackexchange.com/questions/3298/is-there-a-standard-for-openssl-interoperable-aes-encryption/#35614

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