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

Class ToolDES

security/src/main/java/pro/tools/security/ToolDES.java:22–146  ·  view source on GitHub ↗

DES安全编码组件 @author SeanDragon

Source from the content-addressed store, hash-verified

20 * @author SeanDragon
21 */
22public final class ToolDES {
23
24 private ToolDES() {
25 throw new UnsupportedOperationException("我是工具类,别初始化我。。。");
26 }
27
28 /**
29 * 密钥算法 <br>
30 * Java 6 只支持56bit密钥 <br>
31 * Bouncy Castle 支持64bit密钥
32 */
33 public static final String KEY_ALGORITHM = "DES";
34
35 /**
36 * 加密/解密算法 / 工作模式 / 填充方式
37 */
38 public static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5PADDING";
39
40 /**
41 * 转换密钥
42 *
43 * @param key
44 * 二进制密钥
45 *
46 * @return Key 密钥
47 *
48 * @throws Exception
49 */
50 private static Key toKey(byte[] key) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
51
52 // 实例化DES密钥材料
53 DESKeySpec dks = new DESKeySpec(key);
54
55 // 实例化秘密密钥工厂
56 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
57
58 // 生成秘密密钥
59 SecretKey secretKey = keyFactory.generateSecret(dks);
60
61 return secretKey;
62 }
63
64 /**
65 * 解密
66 *
67 * @param data
68 * 待解密数据
69 * @param key
70 * 密钥
71 *
72 * @return byte[] 解密数据
73 *
74 * @throws Exception
75 */
76 public static byte[] decrypt(byte[] data, byte[] key) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException, NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException {
77
78 // 还原密钥
79 Key k = toKey(key);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected