Android AES Encryption tool class (measurement compatible with all versions, reliable)

Source: Internet
Author: User

Import Android.annotation.suppresslint;import Java.security.securerandom;import Javax.crypto.cipher;import Javax.crypto.keygenerator;import Javax.crypto.secretkey;import Javax.crypto.spec.ivparameterspec;import javax.crypto.spec.secretkeyspec;/** * * * Author:sunger */public class Aesutils {public static string encrypt (string see D, String cleartext) throws Exception {byte[] Rawkey = Getrawkey (Seed.getbytes ()); byte[] result = Encrypt (Rawkey, Cleartex T.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); @SuppressLint ("Trulyrandom") private static byte[] Getrawkey (byte[] seed) throws Exception {Keygenerator KGen = Keygenerator.getinstance ("AES"); securerandom sr = securerandom.getinstance ("Sha1prng", "Crypto"); Sr.setseed (seed); Kgen.init (SR); 192 and AvailablesecretkeySkey = 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, New Ivparameterspec (New byte[ Cipher.getblocksize ()]); 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, New Ivparameterspec (New byte[ Cipher.getblocksize ()])); byte[] decrypted = cipher.dofinal (encrypted); return decrypted;} private 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;} private 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 (HEX . CharAt ((b >> 4) & 0x0f). Append (Hex.charat (b & 0x0f));}}

Android AES Encryption tool class (measurement compatible with all versions, reliable)

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.