Aes,des encryption and decryption on Android platform

Source: Internet
Author: User
Tags decrypt

when using Java for AES encryption, the following methods are used:securerandom sr = securerandom.getinstance ("sha1prng");
However, in the android4.2 above encryption, decryption, will be reported similar to the badpadding exception, but the code to replace the method is all OK. securerandom sr = securerandom.getinstance ("sha1prng", "Crypto");
AES Encryption algorithm:
Import Java.security.securerandom;import Javax.crypto.cipher;import Javax.crypto.keygenerator;import Javax.crypto.secretkey;import Javax.crypto.spec.secretkeyspec;public class Aesutil {public static String encrypt ( String seed, String cleartext) throws Exception {byte[] Rawkey = Getrawkey (Seed.getbytes ()); byte[] result = Encrypt (Rawkey , Cleartext.getbytes ()); return Tohex (Result); public static string decrypt (string seed, string encrypted) throws Exception {byte[] Rawkey = Getrawkey (Seed.getbytes ()); b yte[] enc = tobyte (encrypted); byte[] result = Decrypt (rawkey, ENC); return new String (result); private static byte[] Getrawkey (byte[] seed) throws Exception {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 (); return raw;} Private StAtic byte[] Encrypt (byte[] raw, byte[] clear) throws Exception {Secretkeyspec Skeyspec = new Secretkeyspec (Raw, "AES"); Cipher Cipher = cipher.getinstance ("AES"); Cipher.init (Cipher.encrypt_mode, Skeyspec); byte[] encrypted = Cipher.dofinal (clear); return encrypted;} private static byte[] Decrypt (byte[] raw, byte[] encrypted) throws Exception {Secretkeyspec Skeyspec = new Secretkeyspec (RA W, "AES"); Cipher Cipher = cipher.getinstance ("AES"); Cipher.init (Cipher.decrypt_mode, Skeyspec); byte[] decrypted = Cipher.dofinal (encrypted); return decrypted;} public static string Tohex (String txt) {return Tohex (Txt.getbytes ());} public static string Fromhex (String hex) {return new string (ToByte (hex));} public static byte[] ToByte (String hexstring) {int len = Hexstring.length ()/2;byte[] result = new Byte[len];for (int i = 0 ; i < Len; i++) Result[i] = integer.valueof (hexstring.substring (2*i, 2*i+2), +). Bytevalue (); return result;} public static String Tohex (byte[] buf) {if (buf = = null) return ""; StringBuffer RESult = new StringBuffer (2*buf.length); for (int i = 0; i < buf.length; i++) {Appendhex (result, buf[i]);} return result.tostring ();} Private final static String HEX = "0123456789ABCDEF";p rivate static void Appendhex (StringBuffer sb, byte b) {Sb.append (HE X.charat ((b>>4) &0x0f)). Append (Hex.charat (b&0x0f));}}

DES encryption algorithm
Import Java.io.unsupportedencodingexception;import Java.security.key;import Java.util.locale;import Javax.crypto.cipher;import Javax.crypto.secretkeyfactory;import Javax.crypto.spec.desedekeyspec;import javax.crypto.spec.ivparameterspec;/** * DES encryption * * @author Houjinyun * */public class Desutils {private static final Stri ng KEY = "********************";/** * DES3 Encryption of Strings * * @param str * @return encrypted string, null */public static string if failed enc Ode (String str) {try {byte[] key = Key.getbytes ("UTF-8"); byte[] data = str.getbytes ("UTF-8"); byte[] Encodeddata = Des3enco DEECB (key, data); return byte2hexstring (Encodeddata);} catch (Unsupportedencodingexception e) {e.printstacktrace ();} catch (Exception e) {e.printstacktrace ();} return null;} public static string decode (String str) {try {byte[] key = Key.getbytes ("UTF-8"); byte[] data = Hexstring2byte (str); byte[] Decodeddata = DES3DECODEECB (key, data); return new String (Decodeddata, "UTF-8");} catch (Exception e) {e.printstacktrace ();} return null;} /** * ECB encryption, do not IV * * @param key * key * @param data * plaintext * @return Base64 encoded ciphertext * @throws Exception */PU Blic static byte[] DES3ENCODEECB (byte[] key, byte[] data) throws Exception {key Deskey = Null;desedekeyspec Spec = new Dese Dekeyspec (key); Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("Desede");d Eskey = Keyfactory.generatesecret (spec); Cipher Cipher = cipher.getinstance ("Desede" + "/ecb/pkcs5padding"); Cipher.init (Cipher.encrypt_mode, Deskey); byte[] BOut = cipher.dofinal (data); return bOut;} /** * ECB decryption, do not IV * * @param key * key * @param data * Base64 encoded ciphertext * @return plaintext * @throws Exception * /public Static byte[] EES3DECODEECB (byte[] key, byte[] data) throws Exception {key Deskey = Null;desedekeyspec Spec = new D Esedekeyspec (key); Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("Desede");d Eskey = Keyfactory.generatesecret (spec); Cipher Cipher = cipher.getinstance ("Desede" + "/ecb/pkcs5padding"); Cipher.init (Cipher.decrypt_mode, deskey); byte[] BOut = cipher.dofinal (data); return bOut;} /** * CBC Encryption * * @param key * key * @param keyiv * IV * @param data * Clear text * @return Base64 Code ciphertext * @throws Exception */public static byte[] DES3ENCODECBC (byte[] key, byte[] Keyiv, byte[] data) throws Exception {key Deskey = Null;desedekeyspec spec = new Desedekeyspec (key); Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("Desede");d Eskey = Keyfactory.generatesecret (spec); Cipher Cipher = cipher.getinstance ("Desede" + "/cbc/pkcs5padding"); ivparameterspec ips = new Ivparameterspec (KEYIV); Cipher.init (Cipher.encrypt_mode, Deskey, IPs); byte[] BOut = cipher.dofinal (data); return bOut;} /** * CBC Decryption * * @param key * key * @param keyiv * IV * @param data * Base64 encoded ciphertext * @retur N PlainText * @throws Exception */public static byte[] DES3DECODECBC (byte[] key, byte[] Keyiv, byte[] data) throws Exception {key Deskey = Null;desedekeyspec spec = new Desedekeyspec (key); Secretkeyfactory KEYFACTory = Secretkeyfactory.getinstance ("Desede");d Eskey = Keyfactory.generatesecret (spec); Cipher Cipher = cipher.getinstance ("Desede" + "/cbc/pkcs5padding"); ivparameterspec ips = new Ivparameterspec (KEYIV); Cipher.init (Cipher.decrypt_mode, Deskey, IPs); byte[] BOut = cipher.dofinal (data); return bOut;} public static byte[] DES3DECODEECB (byte[] key, byte[] data) throws Exception {key Deskey = Null;desedekeyspec Spec = new DE Sedekeyspec (key); Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("Desede");d Eskey = Keyfactory.generatesecret (spec); Cipher Cipher = cipher.getinstance ("Desede" + "/ecb/pkcs5padding"); Cipher.init (Cipher.decrypt_mode, Deskey); byte[] BOut = cipher.dofinal (data); return bOut;} private static String byte2hexstring (byte[] b) {String a = ""; for (int i = 0; i < b.length; i++) {String hex = integer.tohexstring (B[i] & 0xFF). toUpperCase (locale.us); if (hex.length () = = 1) {hex = ' 0 ' + hex;} A = a + hex;} return A;} private static byte[] Hexstring2byte (String str) {if (str = = NULL) return NULL;STR = Str.trim (); int len = Str.length (); if (len = = 0 | | len% 2 = 1) return null;byte[] B = new Byte[len /2];try {for (int i = 0; i < str.length (); i + = 2) {B[I/2] = (byte) integer.decode ("0x" + str.substring (i, i + 2)). Intvalue ();} return b;} catch (Exception e) {return null;}}}



Aes,des encryption and decryption on Android platform

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.