Java encryption and decryption-AES symmetric encryption algorithm

Source: Internet
Author: User
Package COM. jadyer. util. codec; import Java. security. key; import javax. crypto. cipher; import javax. crypto. keygenerator; import javax. crypto. secretkey; import javax. crypto. spec. secretkeyspec; import Org. apache. commons. codec. binary. base64; /*** AES symmetric encryption algorithm * @ see ============================== ========================================================== ================================================================= *@ see demonstrates the implementation of java6.0, bouncycast Of course Le also supports the AES symmetric encryption algorithm * @ see. In addition, we can use the AES algorithm implementation as a reference to complete RC2, implementation of RC4 and blowfish algorithms * @ see ================================ ========================================================== ================================================================= * @ see because des is insecure and desede is inefficient, as a result, the AES algorithm (Advanced Encryption Standard) * @ see is faster than DES, with high security, short key creation time, high sensitivity, and low memory requirements, widely used in various fields * @ see currently, AES algorithms are usually used in mobile communication systems and security shells of some software, some wireless routers also use the AES algorithm to construct the encryption protocol * @ see ========================== = ========================================================== ========================================================== * @ See because java6.0 supports most algorithms, however, due to exit restrictions, the key length cannot meet the requirements * @ see. Therefore, note that if you use a 256-bit key, therefore, there is no policy restriction file (unlimited strength jurisdiction policy files) * @ see. However, Sun restricts the permission file local_poblicy.jar and us_export_policy.jar. We can download the replacement file on the sun official website, remove restrictions * @ see URL for http://www.oracle.com/technetwork/java/javase/downloads/index.html * @ see find Java at the bottom of the page Cryptography Extension (JCE) unlimited strength jurisdiction policy files 6, click to download * @ see http://download.oracle.com/otn-pub/java/jce_policy/6/jce_policy-6.zip * @ see http://download.oracle.com/otn-pub/java/jce/7/UnlimitedJCEPolicyJDK7.zip * @ see and then overwrite the files in the security directory under the Local JDK directory and JRE directory * @ see ======== ========================================================== ========================================================== ===============================* @ s EE for more information about AES, you can refer to this blog http://blog.csdn.net/kongqz/article/category/800296 * @ create Jul 17,201 2 6:35:36 * @ author Xuan Yu (http://blog.csdn/net/jadyer) */public class aescodec {// key algorithm public static final string key_algorithm = "AES"; // encryption/decryption algorithm/working mode/filling mode. java6.0 supports the pkcs5padding filling mode, bouncycastle supports the pkcs7padding filling mode public static final string cipher_algorithm = "AEs/ECB/pkcs5padding";/*** generate key */Public stat IC string initkey () throws exception {keygenerator kg = keygenerator. getinstance (key_algorithm); // instantiate the key generator kg. init (128); // initialize the key generator: AES requires that the key length be 128,192,256-bit secretkey = kg. generatekey (); // generate the key return base64.encodebase64string (secretkey. getencoded (); // obtain the binary key encoding form}/*** convert key */public static key Tokey (byte [] Key) throws exception {return New secretkeyspec (key, key_algorithm);}/*** encrypt data ** @ par Am data to be encrypted * @ Param key * @ return encrypted data */public static string encrypt (string data, string key) throws exception {key K = Tokey (base64.decodebase64 (key); // restore the key // use the pkcs7padding filling method, so you have to write it here (that is, call the bouncycastle component) // cipher = cipher. getinstance (cipher_algorithm, "BC"); cipher = cipher. getinstance (cipher_algorithm); // instantiate the cipher object, which is used to complete the actual encryption operation cipher. init (cipher. encrypt_mode, k); // initial Initialize the cipher object and set it to the encryption mode return base64.encodebase64string (Cipher. dofinal (data. getbytes (); // perform the encryption operation. Encrypted results are usually transmitted in base64 encoding.}/***** decrypt data * @ Param data to be decrypted data * @ Param key * @ return decrypted data **/ public static string decrypt (string data, string key) throws exception {key K = Tokey (base64.decodebase64 (key); cipher = cipher. getinstance (cipher_algorithm); cipher. init (cipher. decrypt_mode, k); // initialize the cipher object and set it to the decryption mode return new string (cipher. dofinal (base64.decodebase64 (data); // execute the decryption operation} public static Vo Id main (string [] ARGs) throws exception {string source = "standing on the cloud, knocking on the keyboard and looking at the window leading to the other end of the world, only for those who read 0 and 1 .. "; System. out. println ("Original:" + source); string key = initkey (); system. out. println ("key:" + key); string encryptdata = encrypt (source, key); system. out. println ("encryption:" + encryptdata); string decryptdata = decrypt (encryptdata, key); system. out. println ("decryption:" + decryptdata );}}

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.