raj
raj

Reputation: 168

AES Encryption by Java code and decryption by using OpenSSL (terminal)

I am able to encrypt the data in the file using below mentioned Java code. But when I try to decrypt the encrypted file using OpenSSL from the command line then I am not be able to do that.

I tried the command

openssl enc -aes-256-cbc -d -in file_05.encrypt.txt -out file_05.decrypt.txt

It asked for a password - enter aes-256-cbc decryption password: I entered the password as "helloworld",

Then in the terminal it shows a "bad magic number" error message.

String pwd  = "helloworld";
String SALT_VALUE  = "12345@salt";
private String algorithm = "PBEWITHSHA256AND256BITAES-CBC-BC";


String finalTxt  = "Hi, Goof After noon All.";

char[] pwd = Crypt.password.toCharArray();

SecretKey originalKey = Crypt.generateSK(pwd);

byte[] cipherText = Crypt.encrypt(finalTxt.getBytes(),SALT_VALUE.getBytes(), originalKey);

public static SecretKey generateSK(char[] passPhrase) throws NoSuchAlgorithmException,
                                                             InvalidKeySpecException,
                                                             NoSuchPaddingException,
                                                             InvalidAlgorithmParameterException,
                                                             InvalidKeyException {

    PBEKeySpec pbeKeySpec = new PBEKeySpec(passPhrase);
    SecretKeyFactory secretKeyFactory;
    secretKeyFactory = SecretKeyFactory.getInstance(algorithm);
    return secretKeyFactory.generateSecret(pbeKeySpec);
}



public static byte[] encrypt(byte[] image, byte[] salt, SecretKey sKey) throws InvalidKeyException,
            IllegalBlockSizeException,
            BadPaddingException,
            InvalidKeySpecException,
            UnsupportedEncodingException,
            InvalidAlgorithmParameterException {
        Cipher cipher;
        try {
            cipher = getCipher(Cipher.ENCRYPT_MODE, salt, sKey);
            return cipher.doFinal(image);
        } catch (Exception e) {
            e.printStackTrace();

        }

        return null;
    }

private static Cipher getCipher(int mode, @NonNull byte[] salt, @NonNull SecretKey secretKey) throws Exception {
        PBEParameterSpec pbeParamSpecKey = new PBEParameterSpec(salt, 1000);
            Cipher cipher = Cipher.getInstance(algorithm);
            cipher.init(mode, secretKey, pbeParamSpecKey);
            return cipher;
    }

Upvotes: 0

Views: 453

Answers (2)

gusto2
gusto2

Reputation: 12075

It asked for a password - enter aes-256-cbc decryption password: I entered the password as "helloworld",
Then in the terminal it shows a "bad magic number" error message

Openssl uses by default its internal EVP_BytesToKey function to generate key and IV from provided password and salt. Just search on internet to find Java implementation if needed.

By default Openssl expect format Salted__<8bit_salt><ciphertext> if you don't provide key and IV directly.

I try to decrypt the encrypted file using OpenSSL from the command line then I am not be able to do that

I am not sure what your Crypt class is implemented, you may try to print hex encoded key and iv. The using openssl with parameters -iv <hex_IV> -K <hex_key> you can directly provide the IV and Key value to decrypt the ciphertext

Upvotes: 1

yachoor
yachoor

Reputation: 939

It seems like you are missing header expected by openssl - string Salted__, followed by 8 bytes salt, followed by ciphertext.

Upvotes: 0

Related Questions