0

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;
    }
raj
  • 158
  • 9
  • 1
    What is your question ? – m.raynal Mar 26 '19 at 12:24
  • 1
    *"openssl from command line ..."* - I don't believe OpenSSL supports `PBEWithSHA1And256BitAES`. Also see [Bouncy Castle's Password Based Encryption With AES in CBC mode](https://stackoverflow.com/q/7916617/608639) and [What Java encryption algorithms should I use?](https://stackoverflow.com/q/35685679/608639) – jww Mar 26 '19 at 12:51
  • Some clarification but still not clear if there's a specific question. Please provide the full error message. – kellyfj Mar 26 '19 at 13:07

2 Answers2

1

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

gusto2
  • 8,500
  • 2
  • 14
  • 26
0

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

yachoor
  • 859
  • 1
  • 8
  • 18