Home Java javaTutorial How to Decrypt OpenSSL-Encrypted Files in Java using AES?

How to Decrypt OpenSSL-Encrypted Files in Java using AES?

Dec 03, 2024 am 08:42 AM

How to Decrypt OpenSSL-Encrypted Files in Java using AES?

Decrypting OpenSSL-Encrypted Files using JAVA with AES

In UNIX, you can encrypt files using the following OpenSSL command:

openssl aes-256-cbc -a -salt -in password.txt -out password.txt.enc
Copy after login

To decrypt this file in JAVA, you cannot replicate the exact UNIX command. Instead, you must follow a different yet functional approach to decryption.

JAVA Implementation

Below is a Java code snippet that demonstrates how to decrypt an OpenSSL-encrypted file using AES:

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 {
    private static final Charset ASCII = Charset.forName("ASCII");
    private static final int INDEX_KEY = 0;
    private static final int INDEX_IV = 1;
    private static final int ITERATIONS = 1;

    private static final int ARG_INDEX_FILENAME = 0;
    private static final int ARG_INDEX_PASSWORD = 1;

    private static final int SALT_OFFSET = 8;
    private static final int SALT_SIZE = 8;
    private static final int CIPHERTEXT_OFFSET = SALT_OFFSET + SALT_SIZE;

    private static final int KEY_SIZE_BITS = 256;

    ...

    public static void main(String[] args) {
        ...
        // Parse command-line arguments
        File f = new File(args[ARG_INDEX_FILENAME]);

        // Read and decode base 64 encoded file
        String dataBase64 = new String(Files.readAllBytes(f.toPath()), ASCII);
        byte[] headerSaltAndCipherText = Base64.decode(dataBase64);

        ...

        // Decrypt the file using the provided password and encryption parameters
        byte[] decrypted = aesCBC.doFinal(encrypted);

        // Convert decrypted bytes to a string and print the result
        String answer = new String(decrypted, ASCII);
        System.out.println(answer);
    }
}
Copy after login

This code leverages the EVP_BytesToKey method to derive encryption keys and initialization vectors (IVs) based on the given password. The Cipher class is then used to perform AES decryption with CBC mode and PKCS5 padding.

Additional Notes

  • Beware of character sets: Ensure that the code uses the same character set specified in the OpenSSL command (ASCII in this case).
  • Consider NIST-approved PBKDF2: It's more secure than the default OpenSSL password derivation method.
  • Be mindful of digest algorithm changes: OpenSSL versions may use different digest algorithms, so specify the digest explicitly in both the command line interface and the Java code.

The above is the detailed content of How to Decrypt OpenSSL-Encrypted Files in Java using AES?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does Java's classloading mechanism work, including different classloaders and their delegation models? How does Java's classloading mechanism work, including different classloaders and their delegation models? Mar 17, 2025 pm 05:35 PM

How does Java's classloading mechanism work, including different classloaders and their delegation models?

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte Mar 07, 2025 pm 06:09 PM

Top 4 JavaScript Frameworks in 2025: React, Angular, Vue, Svelte

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution? Mar 17, 2025 pm 05:46 PM

How do I use Maven or Gradle for advanced Java project management, build automation, and dependency resolution?

How can I implement functional programming techniques in Java? How can I implement functional programming techniques in Java? Mar 11, 2025 pm 05:51 PM

How can I implement functional programming techniques in Java?

Node.js 20: Key Performance Boosts and New Features Node.js 20: Key Performance Boosts and New Features Mar 07, 2025 pm 06:12 PM

Node.js 20: Key Performance Boosts and New Features

Iceberg: The Future of Data Lake Tables Iceberg: The Future of Data Lake Tables Mar 07, 2025 pm 06:31 PM

Iceberg: The Future of Data Lake Tables

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading? Mar 17, 2025 pm 05:43 PM

How can I use JPA (Java Persistence API) for object-relational mapping with advanced features like caching and lazy loading?

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache? Mar 17, 2025 pm 05:44 PM

How do I implement multi-level caching in Java applications using libraries like Caffeine or Guava Cache?

See all articles