Android Development decryption is appearing pad block corrupted error

Source: Internet
Author: User
Tags decrypt

Scenario: Running on the virtual machine is normal, but to my real machine decryption failed, appeared pad block corrupted, is said to be version reason: My machine is Xiaomi 3 latest version of Android 4.2

The code that is having the problem:

private static byte[] Getrawkey (byte[] seed) throws Exception {
Keygenerator KGen = keygenerator.getinstance ("AES");
//securerandom sr = securerandom.getinstance ("sha1prng"); //Previous code
securerandom sr = securerandom.getinstance ("Sha1prng", "Crypto"); The code after the modification
Sr.setseed (seed);
Kgen.init (+, SR); 192 and available
Secretkey skey = Kgen.generatekey ();
Byte[] raw = skey.getencoded ();
return raw;
}

The following is a full code for encrypting and decrypting classes:

 PackageCom.example.qurenwu.qurenwu_2.util;ImportJava.security.SecureRandom;ImportJavax.crypto.Cipher;ImportJavax.crypto.KeyGenerator;ImportJavax.crypto.SecretKey;ImportJavax.crypto.spec.SecretKeySpec;/*** AES Encryption *@authorEric_ni **/ Public classAesencryptor {/*** AES Encryption*/     Public StaticString Encrypt (string seed, String cleartext)throwsException {byte[] Rawkey =Getrawkey (Seed.getbytes ()); byte[] result =Encrypt (Rawkey, cleartext.getbytes ()); returnTohex (Result); }    /*** AES Decryption*/     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");securerandom sr = securerandom.getinstance ("Sha1prng", "Crypto");        Sr.setseed (seed); Kgen.init (, SR);//192 and availableSecretkey Skey =Kgen.generatekey (); byte[] Raw =skey.getencoded (); returnRaw; }    /*** Encryption *@paramRaw *@paramClear *@return     * @throwsException*/    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); byte[] encrypted =cipher.dofinal (Clear); returnencrypted; }    /*** Decryption *@paramRaw *@paramEncrypted *@return     * @throwsException*/    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); byte[] decrypted =cipher.dofinal (encrypted); returndecrypted; }     Public Staticstring Tohex (String txt) {returnTohex (Txt.getbytes ()); }     Public Staticstring Fromhex (String hex) {return NewString (ToByte (hex)); }     Public 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; }     Public StaticString Tohex (byte[] buf) {        if(BUF = =NULL)            return""; StringBuffer result=NewStringBuffer (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)); }}

Call Method:

Password = aesencryptor.decrypt (aes_seed, password);  // Aes_seed is the seed code, I do not know what the specific role, anyway can be defined with their own

Android Development decryption is appearing pad block corrupted error

Related Article

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.