Home > Java > javaTutorial > How to Decrypt OpenSSL AES-256-CBC Encrypted Files in Java?

How to Decrypt OpenSSL AES-256-CBC Encrypted Files in Java?

Susan Sarandon
Release: 2024-12-19 16:30:10
Original
189 people have browsed it

How to Decrypt OpenSSL AES-256-CBC Encrypted Files in Java?

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 ...
}
Copy after login

Notes:

  • ASCII charset is specified, but the actual character set may vary.
  • NIST-approved PBKDF2 algorithm should be used for improved security.
  • OpenSSL 1.1.0c changed its digest algorithm. Specify it explicitly both in the command and in the Java code.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template