2013-04-17 51 views
0

我想解密是通过使用我的网站的公共密钥在客户端加密的消息:访问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

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

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); 
+0

这就像一个魅力,谢谢! – Emmanuel