diff --git a/src/encryptionAlgorithm/AES.java b/src/encryptionAlgorithm/AES.java index 98a8ffc105b6b68e1ec05367b87335d69ec08320..79c66e2f7004f02db0a773c1077c0b1b806ae8f5 100644 --- a/src/encryptionAlgorithm/AES.java +++ b/src/encryptionAlgorithm/AES.java @@ -18,20 +18,20 @@ import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.binary.Hex; public class AES { - public static final String ALGORITHM_AES = "AES"; //AES㷨 + public static final String ALGORITHM_AES = "AES"; //AES算法 /** - * Ժ + * 这是一个测试函数 * @param args */ public static void main(String[] args) { - String data = "Ҫܵhahahah"; + String data = "我是要加密的内容hahahah"; String secretKey; try { secretKey = generateKey(); byte[] encryptResult = encryptByAES(data, secretKey); - System.out.println("ܺĽΪ" + new String(encryptResult,"UTF-8")); + System.out.println("加密后的结果为:" + new String(encryptResult,"UTF-8")); String decryptResult = decryptByAES(encryptResult,secretKey); - System.out.println("ܺĽΪ" + decryptResult); + System.out.println("解密后的结果为:" + decryptResult); } catch (Exception e) { e.printStackTrace(); @@ -39,7 +39,7 @@ public class AES { } /** - * Կ + * 生成密钥 * @return * @throws NoSuchAlgorithmException */ @@ -52,15 +52,15 @@ public class AES { }*/ public static String generateKey() { try { - // DESķʽʼKey + // 以DES的方式初始化Key生成器 KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM_AES); - // ԿijΪ128λ + // 设置密钥的长度为128位 keyGenerator.init(128); - // һKey + // 生成一个Key SecretKey generateKey = keyGenerator.generateKey(); - // תΪֽ + // 转变为字节数组 byte[] encoded = generateKey.getEncoded(); - // Կַ + // 生成密钥字符串 String key = Hex.encodeHexString(encoded); return key; } catch (NoSuchAlgorithmException e) { @@ -71,9 +71,9 @@ public class AES { } /** - * AES - * @param content // - * @param secretKey //Կ + * AES加密 + * @param content //加密内容 + * @param secretKey //密钥 * @return * @throws InvalidKeyException * @throws NoSuchAlgorithmException @@ -84,17 +84,17 @@ public class AES { * @throws DecoderException */ public static byte[] encryptByAES(String data, String key) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, DecoderException{ - // ַkeyתΪֽ飬һʹã֤ + // 把字符串key转变为字节数组,可以用于另一方使用,验证 byte[] decodeHex = Hex.decodeHex(key.toCharArray()); - // Կ + // 生成密钥对象 SecretKeySpec secretKey = new SecretKeySpec(decodeHex, ALGORITHM_AES); return aes(data.getBytes("UTF-8"),Cipher.ENCRYPT_MODE,secretKey); } /** - * AES - * @param contentArray // - * @param secretKey //Կ + * AES解密 + * @param contentArray //解密内容 + * @param secretKey //密钥 * @return * @throws UnsupportedEncodingException * @throws InvalidKeyException @@ -105,9 +105,9 @@ public class AES { * @throws DecoderException */ public static String decryptByAES(byte[] data, String key) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, DecoderException{ - // ַkeyתΪֽ飬һʹã֤ + // 把字符串key转变为字节数组,可以用于另一方使用,验证 byte[] decodeHex = Hex.decodeHex(key.toCharArray()); - // Կ + // 生成密钥对象 SecretKeySpec secretKey = new SecretKeySpec(decodeHex, ALGORITHM_AES); byte[] result = aes(data,Cipher.DECRYPT_MODE,secretKey); return new String(result,"UTF-8"); @@ -115,7 +115,7 @@ public class AES { } /** - * AES㷨 + * AES算法主体 * @param contentArray * @param mode * @param secretKey