Java encryption AES symmetric encryption algorithm

Source: Internet
Author: User
Tags asymmetric encryption

[AES]

A symmetric encryption algorithm, used by DES.

For more information about encryption, see Java encryption and decryption symmetric encryption algorithm asymmetric encryption algorithm MD5 BASE64 AES RSA


[Code]

There are many codes. Some of them are not the content code of this Article. Let's take a look at them for details.

Package com. uikoo9.util. encrypt; import java. math. bigInteger; import java. security. messageDigest; import java. security. secureRandom; import javax. crypto. cipher; import javax. crypto. keyGenerator; import javax. crypto. spec. secretKeySpec; import sun. misc. BASE64Decoder; import sun. misc. BASE64Encoder; import com. uikoo9.util. QStringUtil;/*** encoding tool class * 1. convert byte [] to a variety of hexadecimal strings * 2. base 64 encode * 3. base 64 decode * 4. obtain Md5 value of byte [] * 5. obtain the md5 value of the string * 6. use base64 to implement md5 encryption * 7. AES encryption * 8. AES encrypted to base 64 code * 9. AES decryption * 10. decrypt base 64 code AES * @ author uikoo9 * @ version 0.0.7.20140601 */public class QEncodeUtil {public static void main (String [] args) throws Exception {String content = "I Love You"; System. out. println ("before encryption:" + content); String key = "123456"; System. out. println ("encryption key and decryption key:" + key); String encrypt = aesEncrypt (content, Key); System. out. println ("encrypted:" + encrypt); String decrypt = aesDecrypt (encrypt, key); System. out. println ("decrypted:" + decrypt );} /*** convert byte [] into various hexadecimal strings * @ param bytes byte [] * @ param radix can be converted to the hexadecimal range from Character. MIN_RADIX to Character. MAX_RADIX, which goes out of the range and becomes a 10-digit * @ return converted String */public static String binary (byte [] bytes, int radix) {return new BigInteger (1, bytes ). toString (radix); // here 1 represents a positive number}/*** base 64 encode * @ Param bytes the byte [] * @ return encoded base 64 code */public static String base64Encode (byte [] bytes) {return new BASE64Encoder (). encode (bytes );} /*** base 64 decode * @ param base64Code base 64 code * @ return decoded byte [] * @ throws Exception */public static byte [] base64Decode (String base64Code) throws Exception {return QStringUtil. isEmpty (base64Code )? Null: new BASE64Decoder (). decodeBuffer (base64Code );} /*** get the md5 value of byte [] * @ param bytes byte [] * @ return md5 * @ throws Exception */public static byte [] md5 (byte [] bytes) throws Exception {MessageDigest md = MessageDigest. getInstance ("MD5"); md. update (bytes); return md. digest ();}/*** get String md5 value * @ param msg * @ return md5 * @ throws Exception */public static byte [] md5 (String msg) throws Exception {ret Urn QStringUtil. isEmpty (msg )? Null: md5 (msg. getBytes ());} /*** implement md5 encryption with base64 * @ param msg String to be encrypted * @ return convert md5 to base64 * @ throws Exception */public static String md5Encrypt (String msg) throws Exception {return QStringUtil. isEmpty (msg )? Null: base64Encode (md5 (msg ));} /*** AES encryption ** @ param content to be encrypted * @ param encryptKey encryption key * @ return encrypted byte [] * @ throws Exception */public static byte [] aesEncryptToBytes (String content, string encryptKey) throws Exception {KeyGenerator kgen = KeyGenerator. getInstance ("AES"); kgen. init (128, new SecureRandom (encryptKey. getBytes (); Cipher cipher = Cipher. getInstance ("AES"); cipher. init (Cipher. ENCR YPT_MODE, new SecretKeySpec (kgen. generateKey (). getEncoded (), "AES"); return cipher. doFinal (content. getBytes ("UTF-8 "));} /*** encrypted AES to base 64 code * @ param content to be encrypted * @ param encryptKey encryption key * @ return encrypted base 64 code * @ throws Exception */public static String aesEncrypt (String content, string encryptKey) throws Exception {return base64Encode (aesEncryptToBytes (content, encryptKey);}/*** AES decryption * @ Param encryptBytes byte [] * @ param decryptKey decryption key * @ return decrypted String * @ throws Exception */public static String aesdecryptbytes (byte [] encryptBytes, string decryptKey) throws Exception {KeyGenerator kgen = KeyGenerator. getInstance ("AES"); kgen. init (128, new SecureRandom (decryptKey. getBytes (); Cipher cipher = Cipher. getInstance ("AES"); cipher. init (Cipher. DECRYPT_MODE, new SecretKeySpec (k Gen. generateKey (). getEncoded (), "AES"); byte [] decryptBytes = cipher. doFinal (encryptBytes); return new String (decryptBytes );} /*** decrypt base 64 code AES * @ param encryptStr base 64 code to be decrypted * @ param decryptKey decryption key * @ return the decrypted string * @ throws Exception */public static String aesDecrypt (String encryptStr, string decryptKey) throws Exception {return QStringUtil. isEmpty (encryptStr )? Null: aesdecryptbytes (base64Decode (encryptStr), decryptKey );}}

[Output]

Before encryption: I love your encryption key and decryption key: 123456 after encryption: A63fa7DjAe3yYji44BTm1g = after decryption: I love you


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.