Java 加密 AES 對稱加密演算法
阿新 • • 發佈:2019-01-23
【AES】
一種對稱加密演算法,DES的取代者。
【程式碼】
程式碼比較多,有一部分非本文章內容程式碼,具體自己看吧。
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; /** * 編碼工具類 * 1.將byte[]轉為各種進位制的字串 * 2.base 64 encode * 3.base 64 decode * 4.獲取byte[]的md5值 * 5.獲取字串md5值 * 6.結合base64實現md5加密 * 7.AES加密 * 8.AES加密為base 64 code * 9.AES解密 * 10.將base 64 code AES解密 * @author uikoo9 * @version 0.0.7.20140601 */ 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); } /** * 將byte[]轉為各種進位制的字串 * @param bytes byte[] * @param radix 可以轉換進位制的範圍,從Character.MIN_RADIX到Character.MAX_RADIX,超出範圍後變為10進位制 * @return 轉換後的字串 */ public static String binary(byte[] bytes, int radix){ return new BigInteger(1, bytes).toString(radix);// 這裡的1代表正數 } /** * base 64 encode * @param bytes 待編碼的byte[] * @return 編碼後的base 64 code */ public static String base64Encode(byte[] bytes){ return new BASE64Encoder().encode(bytes); } /** * base 64 decode * @param base64Code 待解碼的base 64 code * @return 解碼後的byte[] * @throws Exception */ public static byte[] base64Decode(String base64Code) throws Exception{ return QStringUtil.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code); } /** * 獲取byte[]的md5值 * @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(); } /** * 獲取字串md5值 * @param msg * @return md5 * @throws Exception */ public static byte[] md5(String msg) throws Exception { return QStringUtil.isEmpty(msg) ? null : md5(msg.getBytes()); } /** * 結合base64實現md5加密 * @param msg 待加密字串 * @return 獲取md5後轉為base64 * @throws Exception */ public static String md5Encrypt(String msg) throws Exception{ return QStringUtil.isEmpty(msg) ? null : base64Encode(md5(msg)); } /** * AES加密 * @param content 待加密的內容 * @param encryptKey 加密金鑰 * @return 加密後的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.ENCRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES")); return cipher.doFinal(content.getBytes("utf-8")); } /** * AES加密為base 64 code * @param content 待加密的內容 * @param encryptKey 加密金鑰 * @return 加密後的base 64 code * @throws Exception */ public static String aesEncrypt(String content, String encryptKey) throws Exception { return base64Encode(aesEncryptToBytes(content, encryptKey)); } /** * AES解密 * @param encryptBytes 待解密的byte[] * @param decryptKey 解密金鑰 * @return 解密後的String * @throws Exception */ 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); } /** * 將base 64 code AES解密 * @param encryptStr 待解密的base 64 code * @param decryptKey 解密金鑰 * @return 解密後的string * @throws Exception */ public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception { return QStringUtil.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey); } }
【輸出】
加密前:我愛你
加密金鑰和解密金鑰:123456
加密後:A63fa7DjAe3yYji44BTm1g==
解密後:我愛你