Java中AES加密解密以及簽名校驗

import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; 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; import org.apache.commons.codec.digest.DigestUtils; /** * Created by Lb on 2017/9/10. */ public class AESCoder { //密鑰算法 public static final String KEY_ALGORITHM = "AES"; //工作模式,填充模式 public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding"; /** * 轉換密鑰 * * @param key 二進制密鑰 * @return Key密鑰 */ private static Key toKey(byte[] key) { SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM); return secretKey; } /** * 解密 */ public static byte[] decrpyt(byte[] data, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { //還原密鑰 Key k = toKey(key); //實例化 Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); //初始化 cipher.init(Cipher.DECRYPT_MODE, k); //執行操作 return cipher.doFinal(data); } /** * 加密 */ public static byte[] encrypt(byte[] data, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { //還原密鑰 Key k = toKey(key); //實例化 Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); //初始化 cipher.init(Cipher.ENCRYPT_MODE, k); //執行操作 return cipher.doFinal(data); } /** * 生成密鑰 */ public static byte[] initKey() throws NoSuchAlgorithmException { //實例化 KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM); //AES 要求密鑰長度爲128位、192位或256位 keyGenerator.init(128); //生成祕密密鑰 SecretKey secretKey = keyGenerator.generateKey(); //獲取密鑰的二進制編碼形式 return secretKey.getEncoded(); } /** * 初始化密鑰 * * @return Base64編碼密鑰 */ public static String initKeyString() throws NoSuchAlgorithmException { return Base64.encodeBase64String(initKey()); } /** * 獲取密鑰 */ public static byte[] getKey(String key) { return Base64.decodeBase64(key); } /** * 解密 * * @param data 待解密數據 * @param key 密鑰 * @return byte[] 解密數據 */ public static byte[] decrypt(byte[] data, String key) throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException { return decrpyt(data, getKey(key)); } /** * 加密 * * @param data 待加密數據 * @param key 密鑰 * @return byte[]加密數據 */ public static byte[] encrypt(byte[] data, String key) throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException { return encrypt(data, getKey(key)); } /** * 摘要處理 * * @param data 待摘要數據 * @return 摘要字符串 */ public static String shaHex(byte[] data) { return DigestUtils.md5Hex(data); } /** * 驗證 * * @param data 待摘要數據 * @param messageDigest 摘要字符串 * @return 驗證結果 */ public static boolean vailidate(byte[] data, String messageDigest) { return messageDigest.equals(shaHex(data)); } public static void main(String[] args) throws NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException { String inputStr = "AES18803696960http://www.baidu;com"; byte[] inputData = inputStr.getBytes(); System.out.println("原文:" + inputStr); //初始化密鑰 byte[] key = AESCoder.initKey(); System.out.println("密鑰:" + Base64.encodeBase64String(key)); //加密 inputData = AESCoder.encrypt(inputData, key); System.out.println("加密後:" + Base64.encodeBase64String(inputData)); //解密 byte[] outputData = AESCoder.decrpyt(inputData, key); String outputStr = new String(outputData); System.out.println("解密後:" + outputStr); System.out.println("校驗簽名:" + AESCoder.vailidate(inputData, AESCoder.shaHex(inputData))); } }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章