Use the AES Encryption Algorithm in Java

Source: Internet
Author: User

AES is a Federation standard for private keys or symmetric encryption. Advanced Encryption Standard in cryptography
Encryption Standard, AES), also known as Rijndael Encryption algorithm. It supports a combination of keys and block sizes of 128,192 and 256.

 

Java Implementation of AES

Import javax. crypto. cipher; <br/> import javax. crypto. keyGenerator; <br/> import javax. crypto. secretKey; <br/> import javax. crypto. spec. secretKeySpec; <br/> public class AES {<br/>/** <br/> * convert a byte array to a hexadecimal string <br/> *@ param buf is converted to a hex byte array <br/> * @ return: The generated hexadecimal String <br/> */<br/> public static String asHex (byte buf []) {<br/> // buf. length * 2 byte array to hexadecimal format, multiply by 2 <br/> StringBuffer strbuf = new StringBuffer (buf. length * 2); <br/> int I; <br/> for (I = 0; I <buf. length; I ++) {<br/> if (int) buf [I] & 0xff) <0x10) <br/> strbuf. append ("0"); // If buf [I] & 0xff is less than 10, add 0 <br/> strbuf. append (Long. toString (int) buf [I] & 0xff, 16); <br/>}< br/> return strbuf. toString (); <br/>}< br/> public static void main (String [] args) throws Exception {<br/> String message = "This is just an example"; <br/> // obtain the key generator <br/> KeyGenerator kgen = KeyGenerator. getInstance ("AES"); <br/> kgen. init (128); // 192 and 256 bits may be unavailable <br/> // generate a key secret protocol <br/> SecretKey skey = kgen. generateKey (); <br/> byte [] raw = skey. getEncoded (); // encode the key into a byte array <br/> SecretKeySpec skeySpec = new SecretKeySpec (raw, "AES "); <br/> // initialize the password object <br/> Cipher cipher = Cipher. getInstance ("AES"); <br/> cipher. init (Cipher. ENCRYPT_MODE, skeySpec); // sets the password object to the encryption mode and sets the encryption key secret protocol <br/> // encrypts the string byte array through the password object, generate an encrypted byte array <br/> byte [] encrypted = cipher. doFinal (message. getBytes (); <br/> System. out. println ("encrypted string:" + asHex (encrypted); <br/> cipher. init (Cipher. DECRYPT_MODE, skeySpec); // set the password object to the decryption mode and set the decryption key secret protocol <br/> byte [] original = cipher. doFinal (encrypted); // decrypt the byte array by using the password object to generate a decryption byte array <br/> String originalString = new String (original ); // restore the original string <br/> System. out. println ("Original string:" + originalString + "" <br/> + asHex (original); <br/>}< br/> 

This is just a simple AES implementation that demonstrates how to use strong cryptographic technology for development. Other elastic and robust encryption technologies can be used in Java. You can visit the internet.

 

Link: http://java.sun.com/developer/technicalArticles/Security/AES/AES_v1.html

 

AES encryption algorithm home page: http://www.nist.gov/aes

 

Encryption Algorithm Introduction: http://news.csdn.net/n/20070425/103328.html

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.