java - Android javax.crypto.BadPaddingException: pad block corrupted, Is there an alternative or Fix? -


i trying encrypt data in android app. having following exception.

 javax.crypto.badpaddingexception: pad block corrupted 

at line

         byte[] decrypted = cipher.dofinal(encrypted); 

my code follows

 private final static string hex = "0123456789abcdef";   public static string encryptstring(string str) throws exception {     return encrypt("key123", str); }  public static string decryptstring(string str) throws exception {     return decrypt("key123", str); }  public static string encrypt(string seed, string cleartext) throws exception {     byte[] rawkey = getrawkey(seed.getbytes());     byte[] result = encrypt(rawkey, cleartext.getbytes());     return tohex(result); }  public static string decrypt(string seed, string encrypted) throws exception {     byte[] rawkey = getrawkey(seed.getbytes());     byte[] enc = tobyte(encrypted);     byte[] result = decrypt(rawkey, enc);     return new string(result); }  private static byte[] getrawkey(byte[] seed) throws exception {     keygenerator kgen = keygenerator.getinstance("aes");     securerandom sr = securerandom.getinstance("sha1prng", "crypto");     sr.setseed(seed);     kgen.init(128, sr); // 192 , 256 bits may not available     secretkey skey = kgen.generatekey();     byte[] raw = skey.getencoded();     return raw; }  private static 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 static 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; }  public static string tohex(string txt) {     return tohex(txt.getbytes()); }  public static string fromhex(string hex) {     return new string(tobyte(hex)); }  public static byte[] tobyte(string hexstring) {     int len = hexstring.length() / 2;     byte[] result = new byte[len];     (int = 0; < len; i++)         result[i] = integer.valueof(hexstring.substring(2 * i, 2 * + 2), 16).bytevalue();     return result; }  public static string tohex(byte[] buf) {     if (buf == null)         return "";     stringbuffer result = new stringbuffer(2 * buf.length);     (int = 0; < buf.length; i++) {         appendhex(result, buf[i]);     }     return result.tostring(); }  private static void appendhex(stringbuffer sb, byte b) {     sb.append(hex.charat((b >> 4) & 0x0f)).append(hex.charat(b & 0x0f)); } 

i using encryptstring , encryptstring methods throughout app encrypt , decrypt data, tried converting securerandom sr = securerandom.getinstance("sha1prng", "crypto"); securerandom sr = securerandom.getinstance("sha1prng"); , vice versa nothing helped me.

are there other techniques can use apart above technique?

update:-

added hex , if don't know solution or answer, there no need downvote mind own business , move ahead.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -