Android aes encryption pad block corrupted -
i using methods below , if enter right key works fine. if enter wrong key receiving badpaddingexception:pad block corrupted... doing wrong?
public void initkey(string passwd, byte[] salt) throws nosuchalgorithmexception, invalidkeyspecexception, nosuchproviderexception{ byte[] localsalt = salt; pbekeyspec password = new pbekeyspec(passwd.tochararray(),localsalt, 1024,128);//, localsalt, 1000, 128); //128bit enc aes secretkeyfactory factory = secretkeyfactory.getinstance("pbewithmd5and128bitaes-cbc-openssl","bc"); pbekey key = (pbekey) factory.generatesecret(password); enckey = new secretkeyspec(key.getencoded(), "aes"); } public string txt2enc(string etxt) throws nosuchalgorithmexception, nosuchpaddingexception, invalidkeyexception, illegalblocksizeexception, badpaddingexception, unsupportedencodingexception { final cipher cipher = cipher.getinstance("aes");//aes cipher.init(cipher.encrypt_mode, enckey); byte[] encrypted = cipher.dofinal((etxt).getbytes("utf-8")); return base64.encodetostring(encrypted, 0); } //decryption public string txt2dec(string dtxt) throws invalidkeyexception, nosuchalgorithmexception, nosuchpaddingexception, illegalblocksizeexception, badpaddingexception, unsupportedencodingexception{ final cipher cipher = cipher.getinstance("aes"); cipher.init(cipher.decrypt_mode, enckey); byte[] decrypt = cipher.dofinal(base64.decode(dtxt, 0)); return new string(decrypt);//return base64.encodetostring(decrypt, 0); }
you're not doing wrong. it's expected. technically duplicate of java encryption issue (which doesn't seem easy find). see answer explanation. can consider adding mac (message authentication code) safely accomplish more checksum.
Comments
Post a Comment