Fix for Cannot recover key error in JAVA

This is how i fixed Cannot recover key error in JAVA while i was setting up an SSL configuration for one of the Application Servers. Following is the part of the exception that i noted in the error log of my app,I couldn't start my tomcat and  jboss servers because of the below error.

ERROR - Could not start server:java.security.UnrecoverableKeyException: Cannot recover key
java.io.IOException: !JsseListener: java.security.UnrecoverableKeyException: Cannot recover key
    at org.mortbay.jetty.security.SslSocketConnector.newServerSocket(SslSocketConnector.java:516)
    at org.mortbay.jetty.bio.SocketConnector.open(SocketConnector.java:73)
    at org.mortbay.jetty.AbstractConnector.doStart(AbstractConnector.java:283)
    at org.mortbay.jetty.bio.SocketConnector.doStart(SocketConnector.java:147)
    at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)

I see there are lot of post in google, but hard to find the exact solution, here it goes.

What causes this SSL error?
 
The answer is simple, the passwords of the Certificate Key and the generated keystore are different, they cannot be different and they should be same.If your certificate cer/crt/pfx key has a password called  hpanswers then your generated keystore file should also have a similar password called  hpanswers, it cannot be different, so both the certificate key and the jks keystore file should have the same password.

You either need to regenerate the keystore with the same password as certificate key or change the password for both..Set the key password to be the same as keystore password, using the below java keytool command

The below is an example , here the certificate key password is oldpass and the key password is  hpanswers, we are now changing they certificate key password to  hpanswers 

$ keytool -keypasswd -keystore test.jks -alias ssltest
Enter keystore password:  hpanswers
Enter key password for ssltesr:  oldpass
New key password for ssltest:  hpanswers
Re-enter new key password for :  hpanswers

Following is some useful information i noted while i was solving this posted on  globalsign

How to Fix "Cannot Recover Key" Error

The error “java.security.UnrecoverableKeyException: Cannot recover key” occurs when the keystore and keyEntry passwords are different. To resolve this issue, you must remove all traces of the past certificate and request the file.

You must generate a new keystore, keyEntry and CSR. You must also specify the same password for the keystore and the keyEntry. 

Useful References to understand this issue better.
  1. java.io.IOException: Cannot recover key
  2.  Cannot recover key tomcat
  3. Fish Eye Server Error
最简单的方法,直接用java里的keytool工具生成一个keystore文件,然后直接用这个文件启用https就可以了。 方法如下: 命令行执行%JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RSA 执行过程中会询问你一些信息,比如国家代码,省市等,其中需要填写两个密码,一次在开头,一次在最后,请保持两个密码相同。比如,我将密码都设成s3cret。 如果不同,启动会报错,大概是下面这样的 java.io.IOException: Cannot recover key 执行完成后会生成一个.keystore文件,将它复制到tomcat的bin目录下(并不一定,放哪里都可以) 打开conf目录下的server.xml文件,找到以下这一段 它被注释掉了,将注释去掉,并将这一段改成以下 maxThreads="150" scheme="https" secure="true" keystoreFile="bin/.keystore" keystorePass=" s3cret" clientAuth="false" sslProtocol="TLS" /> 之后启动tomcat就可以了,通过https方式访问8443端口,就能看到效果。如果用http访问之前的端口,那么还是普通的未加密连接。 到这里问题来了,我的目的是启用https,但现在http还能访问,那么就可以绕开https。https也就起不了什么作用了。因此要强制访问https。 打开你的web应用的web.xml文件,在最后加上这样一段 Protected Context /* CONFIDENTIAL 重启tomcat,现在你放问原来的地址,假设是http://localhost:8080/mywebapp/,可以看到,连接被重定向到了https的连接 https://localhost:8443/mywebapp/。这样,我们的目的达到了。 但似乎还有点小问题,keystorePass="s3cret",这个密码直接被明码方式卸载server.xml里。总觉得有还是有点不爽。 那么还有一种稍微复杂点的方式,我们使用openssl。 首先,需要下载openssl,为了方便,可以下载一个绿色版, 加压后除了openssl.exe以外,还有一个bat文件,这个可以帮助我们快速创建证书申请文件。 运行autocsr.bat,按照提示输入信息,之后按任意键确认。你会得到两个文件,一个server.key,这是私钥文件,还有一个名为certreq.csr的证书请求文件。 如果你要向证书颁发机构申请正式的安全证书,那么就把这个certreq.csr文件发给他们就行了。他们会给你发来两个cer文件,一个是服务器证书,一个是根证书 如果你只是要使用https,那么证书自己签署就可以了。 在命令行下进入刚才解压的目录,找到openssl.exe所在的目录,执行以下命令 openssl x509 -req -in certreq.csr -out cert.cer -signkey server.key -days 3650 现在你将得到一个名为cert.cer的证书文件。 修改server.xml将 maxThreads="150" scheme="https" secure="true" keystoreFile="bin/.keystore" keystorePass=" s3cret" clientAuth="false" sslProtocol="TLS" /> 修改为以下内容(假设cert.cer和server.key文件都放在tomcat的conf目录下) maxThreads="150" scheme="https" secure="true" SSLCertificateFile="conf/cert.cer" SSLCertificateKeyFile="conf/server.key" sslProtocol="TLS" /> PS.如果真的向证书颁发机构申请到了正式的安全证书,那么配置还有点不同,如下 maxThreads="150" scheme="https" secure="true" SSLCertificateFile="conf/server.cer" SSLCertificateKeyFile="conf/server.key" SSLCertificateChainFile="conf/intermediate.cer" sslProtocol="TLS" /> 因为证书颁发机构会给两个整数,一个是签署后的服务器证书,还有一个中级CA证书,所以要多一行配置。 可能证书颁发机构只会给你服务器证书也就是server.cer, 中级的CA证书intermediate.cer 需要到 证书颁发机构提供的网站中去下载,具体的操作会为证书颁发机构给发的邮箱中会有相关的提示 好了,到这里都配置完了,重启tomcat,就可以看到效果。不过,看到的通常会是一个exception,大概是说APR not available 如果遇到这个异常,说明你的tomcat没有安装apr支持 apr安装详见:http://www.blogjava.net/yongboy/archive/2009/08/31/293343.html 之后启动tomcat,问题应该解决了,看起来效果和第一种方式没什么不同。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值