AES decryption appears java.lang.NumberFormatException:Invalid int: "CH"

Source: Internet
Author: User

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"

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.