AES Encryption Example

Source: Internet
Author: User

Recently used to encrypt the text content, and then looked at the usual encryption algorithm:

DES (Data Encryption Standard): symmetric algorithm, data encryption standards, fast, suitable for encrypting large amounts of data;
3DES (Triple des): is based on des symmetric algorithm, a piece of data with three different keys for three times encryption, higher intensity;
RC2 and RC4: symmetric algorithm, using variable-length key to encrypt large amounts of data, faster than DES;
IDEA (International Data encryption algorithm) International data encryption algorithm, using 128-bit key to provide very strong security;
RSA: Invented by RSA, is a public key algorithm that supports variable-length key, and the length of the file block that needs to be encrypted is also variable, asymmetric algorithm;
DSA (Digitally Signature algorithm): Digital Signature Algorithm, is a standard DSS (digital signature standard), strictly speaking, is not a cryptographic algorithm;
AES (Advanced encryption): High Encryption Standard, symmetric algorithm, is the next generation of cryptographic algorithm standard, fast, high security level, the current implementation of the AES standard is the Rijndael algorithm;
BLOWFISH, which uses a variable-length key, can be up to 448 bits long and runs fast;
MD5: Strictly speaking is not a cryptographic algorithm, can only be said to be a digest algorithm.

The following is an example of a Java implementation of AES plus decryption:

Encryption:

/*
* Encrypt
* @param content:
* @param Password:
*/
private static byte[] Encrypt (string content, string password) {
try {
Keygenerator KGen = keygenerator.getinstance ("AES");
Kgen.init (+, New SecureRandom (Password.getbytes ()));
Secretkey Secretkey = Kgen.generatekey ();
byte[] Encodeformat = secretkey.getencoded ();
Secretkeyspec key = new Secretkeyspec (Encodeformat, "AES");
Cipher Cipher = cipher.getinstance ("AES");
byte[] bytecontent = content.getbytes ("Utf-8");
Cipher.init (Cipher.encrypt_mode, key);
Byte[] result = cipher.dofinal (bytecontent);
return result;
} catch (NoSuchAlgorithmException e) {
E.printstacktrace ();
} catch (Nosuchpaddingexception e) {
E.printstacktrace ();
} catch (InvalidKeyException e) {
E.printstacktrace ();
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
} catch (Illegalblocksizeexception e) {
E.printstacktrace ();
} catch (Badpaddingexception e) {
E.printstacktrace ();
}

return null;
}

Decrypt:
/*
* Decrypt
* @param content:
* @param Password:
*/
private static byte[] Decrypt (byte[] content, String password) {
try {
Keygenerator KGen = keygenerator.getinstance ("AES");
Kgen.init (+, New SecureRandom (Password.getbytes ()));
Secretkey Secretkey = Kgen.generatekey ();
byte[] Encodeformat = secretkey.getencoded ();
Secretkeyspec key = new Secretkeyspec (Encodeformat, "AES");
Cipher Cipher = cipher.getinstance ("AES");
Cipher.init (Cipher.decrypt_mode, key);
Byte[] result = cipher.dofinal (content);
return result;
} catch (NoSuchAlgorithmException e) {
E.printstacktrace ();
} catch (Nosuchpaddingexception e) {
E.printstacktrace ();
} catch (InvalidKeyException e) {
E.printstacktrace ();
} catch (Illegalblocksizeexception e) {
E.printstacktrace ();
} catch (Badpaddingexception e) {
E.printstacktrace ();
}

return null;
}

Example:

String content = "Test";
String password = "12345678";
Encryption
System.out.println ("Before encryption:" + content);
byte[] Encryptresult = Encrypt (content, password);
Decrypt
byte[] Decryptresult = Decrypt (Encryptresult,password);
System.out.println ("decrypted:" + new String (Decryptresult));

If you want the hexadecimal display after encryption, you can add two functions, binary and 16 binary conversions
Binary conversion to 16 binary:

private static String Parsebyte2hexstr (byte buf[]) {
StringBuffer sb = new StringBuffer ();
for (int i = 0; i < buf.length; i++) {
String hex = integer.tohexstring (Buf[i] & 0xFF);
if (hex.length () = = 1) {
Hex = ' 0 ' + hex;
}
Sb.append (Hex.touppercase ());
}
return sb.tostring ();
}


16 binary conversion to binary:
private static byte[] Parsehexstr2byte (String hexstr) {
if (Hexstr.length () < 1) {
return null;
}

Byte[] result = new Byte[hexstr.length ()/2];
for (int i = 0; i < hexstr.length ()/2; i++) {
int high = Integer.parseint (Hexstr.substring (i*2, i*2+1), 16);
int low = Integer.parseint (Hexstr.substring (i*2+1, i*2+2), 16);
Result[i] = (byte) (high * + low);
}
return result;
}

Example:

String content = "Test";
String password = "12345678";
Encryption
System.out.println ("Before encryption:" + content);
byte[] Encryptresult = Encrypt (content, password);
String encryptresultstr = Parsebyte2hexstr (Encryptresult);
SYSTEM.OUT.PRINTLN ("After encryption:" + ENCRYPTRESULTSTR);
Decrypt
byte[] Decryptfrom = Parsehexstr2byte (ENCRYPTRESULTSTR);
byte[] Decryptresult = Decrypt (Decryptfrom,password);
System.out.println ("decrypted:" + new String (Decryptresult));

AES Encryption Example

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.