Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Java to decrypt openssl aes-256-cbc using provided key and iv

Tags:

java

openssl

I have been searching for a Java code example to do the following but have been unsuccessful. I'm looking for a solution for my particular situation.

A key and IV have been generated using "testtest" for a password:

openssl enc -aes-256-cbc -P 
salt=2855243412E30BD7
key=E4A38479A2349177EAE6038A018483318350E7F5430BDC8F82F1974715CB54E5
iv=629E2E1500B6BA687A385D410D5B08E3

A file(named text) has been encrypted on Linux using openssl command:

openssl enc -aes-256-cbc -K 
E4A38479A2349177EAE6038A018483318350E7F5430BDC8F82F1974715CB54E5 -iv
629E2E1500B6BA687A385D410D5B08E3 -e -in text -out text_ENCRYPTED

It can be decrypted successfully using:

openssl enc -aes-256-cbc -K 
E4A38479A2349177EAE6038A018483318350E7F5430BDC8F82F1974715CB54E5 -iv 
629E2E1500B6BA687A385D410D5B08E3 -d -in text_ENCRYPTED -out text_DECRYPTED

I have access to the encrypted file, the salt, the key and the iv. I do not believe I'm going to receive the password. Also, i have installed the unlimited strength JCE policy. So far I have only found examples where another java program does the encryption and generates these parameters. For my case I must use the salt, key and iv values given to me to decrypt a file. Is this possible with Java? Please remember I'm bound by this configuration, thank you very much for your time and help.

like image 237
user2203629 Avatar asked Mar 24 '13 02:03

user2203629


People also ask

How to implement AES encryption and decryption in Java?

The Advanced Encryption Standard (AES) is a widely used symmetric-key encryption algorithm. In this tutorial, we’ll learn how to implement AES encryption and decryption using the Java Cryptography Architecture (JCA) within the JDK. Use CipherInputStream and CipherOutputStream classes to encrypt and decrypt files in Java.

How to decrypt AES encryption (AES-256-CBC)?

Decrypting: OpenSSL API. To decrypt the output of an AES encryption (aes-256-cbc) we will use the OpenSSL C++ API. Unlike the command line, each step must be explicitly performed with the API.

How to encrypt and decrypt files with OpenSSL?

OpenSSL is a powerful cryptography toolkit that can be used for encryption of files and messages. If you want to use the same password for both encryption of plaintext and decryption of ciphertext, then you have to use a method that is known as symmetric-key algorithm. From this article you’ll learn how to encrypt and decrypt files ...

How to encrypt a password in Java?

In your application, you can store and validate the data in byte array format as well. 2. AES-256 Encryption Java program to encrypt a password (or any information) using AES 256 bits. Do not forget to use the same secret key and salt in encryption and decryption.


1 Answers

You should use something like this:

InputStream cipherInputStream = null;
try {
    final StringBuilder output = new StringBuilder();
    final byte[] secretKey = javax.xml.bind.DatatypeConverter.parseHexBinary("E4A38479A2349177EAE6038A018483318350E7F5430BDC8F82F1974715CB54E5");
    final byte[] initVector = javax.xml.bind.DatatypeConverter.parseHexBinary("629E2E1500B6BA687A385D410D5B08E3");
    final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secretKey, "AES"), new IvParameterSpec(initVector, 0, cipher.getBlockSize()));
    cipherInputStream = new CipherInputStream(new FileInputStream("text_ENCRYPTED"), cipher);

    final String charsetName = "UTF-8";

    final byte[] buffer = new byte[8192];
    int read = cipherInputStream.read(buffer);

    while (read > -1) {
        output.append(new String(buffer, 0, read, charsetName));
        read = cipherInputStream.read(buffer);
    }

    System.out.println(output);
} finally {
    if (cipherInputStream != null) {
        cipherInputStream.close();
    }
}
like image 81
laz Avatar answered Oct 07 '22 20:10

laz