AES encryption decryption in Java and Android under interoperability

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif;" > Yesterday the man who outsourced Android said AES's decryption results were different. So Baidu search found is really!</span>

Paste the AES Encryption core:

Cipher cipher = Cipher.getinstance ("aes/cbc/pkcs5padding");
cipher.init ( Cipher.encrypt_mode, Key, ZEROIV)

CBC is working mode, AES a co-owned electronic password this mode ( ecb cbc cfb< Span style= "LINE-HEIGHT:26PX; Color:rgb (51,51,51); Font-family: Four modes of the song Body ">" and the output feedback mode ( ofb pkcs5padding is fill mode, and there are other fill modes: Then, Cipher.init () a co-owner has three references: Cipher.encrypt_ MODE, Key, ZEROIV,ZEROIV is the initialization vector, and a 8 is a character array. working mode, fill mode, initialization vector None of the three factors can be reduced. Otherwise, suppose you don't specify. Then the program will invoke the default implementation.

Know the reason is good to do, a variety of debugging test after the completion of AES in Java and the Security table interoperability.

now paste the core code:

/** fill mode */private static final String transformation = "aes/cbc/pkcs5padding";/** * Encryption * * @param content required to be encrypted * @par AM Password Encrypted password * @return */public static string Encrypt (string content, string password) {try {Ivparameterspec zeroiv = New Ivparameterspec (Password.getbytes ()); Secretkeyspec key1 = new Secretkeyspec (Password.getbytes (), "AES"); Cipher Cipher = cipher.getinstance (transformation); Cipher.init (Cipher.encrypt_mode, Key1, ZEROIV); byte[]        EncryptedData = Cipher.dofinal (Content.getbytes ());        String encryptresultstr = Parsebyte2hexstr (EncryptedData); return ENCRYPTRESULTSTR;} catch (Exception e) {e.printstacktrace ();} return null;} /** * Decrypt * * @param content to decrypt * @param password decryption key * @return */public static string decrypt (string content, string Password) {try {byte[] Decryptfrom = parsehexstr2byte (content); Ivparameterspec zeroiv = new Ivparameterspec ( Password.getbytes ()); Secretkeyspec key1 = new Secretkeyspec (Password.getbytes (), "AES"); Cipher Cipher = CipheR.getinstance (transformation); Cipher.init (Cipher.decrypt_mode, Key1, ZEROIV); byte decrypteddata[] = Cipher.doFinal ( Decryptfrom); return new String (Decrypteddata);} catch (Exception e) {e.printstacktrace ();} return null;} /** convert binary to 16 binary * @param buf * @return */public static String Parsebyte2hexstr (byte buf[]) {StringBuffer SB          = new StringBuffer ();                  for (int i = 0; i < buf.length; i++) {String hex = integer.tohexstring (Buf[i] & 0xFF);                  if (hex.length () = = 1) {hex = ' 0 ' + hex;          } sb.append (Hex.touppercase ());  } return sb.tostring (); }/** converts 16 binary to binary * @param hexstr * @return */public static byte[] Parsehexstr2byte (String hexstr) {if (hex          Str.length () < 1) return null;          Byte[] result = new Byte[hexstr.length ()/2]; for (int i = 0;i< hexstr.length ()/2; i++) {int high = Integer.parsEint (Hexstr.substring (i*2, i*2+1), 16);                  int low = Integer.parseint (Hexstr.substring (i*2+1, i*2+2), 16);          Result[i] = (byte) (high * + low);  } return result;   }
Finally, I would like to say that it took the points to finally solve the interoperability. I just want to say that I don't cheat points with this.



AES encryption decryption in Java and Android under interoperability

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.