生活随笔
收集整理的這篇文章主要介紹了
Java 加密 AES 对称加密算法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
版權聲明:本文為博主原創文章,未經博主允許不得轉載。
【AES】
一種對稱加密算法,DES的取代者。
加密相關文章見:Java 加密解密 對稱加密算法 非對稱加密算法 MD5 BASE64 AES RSA
【代碼】
代碼比較多,有一部分非本文章內容代碼,具體自己看吧。
[java] view plaincopy
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;????public?class?QEncodeUtil?{????????????public?static?void?main(String[]?args)?throws?Exception?{??????????String?content?=?"我愛你";??????????System.out.println("加密前:"?+?content);????????????String?key?=?"123456";??????????System.out.println("加密密鑰和解密密鑰:"?+?key);????????????????????String?encrypt?=?aesEncrypt(content,?key);??????????System.out.println("加密后:"?+?encrypt);????????????????????String?decrypt?=?aesDecrypt(encrypt,?key);??????????System.out.println("解密后:"?+?decrypt);??????}????????????????public?static?String?binary(byte[]?bytes,?int?radix){??????????return?new?BigInteger(1,?bytes).toString(radix);????}????????????????public?static?String?base64Encode(byte[]?bytes){??????????return?new?BASE64Encoder().encode(bytes);??????}????????????????public?static?byte[]?base64Decode(String?base64Code)?throws?Exception{??????????return?QStringUtil.isEmpty(base64Code)???null?:?new?BASE64Decoder().decodeBuffer(base64Code);??????}????????????????public?static?byte[]?md5(byte[]?bytes)?throws?Exception?{??????????MessageDigest?md?=?MessageDigest.getInstance("MD5");??????????md.update(bytes);????????????????????return?md.digest();??????}????????????????public?static?byte[]?md5(String?msg)?throws?Exception?{??????????return?QStringUtil.isEmpty(msg)???null?:?md5(msg.getBytes());??????}????????????????public?static?String?md5Encrypt(String?msg)?throws?Exception{??????????return?QStringUtil.isEmpty(msg)???null?:?base64Encode(md5(msg));??????}????????????????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.ENCRYPT_MODE,?new?SecretKeySpec(kgen.generateKey().getEncoded(),?"AES"));????????????????????return?cipher.doFinal(content.getBytes("utf-8"));??????}????????????????public?static?String?aesEncrypt(String?content,?String?encryptKey)?throws?Exception?{??????????return?base64Encode(aesEncryptToBytes(content,?encryptKey));??????}????????????????public?static?String?aesDecryptByBytes(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(kgen.generateKey().getEncoded(),?"AES"));??????????byte[]?decryptBytes?=?cipher.doFinal(encryptBytes);????????????????????return?new?String(decryptBytes);??????}????????????????public?static?String?aesDecrypt(String?encryptStr,?String?decryptKey)?throws?Exception?{??????????return?QStringUtil.isEmpty(encryptStr)???null?:?aesDecryptByBytes(base64Decode(encryptStr),?decryptKey);??????}????????}??
【輸出】
[java] view plaincopy
加密前:我愛你??加密密鑰和解密密鑰:123456??加密后:A63fa7DjAe3yYji44BTm1g==??解密后:我愛你?
轉載于:https://www.cnblogs.com/Free-Thinker/p/5824641.html
總結
以上是生活随笔為你收集整理的Java 加密 AES 对称加密算法的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。