Files
Knowledge/sunyard/PKCS11 JCE.md
T

58 lines
1.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
sun.security.pkcs11.wrapper.PKCS11Constants 内包含 CKM 的定义,例如:CKM_MD5
```
// 使用指定的算法和提供者创建 HMAC 实例
Mac mac = null;
try {
// 方案1 生成密钥
// // 1. 创建 KeyGenerator 实例,指定 HMAC 算法(如 HmacSHA256)
// KeyGenerator keyGen = KeyGenerator.getInstance("HmacSM3", this.pkcs11Provider);
// // 2. 初始化密钥生成器(可选:指定密钥长度)
// // - 如果省略初始化,JCE 会根据算法自动选择安全长度
// keyGen.init(128); // 16 字节
// // 3. 生成密钥
// SecretKey key = keyGen.generateKey();
// mac = Mac.getInstance(algorithm, this.pkcs11Provider);
// 方案2 使用明文密钥
// 使用提供的密钥初始化 HMAC SecretKeySpec key = new SecretKeySpec(secretKey2, algorithm);
mac.init(key);
// 执行 HMAC 计算,返回结果
return mac.doFinal(data);
} catch (Exception e) {
e.printStackTrace();
}
```
## 常见问题
no such algorithm: SM4MAC for provider SunyardPKCS11-HSM
## 测试
6 -1 随机数
1 SM3 -1
1 SHA-256 -1
1 SHA-384 -1
1 SHA-512 -1
2 HMAC-SM3 -1
2 SHA-256 -1
2 SHA-384 -1
2 SHA-512 -1
3 SM4 -1 MAC
3 3DES -1
3 AES -1
4 SM2 1 -1
4 SM2 2 -1
4 RSA 1 -1
4 RSA 2 -1
5 SM4 ECB -1
5 SM4 CBC -1
5 3DES ECB -1
5 3DES CBC -1
5 AES ECB -1
5 AES CBC -1