Reason:
Reverse the order of the encrypted/decrypted seed and the encrypted content. Decrypt (string seed, string encrypted)
Attach AES decryption/encryption Code (Android development):
PackageCom.carspeak.client.util;Importjava.io.UnsupportedEncodingException;Importjava.security.InvalidKeyException;Importjava.security.NoSuchAlgorithmException;ImportJava.security.SecureRandom;Importjavax.crypto.BadPaddingException;ImportJavax.crypto.Cipher;Importjavax.crypto.IllegalBlockSizeException;ImportJavax.crypto.KeyGenerator;Importjavax.crypto.NoSuchPaddingException;ImportJavax.crypto.SecretKey;ImportJavax.crypto.spec.IvParameterSpec;ImportJavax.crypto.spec.SecretKeySpec;/*** AES Encryption and decryption * *@authorHuqiang **/ Public classAesutil { Public Staticstring Encrypt (string seed, string cleartext)throwsException {byte[] Rawkey =Getrawkey (Seed.getbytes ()); byte[] result =Encrypt (Rawkey, cleartext.getbytes ()); returnTohex (Result); } Public Staticstring Decrypt (string seed, string encrypted)throwsException {byte[] Rawkey =Getrawkey (Seed.getbytes ()); byte[] enc =ToByte (encrypted); byte[] result =Decrypt (Rawkey, ENC); return NewString (Result); } Private Static byte[] Getrawkey (byte[] seed)throwsException {keygenerator KGen= Keygenerator.getinstance ("AES"); SecureRandom SR= Securerandom.getinstance ("Sha1prng", "Crypto"); Sr.setseed (seed); Kgen.init (, SR);//192 and availableSecretkey Skey =Kgen.generatekey (); byte[] Raw =skey.getencoded (); returnRaw; } Private Static byte[] Encrypt (byte[] Raw,byte[] Clear)throwsException {secretkeyspec Skeyspec=NewSecretkeyspec (Raw, "AES"); Cipher Cipher= Cipher.getinstance ("AES"); Cipher.init (Cipher.encrypt_mode, Skeyspec,NewIvparameterspec (New byte[Cipher.getblocksize ()])); byte[] encrypted =cipher.dofinal (Clear); returnencrypted; } Private Static byte[] Decrypt (byte[] Raw,byte[] encrypted)throwsException {secretkeyspec Skeyspec=NewSecretkeyspec (Raw, "AES"); Cipher Cipher= Cipher.getinstance ("AES"); Cipher.init (Cipher.decrypt_mode, Skeyspec,NewIvparameterspec (New byte[Cipher.getblocksize ()])); byte[] decrypted =cipher.dofinal (encrypted); returndecrypted; } Private Staticstring Tohex (String txt) {returnTohex (Txt.getbytes ()); } Private Staticstring Fromhex (String hex) {return NewString (ToByte (hex)); } Private Static byte[] ToByte (String hexstring) {intLen = Hexstring.length ()/2; byte[] result =New byte[Len]; for(inti = 0; i < Len; i++) Result[i]= Integer.valueof (hexstring.substring (2 * I, 2 * i + 2), 16). Bytevalue (); returnresult; } Private StaticString Tohex (byte[] buf) { if(BUF = =NULL) return""; StringBuffer result=NewStringBuffer (2 *buf.length); for(inti = 0; i < buf.length; i++) {Appendhex (result, buf[i]); } returnresult.tostring (); } Private Final StaticString HEX = "0123456789ABCDEF"; Private Static voidAppendhex (StringBuffer SB,byteb) {sb.append (Hex.charat (b>> 4) & 0x0f). Append (Hex.charat (b & 0x0f)); }}
AES decryption appears java.lang.NumberFormatException:Invalid int: "CH"