encryption - AES - simple encrypt in Java, decrypt with openssl -


i trying simple aes encryption in java, using java cryto, can decrypted in objectivec, using openssl.

as not doing objectivec side, want make sure works, using openssl command line, "bad magic number"

here java code

public class encryptionutils {  private static final string aes_cipher_method = "aes"; private static final int aes_key_size = 128;  public static byte[] generateaeskey() throws nosuchalgorithmexception {     keygenerator keygenerator = keygenerator.getinstance(aes_cipher_method);     keygenerator.init(aes_key_size);     secretkey key = keygenerator.generatekey();     return key.getencoded(); }  public static secretkeyspec createaeskeyspec(byte[] aeskey) {     return new secretkeyspec(aeskey, aes_cipher_method); }  public static void aesencryptfile(file in, file out, secretkeyspec aeskeyspec) throws invalidkeyexception, nosuchalgorithmexception, nosuchpaddingexception, ioexception {     cipher aescipher = cipher.getinstance(aes_cipher_method);     aescipher.init(cipher.encrypt_mode, aeskeyspec);     inputstream inputstream = new fileinputstream(in);     try {         outputstream outputstream = new cipheroutputstream(new fileoutputstream(out), aescipher);         try {             ioutils.copy(inputstream , outputstream);         } {             outputstream.close();         }     } {         inputstream.close();     } } }   //testcode @test public void testaesencryptfile() throws ioexception, invalidkeyexception, illegalblocksizeexception, badpaddingexception, nosuchalgorithmexception, nosuchpaddingexception {     byte[] aeskey = encryptionutils.generateaeskey();     secretkeyspec aeskeyspec = encryptionutils.createaeskeyspec(aeskey);     encryptionutils.aesencryptfile(new file("c:\\test\\test.txt"), new file("c:\\test\\test-encrypted.txt"), aeskeyspec);      fileoutputstream outputstream = new fileoutputstream("c:\\test\\aes.key");     outputstream.write(aeskey);     outputstream.close(); }  @test public void testaesdecryptfile() throws ioexception, invalidkeyexception, illegalblocksizeexception, badpaddingexception, nosuchalgorithmexception, nosuchpaddingexception {     fileinputstream keyfis = new fileinputstream("c:\\test\\aes.key");     bytearrayoutputstream keybaos = new bytearrayoutputstream();     ioutils.copy(keyfis, keybaos);      secretkeyspec keyspec = new secretkeyspec(keybaos.tobytearray(), "aes");     cipher cipher = cipher.getinstance("aes");     cipher.init(cipher.decrypt_mode, keyspec);      fileinputstream fileinputstream = new fileinputstream("c:\\test\\test-encrypted.txt");     bytearrayoutputstream baos = new bytearrayoutputstream();     ioutils.copy(fileinputstream, baos);      byte[] decrypted = cipher.dofinal(baos.tobytearray());     fileoutputstream outputstream = new fileoutputstream("c:\\test\\test-decrypted.txt");     outputstream.write(decrypted);     outputstream.close();  } 

now runs expected, file "test-encrypted.txt" indeed encrypted, , "test-decrypted.txt" == "test.txt"

i tried run decryption on command line using openssl

openssl enc -d -aes-128-ecb -in test-encrypted.txt -k aes.key 

however, give me

bad magic number 

from can see, using algorithm "aes" in java uses "ecb" mode default, above should work. doing wrong.

the problem key. -k argument expects passphrase, not file. in turn, when passphrase used openssl encryption routine, magic , salt put in front of encrypted result. that's magic cannot found.

to use openssl command line, print out key in hex , use -k option instead of lowercase -k option.

you use:

`cat aes.key` 

as argument after -k, given aes.key contains key in hexadecimals.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -