AES encryption and decryption,

Source: Internet
Author: User

AES encryption and decryption,

AES is a type of symmetric encryption. It is simply understood that there is only one key, and it is used for encryption and decryption. The security is not very good.

Package com. aisino. qysds. common. util; import java. io. unsupportedEncodingException; import java. security. invalidKeyException; import java. security. noSuchAlgorithmException; import java. security. secureRandom; import java. util. random; import javax. crypto. badPaddingException; import javax. crypto. cipher; import javax. crypto. illegalBlockSizeException; import javax. crypto. keyGenerator; import javax. crypto. noSuchPaddingException; import javax. crypto. secretKey; import javax. crypto. spec. secretKeySpec; import org. apache. commons. codec. binary. base64; public class AESUtil {private static final String charset = "UTF-8"; private final static int length = 128; private final static String base = "abcdefghijklmnopqrstuvwxyz0123456789 "; private static final String KEY_AES = "AES";/*** aes ECB mode encryption * @ param encryptStr * @ param decryptKey * @ return */public static String encryptECB (String encryptStr, string decryptKey) {try {return parseByte2HexStr (encrypt_ECB (encryptStr, decryptKey);} catch (Exception e) {return null ;}} /*** aes ECB mode decryption * @ param encryptStr * @ param decryptKey * @ return * @ throws Exception */public static String decryptECB (String encryptStr, String decryptKey) throws Exception {return decrypt_ECB (parseHexStr2Byte (encryptStr), decryptKey );} /*** encrypt ECB * @ param content * content to be encrypted * @ param password * encryption password * @ return */private static byte [] encrypt_ECB (String content, String encryptKey) throws Exception {try {Cipher aesECB = Cipher. getInstance ("AES/ECB/PKCS5Padding"); SecretKeySpec key = new SecretKeySpec (encryptKey. getBytes (), KEY_AES); aesECB. init (Cipher. ENCRYPT_MODE, key); byte [] result = aesECB. doFinal (content. getBytes (charset); 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 ();} catch (UnsupportedEncodingException e) {e. printStackTrace ();} return null ;} /*** decrypt ECB * @ param content to be decrypted * @ param password decryption key * @ return */private static String decrypt_ECB (byte [] encryptBytes, String decryptKey) throws Exception {try {Cipher cipher = Cipher. getInstance ("AES/ECB/PKCS5Padding"); // create secret SecretKeySpec key = new SecretKeySpec (decryptKey. getBytes (), KEY_AES); cipher. init (Cipher. DECRYPT_MODE, key); // initialization // byte [] result = parseHexStr2Byte (content); return new String (cipher. doFinal (encryptBytes); // decryption} catch (InvalidKeyException e) {e. printStackTrace ();} catch (NoSuchAlgorithmException e) {e. printStackTrace ();} catch (NoSuchPaddingException e) {e. printStackTrace ();} catch (IllegalBlockSizeException e) {e. printStackTrace ();} catch (BadPaddingException e) {e. printStackTrace ();} return null ;} /*** aes default encryption * @ param content * content to be encrypted * @ param password * encryption password * @ return */private static byte [] encrypt (String content, string password) throws Exception {KeyGenerator kgen = KeyGenerator. getInstance (KEY_AES); SecureRandom secureRandom = SecureRandom. getInstance ("SHA1PRNG"); secureRandom. setSeed (password. getBytes (); kgen. init (length, secureRandom); SecretKey secretKey = kgen. generateKey (); byte [] enCodeFormat = secretKey. getEncoded (); SecretKeySpec key = new SecretKeySpec (enCodeFormat, KEY_AES); Cipher cipher = Cipher. getInstance (KEY_AES); // create a secret byte [] byteContent = content. getBytes (charset); cipher. init (Cipher. ENCRYPT_MODE, key); // initialize byte [] result = cipher. doFinal (byteContent); return result; // encryption}/*** aes default decryption * @ param content to be decrypted * @ param password decryption key * @ return */private static byte [] decrypt (byte [] content, string password) throws Exception {KeyGenerator kgen = KeyGenerator. getInstance (KEY_AES); SecureRandom secureRandom = SecureRandom. getInstance ("SHA1PRNG"); secureRandom. setSeed (password. getBytes (); kgen. init (length, secureRandom); SecretKey secretKey = kgen. generateKey (); byte [] enCodeFormat = secretKey. getEncoded (); SecretKeySpec key = new SecretKeySpec (enCodeFormat, KEY_AES); Cipher cipher = Cipher. getInstance (KEY_AES); // create a cipher. init (Cipher. DECRYPT_MODE, key); // initialize byte [] result = cipher. doFinal (content); return result; // encryption}/*** aes default encryption * @ param content * @ param password * @ return */public static String encryptAES (String content, string password) {try {byte [] encryptResult = encrypt (content, password); return encode (encryptResult);} catch (Exception e) {return null ;}} /*** aes default decryption * @ param content * @ param password * @ return */public static String decryptAES (String content, String password) {try {byte [] decryptResult = decrypt (decode (content. getBytes (), password); return new String (decryptResult, charset);} catch (Exception e) {return null ;}} /*** obtain the Random number * @ param len * @ return */public static String randomStr (int len) {random Random = new Random (); StringBuffer sb = new StringBuffer (); for (int I = 0; I <len; I ++) {int number = random. nextInt (base. length (); sb. append (base. charAt (number);} return sb. toString ();}/** convert binary to hexadecimal form * @ param buf * @ return */public 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 ();}/** convert hexadecimal to binary * @ param buf * @ return */public static byte [] parseHexStr2Byte (String hexStr) {if (hexStr. length () <1) return null; byte [] result = new byte [hexStr. length ()/2]; for (int I = 0; I 

Result output

Before encryption: 123115428541F01951FF4F0A2990535F2C81 after decryption: 1231

 

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.