Android system AES decryption server encrypted data error solution

Source: Internet
Author: User

Prior to do an outsourced project, there are some sensitive information needs AES encryption, but the same code in the server can be encrypted decryption, in the Android system decryption failure, there will be pad block corrupted error, the following solution, pro-Test no problem!

Aes.java

Public class AES {

public static final String vipara = "0123456789abcdef";

public static final String BM = "UTF-8";

public static string encrypt (string Datapassword, String cleartext)

throws Exception {

Ivparameterspec zeroiv = new Ivparameterspec (vipara.getbytes ());

secretkeyspec key = new Secretkeyspec (Datapassword.getbytes (), "AES");

Cipher Cipher = cipher.getinstance ("aes/cbc/pkcs5padding");

cipher.init (cipher. Encrypt_mode, key, ZEROIV);

byte[] EncryptedData = cipher.dofinal (cleartext.getbytes (BM));

return Base64.encode (EncryptedData);

}

public static string decrypt (string Datapassword, String encrypted)

throws Exception {

byte[] Bytemi = Base64.decode (encrypted);

Ivparameterspec zeroiv = new Ivparameterspec (vipara.getbytes ());

secretkeyspec key = new Secretkeyspec (Datapassword.getbytes (), "AES");

Cipher Cipher = cipher.getinstance ("aes/cbc/pkcs5padding");

cipher.init (cipher. Decrypt_mode, key, ZEROIV);

byte[] decrypteddata = cipher.dofinal (Bytemi);

return new String (Decrypteddata,BM);

}

}

Base64.java

Public class BASE64 {

private static final char[] legalchars = " abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/". ToCharArray ();

/**

* data[] to encode

* @param data

* @return

*/

public static String encode (byte[] data) {

int start = 0;

int len = data. length;

StringBuffer buf = new StringBuffer (data. Length * 3/2);

int end = len-3;

int i = start;

int n = 0;

While (i <= end) {

int d = (((((int) data[i]) & 0x0ff) <<)

| (((((int) Data[i + 1]) & 0X0FF) << 8)

| (((int) Data[i + 2]) & 0X0FF);

Buf.append (legalchars[(d >>) &]);

Buf.append (legalchars[(d >>) &));

Buf.append (legalchars[(d >> 6) &]);

Buf.append (legalchars[d &));

i + = 3;

if (n++ >=) {

n = 0;

Buf.append ("");

}

}

if (i = = start + len-2) {

int d = (((((int) data[i]) & 0x0ff) <<)

| (((((int) Data[i + 1]) & 255) << 8);

Buf.append (legalchars[(d >>) &]);

Buf.append (legalchars[(d >>) &));

Buf.append (legalchars[(d >> 6) &]);

Buf.append ("=");

} Else if (i = = start + len-1) {

int d = (((int) data[i]) & 0x0ff) <<;

Buf.append (legalchars[(d >>) &]);

Buf.append (legalchars[(d >>) &));

Buf.append ("= =");

}

return buf.tostring ();

}

private static int decode (char c) {

if (c >= ' A ' && c <= ' Z ')

return (int) c)-

Else if (c >= ' a ' && c <= ' z ')

return (int) c)-+ +;

Else if (c >= ' 0 ' && C <= ' 9 ')

return (int) c)-+ + +;

Else

switch (c) {

case ' + ':

return;

case '/':

return;

case ' = ':

return 0;

Default:

throw New RuntimeException ("Unexpected code:" + C);

}

}

/**

* Decodes the given BASE64 encoded String to a new byte array. The byte

* Array holding the decoded data is returned.

*/

public static byte[] Decode (String s) {

Bytearrayoutputstream BOS = new Bytearrayoutputstream ();

try {

Decode (S, BOS);

} catch (IOException e) {

throw new RuntimeException ();

}

byte[] decodedbytes = Bos.tobytearray ();

try {

Bos.close ();

BOS = null;

} catch (IOException ex) {

System. err.println ("Error while decoding BASE64:" + ex.tostring ());

}

return decodedbytes;

}

private static void decode (String s, OutputStream os) throws IOException {

int i = 0;

int len = s.length ();

While (true) {

While (i < len && S.charat (i) <= ")

i++;

if (i = = len)

Break ;

int tri = (decode (S.charat (i)) <<)

+ (Decode (S.charat (i + 1)) << 12)

+ (Decode (S.charat (i + 2)) << 6)

+ (Decode (S.charat (i + 3)));

Os.write (Tri >>) & 255);

if (S.charat (i + 2) = = ' = ')

Break ;

Os.write (Tri >> 8) & 255);

if (S.charat (i + 3) = = ' = ')

Break ;

Os.write (Tri & 255);

i + = 4;

}

}

}

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.