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

Class ToolElGamal

security/src/main/java/pro/tools/security/ToolElGamal.java:35–204  ·  view source on GitHub ↗

ElGamal安全编码组件 @author SeanDragon

Source from the content-addressed store, hash-verified

33 * @author SeanDragon
34 */
35public final class ToolElGamal {
36 private ToolElGamal() {
37 throw new UnsupportedOperationException("我是工具类,别初始化我。。。");
38 }
39
40 /**
41 * 非对称加密密钥算法
42 */
43 public static final String KEY_ALGORITHM = "ElGamal";
44
45 /**
46 * 密钥长度
47 * <p>
48 * ElGamal算法默认密钥长度为1024 密钥长度范围在160位至16,384位不等。
49 */
50 private static final int KEY_SIZE = 256;
51
52 /**
53 * 公钥
54 */
55 private static final String PUBLIC_KEY = "ElGamalPublicKey";
56
57 /**
58 * 私钥
59 */
60 private static final String PRIVATE_KEY = "ElGamalPrivateKey";
61
62 /**
63 * 用私钥解密
64 *
65 * @param data
66 * 待解密数据
67 * @param key
68 * 私钥
69 *
70 * @return byte[] 解密数据
71 *
72 * @throws Exception
73 */
74 public static byte[] decryptByPrivateKey(byte[] data, byte[] key) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
75 // 加入BouncyCastleProvider支持
76 Security.addProvider(new BouncyCastleProvider());
77
78 // 私钥材料转换
79 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
80
81 // 实例化密钥工厂
82 KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
83
84 // 生成私钥
85 Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
86
87 // 对数据解密
88 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
89
90 cipher.init(Cipher.DECRYPT_MODE, privateKey);
91
92 return cipher.doFinal(data);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected