<span style= "font-family:arial, Helvetica, Sans-serif;" > Yesterday, the man outsourcing Android said AES encryption and decryption results are not the same, so Baidu search found 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 has electronic password this mode ( ecb cbc cfb< Span style= "LINE-HEIGHT:26PX; Color:rgb (51,51,51); Font-family: Song body ">" and output feedback mode ( ofb) four modes, pkcs5padding is fill mode, and there are other fill modes: then Cipher.init () a total of three parameters: 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, if you do not specify, then the program will invoke the default implementation.
Know the reason is good to do, a variety of debugging testing 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 needs 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 no such cheat points!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
AES encryption decryption in Java and Android under interoperability