Decrypting OpenSSL-encrypted file on Android with Java -


i'm trying implement file decryption on android app.

the file encrypted on host(linux) using :

openssl aes-128-ecb -salt -k $hash -in somefile.in -out somefile openssl aes-256-cbc -salt -k $hash -iv $iv -md sha1 -in somefile.in -out somefile openssl aes-256-cbc -d -salt -k $hash -md sha1 -in somefile.in -out somefile 

the problem that, cannot of these combinations(128/256, ecb/cbc, salt/nosalt, -k/-k, -md/none) decrypt on android.

it either decrypts wrong(corrupted), or throws exception.

exception @ decryptaes java.io.ioexception: error while finalizing cipher     @ javax.crypto.cipherinputstream.fillbuffer(cipherinputstream.java:104)     @ javax.crypto.cipherinputstream.read(cipherinputstream.java:155)     @ java.io.inputstream.read(inputstream.java:162)     @ com.temp.temp.cryptographyhelper.decryptaes(cryptographyhelper.java:58)     @ com.temp.temp.mainactivity.__prepfirstlaunch(mainactivity.java:229)     @ com.temp.temp.mainactivity.prepfirstlaunch(mainactivity.java:192)     @ com.temp.temp.mainactivity$prepthread.run(mainactivity.java:42) caused by: javax.crypto.badpaddingexception: evp_cipherfinal_ex     @ com.android.org.conscrypt.nativecrypto.evp_cipherfinal_ex(native method)     @ com.android.org.conscrypt.opensslcipher.dofinalinternal(opensslcipher.java:430)     @ com.android.org.conscrypt.opensslcipher.enginedofinal(opensslcipher.java:490)     @ javax.crypto.cipher.dofinal(cipher.java:1314)     @ javax.crypto.cipherinputstream.fillbuffer(cipherinputstream.java:102)     ... 6 more 

here's current java code(which not working) on android app.

public static inputstream decryptaes(context context) {     inputstream ris = null;      try {         inputstream fis = context.getassets().open("somefile");         fileoutputstream baos = new fileoutputstream("/sdcard/decrypted");         string hash = "somehash";         string ivs = "someiv";          ivparameterspec iv = new ivparameterspec(ivs.getbytes("utf-8"));         secretkeyspec sks = new secretkeyspec(hash.getbytes("utf-8"), "aes");          // none of these work         cipher cipher = cipher.getinstance("aes");         //cipher cipher = cipher.getinstance("aes/ecb/pkcs5padding");         //cipher cipher = cipher.getinstance("aes/ecb/zerobytepadding");         //cipher cipher = cipher.getinstance("aes/cbc/zerobytepadding");         //cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding");         cipher.init(cipher.decrypt_mode, sks);         //cipher.init(cipher.decrypt_mode, sks, iv);         cipherinputstream cis = new cipherinputstream(fis, cipher);         int b;         byte[] d = new byte[1024 * 32];         while ((b = cis.read(d)) != -1) {             baos.write(d, 0, b);         }         baos.flush();         baos.close();         cis.close();     } catch (exception e) {         // meh     }      return ris; } 

i don't care encryption method(128/256, salt/nosalt, ecb/cbc) end nothing critical happens if gets cracked.

can suggest me how tweak code or new code new openssl command combination?

tl;dr - need android java code can decrypt file encrypted on linux via openssl command.

if encrypt file using openssl:

> echo "some test" > test.txt > openssl aes-128-cbc -k "000102030405060708090a0b0c0d0e0f" -iv "77665544332211000011223344556677" -in test.txt -out test.enc 

i can decrypt in java:

public static void main(string[] args) {     try {         byte[] keybytes = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};         byte[] ivbytes =  {0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};          secretkeyspec sks = new secretkeyspec(keybytes, "aes");         ivparameterspec iv = new ivparameterspec(ivbytes);          cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding");         cipher.init(cipher.decrypt_mode, sks, iv);          // read file byte[]         inputstream = new fileinputstream("test.enc");         bytearrayoutputstream baos = new bytearrayoutputstream();         int b;         while ((b = is.read()) != -1) {             baos.write(b);         }         byte[] filebytes = baos.tobytearray();          byte[] decrypted = cipher.dofinal(filebytes);         system.out.println(new string(decrypted));      } catch (exception e) {         e.printstacktrace();     } } 

result:

some test 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -