【发布时间】:2018-05-13 06:27:22
【问题描述】:
我需要使用位于 Safenet HSM Luna SA 设备中的公钥加密数据,还需要使用也位于 JAVA 中的 HSM 设备中的私钥解密数据。
我对 HSM 设备完全陌生。我使用位于 epass 电子令牌设备中的密钥加密/解密数据,如下所示:
private void loadKeys() {
logger.info("In loadKeys method at "+new Date());
try {
char password[] = hsmServiceAppProps.getDigiSigPfxPassword().toCharArray();
Provider userProvider = new sun.security.pkcs11.SunPKCS11(this.getClass().getClassLoader().getResourceAsStream("/pkcs11.cfg"));
Security.addProvider(userProvider);
KeyStore ks = KeyStore.getInstance("PKCS11");
ks.load(null, password);
String alias = null;
/*X509Certificate userCert = null;
PrivateKey userCertPrivKey = null;
PublicKey userCertPubKey = null;
Enumeration<String> e = ks.aliases();
while (e.hasMoreElements()) {
alias = (String) e.nextElement();
logger.info("Alias of the e-Token : " + alias);
userCert = (X509Certificate) ks.getCertificate(alias);
userCertPubKey = (PublicKey) ks.getCertificate(alias).getPublicKey();
userCertPrivKey = (PrivateKey) ks.getKey(alias, password);
}*/
alias = "*************************************";
//X509Certificate certificate = (X509Certificate) ks.getCertificate(alias);
publicKey = (PublicKey) ks.getCertificate(alias).getPublicKey();
privateKey = (PrivateKey) ks.getKey(alias, password);
} catch (Exception e) {
logger.error("Error while getting public and private keys ->> ",e);
}
}
private String performEncryption(String content,PublicKey publicKey) throws Exception {
logger.debug("Encrypting using public key : "+content);
Cipher publicEncryptCipher = Cipher.getInstance("RSA");
publicEncryptCipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedBinaryData = publicEncryptCipher.doFinal(content.getBytes());
Base64 encoder = new Base64();
String encodedEncryptedContent = new String(encoder.encode(encryptedBinaryData),"UTF-8");
logger.debug("Encrypted Content ->> "+encodedEncryptedContent);
return encodedEncryptedContent;
}
private String performDecryption(String encodedEncryptedContent, PrivateKey privateKey) throws Exception {
logger.debug("Decrypting with private key ->> "+encodedEncryptedContent);
Base64 decoder = new Base64();
byte[] encryptedString = decoder.decode(encodedEncryptedContent.getBytes());
Cipher privateDecryptCipher = Cipher.getInstance("RSA");
privateDecryptCipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedBinaryData = privateDecryptCipher.doFinal(encryptedString);
String decryptedContent = new String(decryptedBinaryData,"UTF-8");
logger.debug("Decrypted Content ->> "+decryptedContent);
return decryptedContent;
}
以同样的方式,我需要使用 HSM 设备进行加密/解密。我已安装 Luna 客户端软件并将密钥导入 HSM 设备。
谁能帮帮我
【问题讨论】:
-
假设您的 HSM 是 PKCS#11 设备,您只需重新配置加载的配置文件。你试过什么?
标签: java encryption cryptography hsm