android - Encryption failed in Marshmallow -
i trying encrypt , decrypt data sd card tried oon code , working proper in lower version devices getting 1 exception in marshmallow.
04-19 09:56:50.428: w/system.err(15137): javax.crypto.badpaddingexception: pad block corrupted 04-19 09:56:50.432: w/system.err(15137): @ com.android.org.bouncycastle.jcajce.provider.symmetric.util.baseblockcipher$bufferedgenericblockcipher.dofinal(baseblockcipher.java:1011) 04-19 09:56:50.432: w/system.err(15137): @ com.android.org.bouncycastle.jcajce.provider.symmetric.util.baseblockcipher.enginedofinal(baseblockcipher.java:849) 04-19 09:56:50.432: w/system.err(15137): @ javax.crypto.cipher.dofinal(cipher.java:1502) 04-19 09:56:50.432: w/system.err(15137): @ com.example.encryptaudio.cryptofile.decrypt(cryptofile.java:34) 04-19 09:56:50.432: w/system.err(15137): @ com.example.encryptaudio.mainactivity$2.onclick(mainactivity.java:116) 04-19 09:56:50.432: w/system.err(15137): @ android.view.view.performclick(view.java:5201) 04-19 09:56:50.432: w/system.err(15137): @ android.view.view$performclick.run(view.java:21163) 04-19 09:56:50.432: w/system.err(15137): @ android.os.handler.handlecallback(handler.java:746) 04-19 09:56:50.432: w/system.err(15137): @ android.os.handler.dispatchmessage(handler.java:95) 04-19 09:56:50.432: w/system.err(15137): @ android.os.looper.loop(looper.java:148) 04-19 09:56:50.432: w/system.err(15137): @ android.app.activitythread.main(activitythread.java:5443) 04-19 09:56:50.432: w/system.err(15137): @ java.lang.reflect.method.invoke(native method) 04-19 09:56:50.432: w/system.err(15137): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:728)
the code following encryption
cryptofile simplecrypto = new cryptofile(); try { // encrypt audio file send second argument , corresponding key in first argument. incrept = simplecrypto.encrypt(key, getaudiofile()); //store encrypted file in sd card of mobile name vincent.mp3. fileoutputstream fos = new fileoutputstream(new file("/sdcard/vincent.mp3")); fos.write(incrept); fos.close(); } catch (exception e) { e.printstacktrace(); } }
decryption
cryptofile simplecrypto = new cryptofile(); try { // decrypt file here first argument key , second encrypted file sd card. decrpt = simplecrypto.decrypt(key, getaudiofilefromsdcard()); //play decrypted audio file. playmp3(decrpt); } catch (exception e) { e.printstacktrace(); } }
crypt.java
public class cryptofile { public byte[] encrypt(string seed, byte[] cleartext) throws exception { byte[] rawkey = getrawkey(seed.getbytes()); byte[] result = encrypt(rawkey, cleartext); // return tohex(result); return result; } public byte[] decrypt(string seed, byte[] encrypted) throws exception { byte[] rawkey = getrawkey(seed.getbytes()); byte[] enc = encrypted; byte[] result = decrypt(rawkey, enc); return result; } //done private byte[] getrawkey(byte[] seed) throws exception { keygenerator kgen = keygenerator.getinstance("aes"); securerandom sr = securerandom.getinstance("sha1prng"); sr.setseed(seed); kgen.init(128, sr); // 192 , 256 bits may not available secretkey skey = kgen.generatekey(); byte[] raw = skey.getencoded(); return raw; } private byte[] encrypt(byte[] raw, byte[] clear) throws exception { secretkeyspec skeyspec = new secretkeyspec(raw, "aes"); cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.encrypt_mode, skeyspec); byte[] encrypted = cipher.dofinal(clear); return encrypted; } private byte[] decrypt(byte[] raw, byte[] encrypted) throws exception { secretkeyspec skeyspec = new secretkeyspec(raw, "aes"); cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.decrypt_mode, skeyspec); byte[] decrypted = cipher.dofinal(encrypted); return decrypted; } }
aes keys should consist of random data. if store them string loose information. not specify encoding type either in encryption or decryption.
encryption
encryptedtext = c.dofinal(cleartext.getbytes("utf-8")); return base64.encodetostring(encryptedtext, base64.default);
decryption
cleartext = c.dofinal(base64.decode(encryptedtext, base64.default)); return new string(cleartext, "utf-8");
Comments
Post a Comment