访问服务器上的SSL证书(私钥)

时间:2013-04-17 19:32:07

标签: java-ee tomcat ssl

我想使用我网站的公钥解密在客户端加密的邮件:

URL httpsURL = new URL("https://mediashuttle.com/");
HttpsURLConnection connection = (HttpsURLConnection) httpsURL.openConnection();
connection.connect();
Certificate cert = connection.getServerCertificates()[0];
PublicKey publicKey = cert.getPublicKey();

现在在服务器(Tomcat)端,我想解密传递给Servlet的消息。你能告诉我如何在Tomcat中检索私钥来解密消息吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

您需要从存储服务器密钥的密钥库中获取密钥。像这样:

File keyStoreFile = new File("path/to/keystore/file.jks");
InputStream inputStream = new FileInputStream(keyStoreFile);

KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(inputStream, "password".toCharArray());
Key key = keyStore.getKey("yourKeyAlias", "changeit".toCharArray());
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] encrypted = getEncripted();
byte[] decrypted = cipher.doFinal(encrypted);