java me - J2ME AES Decryption Error(org.bouncycastle.crypto.InvalidCipherTextException: pad block corrupted) -
i doing encryption , decryption using aes algorithm bouncy castle
my encryption , decryption works ok gives me error when plain text size bigger
even giving non decrypted data
public static boolean setencryptionkey(string keytext) { byte[] keybytes = keytext.getbytes(); key = new keyparameter(keybytes); engine = new aesfastengine(); cipher = new paddedbufferedblockcipher(engine); return true; }
encryption:
public static string encryptstring(string plaintext) { byte[] plainarray = plaintext.getbytes(); cipher.init(true, key); byte[] cipherbytes = new byte[cipher.getoutputsize(plainarray.length)]; int cipherlength = cipher.processbytes(plainarray, 0, plainarray.length, cipherbytes, 0); cipher.dofinal(cipherbytes, cipherlength); string cipherstring = new string(cipherbytes); return cipherstring; }
decryption:
public static string decryptstring(string encryptedtext) { byte[] cipherbytes = encryptedtext.getbytes(); cipher.init(false, key); byte[] decryptedbytes = new byte[cipher.getoutputsize(cipherbytes.length)]; int decryptedlength = cipher.processbytes(cipherbytes, 0, cipherbytes.length, decryptedbytes, 0); cipher.dofinal(decryptedbytes, decryptedlength); string decryptedstring = new string(decryptedbytes); int index = decryptedstring.indexof("\u0000"); if (index >= 0) { decryptedstring = decryptedstring.substring(0, index); } return decryptedstring; }
this decryption giving me following error
org.bouncycastle.crypto.invalidciphertextexception: pad block corrupted @ org.bouncycastle.crypto.paddings.pkcs7padding.padcount(+30) @ org.bouncycastle.crypto.paddings.paddedbufferedblockcipher.dofinal(+190) @ com.newcrypto.decryptstring(newcrypto.java:103) @ com.new_midlet.startapp(new_midlet.java:23) @ javax.microedition.midlet.midletproxy.startapp(midletproxy.java:44) @ com.sun.midp.midlet.scheduler.schedule(scheduler.java:375) @ com.sun.midp.main.main.runlocalclass(main.java:477) @ com.sun.midp.main.main.main(+80)
what problem ?
the line
string cipherstring = new string(cipherbytes);
is bug. cipherbytes
byte array arbitrary values , cannot converted string using of java string decoders. should send/save cipher byte array. if must make string you'll have use encoder. base64 encoders used, base16 (hex). can use apache commons codec or favorite, harder base64 codec.
Comments
Post a Comment