Decrypting File Encrypted with OpenSSL in Java Using AES
Challenge:
Users need to decrypt a file encrypted in UNIX using the openssl command with AES-256-CBC encryption in Java. The password is required as well.
OpenSSL Decryption with Java:
OpenSSL employs its own password-based key derivation method. The ciphertext is also implicitly encoded as Base64.
Cryptic Algorithm Defined:
salt = random(8)
keyAndIV = BytesToKey(password, salt, 48)
key = keyAndIV[0..31]
iv = keyAndIV[32..47]
ct = AES-256-CBC-encrypt(key, iv, plaintext)
Java Implementation:
import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.security.GeneralSecurityException; import java.security.MessageDigest; import java.util.Arrays; import java.util.List; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.bouncycastle.util.encoders.Base64; public class OpenSSLDecryptor { // ... Code as in the given answer ... }
Notes:
The above is the detailed content of How to Decrypt OpenSSL AES-256-CBC Encrypted Files in Java?. For more information, please follow other related articles on the PHP Chinese website!