About Android AES parts machine Javax.crypto.BadPaddingException:pad block corrupted

Source: Internet
Author: User
Tags decrypt

Many people Android above using AES appear javax.crypto.BadPaddingException:pad block corrupted under the code without problems, their own control, do not explain!


public static Class cryptogram{



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 (+, SR); 192 and 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,new ivparameterspec (New Byte[cipher.getblocksize ()));
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,new ivparameterspec (New Byte[cipher.getblocksize ()));
byte[] decrypted = cipher.dofinal (encrypted);
return decrypted;
}


private static string Tohex (String txt) {
Return Tohex (Txt.getbytes ());
}
private static string Fromhex (String hex) {
return new String (ToByte (hex));
}

private static byte[] ToByte (String hexstring) {
int len = Hexstring.length ()/2;
Byte[] result = new Byte[len];
for (int i = 0; i < len; i++)
Result[i] = integer.valueof (hexstring.substring (2*i, 2*i+2), +). Bytevalue ();
return result;
}


private static String Tohex (byte[] buf) {
if (buf = = null)
Return "";
StringBuffer result = new StringBuffer (2*buf.length);
for (int i = 0; i < buf.length; i++) {
Appendhex (result, buf[i]);
}
return result.tostring ();
}
Private final static String HEX = "0123456789ABCDEF";
private static void Appendhex (StringBuffer sb, byte b) {
Sb.append (Hex.charat ((b>>4) &0x0f)). Append (Hex.charat (b&0x0f));
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.