MCPcopy Create free account
hub / github.com/SeanDragon/protools / ToolAES

Class ToolAES

security/src/main/java/pro/tools/security/ToolAES.java:32–147  ·  view source on GitHub ↗

AES安全编码组件 高级数据加密标准---AES:由于DES的问题所以产生了AES,像是DES的升级,密钥建立时间短,灵敏性好,内存要求低,被广泛应用 说明: 对于java.security.InvalidKeyException: Illegal key size or default parameters异常, 去掉这种限制需要下载Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files, 下载包的readme.txt 有安装说明。就是替换${JAVA_HOME}/j

Source from the content-addressed store, hash-verified

30 * @author SeanDragon
31 */
32public final class ToolAES {
33 private ToolAES() {
34 throw new UnsupportedOperationException("我是工具类,别初始化我。。。");
35 }
36
37 /**
38 * 密钥算法
39 */
40 public static final String KEY_ALGORITHM = "AES";
41 /**
42 * 加密/解密算法 / 工作模式 / 填充方式 Java 6支持PKCS5Padding填充方式 Bouncy
43 * Castle支持PKCS7Padding填充方式
44 */
45 public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
46 @SuppressWarnings("unused")
47 private static final Logger log = LoggerFactory.getLogger(ToolAES.class);
48
49 /**
50 * 转换密钥
51 *
52 * @param key
53 * 二进制密钥
54 *
55 * @return Key 密钥
56 *
57 * @throws Exception
58 */
59 private static Key toKey(byte[] key) {
60 // 实例化AES密钥材料
61 SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM);
62
63 return secretKey;
64 }
65
66 /**
67 * 解密
68 *
69 * @param data
70 * 待解密数据
71 * @param key
72 * 密钥
73 *
74 * @return byte[] 解密数据
75 *
76 * @throws Exception
77 */
78 public static byte[] decrypt(byte[] data, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
79 // 还原密钥
80 Key k = toKey(key);
81
82 /*
83 * 实例化 使用PKCS7Padding填充方式,按如下方式实现 Cipher.getInstance(CIPHER_ALGORITHM, "BC");
84 */
85 Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
86
87 // 初始化,设置为解密模式
88 cipher.init(Cipher.DECRYPT_MODE, k);
89

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected