Android AES Encryption

Source: Internet
Author: User

Import Javax.crypto.Cipher;
Import Javax.crypto.KeyGenerator;
Import Javax.crypto.SecretKey;
Import Javax.crypto.spec.IvParameterSpec;
Import Javax.crypto.spec.SecretKeySpec;

/**
* @ Effect:
* Modify Person:
* @ Date:
*
*/
public class Aesutils {
public static final String key = "1123131313131313131313131313131313";
/**
* Encryption
*
* @param cleartext
* @return
*/
public static string Encrypt (string cleartext) {
byte[] result = NULL;
try {
byte[] Rawkey = Getrawkey (Key.getbytes ());
result = Encrypt (Rawkey, cleartext.getbytes ());
} catch (Exception e) {
E.printstacktrace ();
}
String content = Tohex (result);
return content;

}

/**
* Decryption
*
* @param encrypted
* @return
*/
public static string decrypt (string encrypted) {
Byte[] Rawkey;
try {
Rawkey = Getrawkey (Key.getbytes ());
byte[] enc = tobyte (encrypted);
Byte[] result = Decrypt (rawkey, ENC);
String coentn = new string (result);
return COENTN;
} catch (Exception e) {
E.printstacktrace ();
return null;
}
}

/**
* @param seed
* @return
* @throws Exception "rsa/none/pkcs1padding"
*/
private static byte[] Getrawkey (byte[] seed) throws Exception {
Keygenerator KGen = keygenerator.getinstance ("AES");
SecureRandom sr = null;
if (Build.VERSION.SDK_INT > 17) {
Cryptographic random number generator
sr = securerandom.getinstance ("Sha1prng", "Crypto");//android4.22 need this.
} else {
sr = securerandom.getinstance ("sha1prng"); Original
}
Kgen.init (+, SR);
Secretkey 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 Cipher = cipher.getinstance ("aes/cbc/pkcs5padding");
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 (Raw, "AES");
Cipher Cipher = cipher.getinstance ("AES");
Cipher Cipher = cipher.getinstance ("aes/cbc/pkcs5padding");
Cipher.init (Cipher.decrypt_mode, Skeyspec, New Ivparameterspec (
New Byte[cipher.getblocksize ()));
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 static void Appendhex (StringBuffer sb, byte b) {
Final String HEX = "0123456789ABCDEF";
Sb.append (Hex.charat ((b >> 4) & 0x0f)). Append (Hex.charat (b & 0x0f));
}
}

Android AES Encryption

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.