java.security.UnrecoverableKeyException: Cannot recover key

236 篇文章 0 订阅

(1)java代码通过keystore文件获取私钥报错

使用keytool 工具生成keystore文件,然后通过java 获取私钥privateKey 时,报错:

java.security.UnrecoverableKeyException: Cannot recover key
	at sun.security.provider.KeyProtector.recover(KeyProtector.java:311)
	at sun.security.provider.JavaKeyStore.engineGetKey(JavaKeyStore.java:121)
	at sun.security.provider.JavaKeyStore$JKS.engineGetKey(JavaKeyStore.java:38)
	at java.security.KeyStore.getKey(KeyStore.java:763)
	at com.jn.test.TestCA.test_01(TestCA.java:18)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)

具体操作如下

使用keystool 生成本地数字证书

keytool -genkeypair -keyalg RSA -keysize 2048 -sigalg SHA1withRSA -validity 36000 -alias localhost -storepass abcdefg -keystore zlex.keystore -dname "CN=localhost, OU=zlex,O=zlex, L=BJ, ST=BJ, C=CN"

 运行结果:

 说明:keystore的密码是abcdefg,通过-storepass 指定。

 

java 代码如下:

@Test
	public void test_01() throws Exception {
		String keyStorePath="d:\\Temp\\a\\a\\ca\\zlex.keystore";
		String password="abcdefg";
		// 获得密钥库
		KeyStore ks = getKeyStore(keyStorePath, password);
		// 获得私钥
		PrivateKey privateKey = (PrivateKey) ks.getKey("localhost", password.toCharArray());
		System.out.println(privateKey);
	}

	/**
	 * 获得KeyStore
	 * 
	 * @param keyStorePath
	 *            密钥库路径
	 * @param password
	 *            密码
	 * @return KeyStore 密钥库
	 */
	private static KeyStore getKeyStore(String keyStorePath, String password)
			throws Exception {
		// 实例化密钥库
		KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
		// 获得密钥库文件流
		FileInputStream is = new FileInputStream(keyStorePath);
		// 加载密钥库
		ks.load(is, password.toCharArray());
		// 关闭密钥库文件流
		is.close();
		return ks;
	}

 运行上述java 代码时,报错:java.security.UnrecoverableKeyException: Cannot recover key

到底是什么原因呢?

原因:keystore 密码和主密码不同

解决方法:keystore 密码和主密码使用相同的密码。

详情请参考:http://stackoverflow.com/questions/4926290/java-keystore-and-password-settings

 

(2)tomcat使用keystore文件启动报错

keystore 密码和主密码不同,启动tomcat时也会报错

命令:keytool -genkey -alias tomcat -keyalg RSA -keysize 1024 -validity 365 -keystore tomcat22.keystore

生成的文件 就是:tomcat22.keystore


 

密码一和 密码二必须相同,否则,启动tomcat 时会报错。

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现在 PDF 文档中添加数字证书,需要使用以下步骤: 1. 生成数字证书文件,可以使用工具如 OpenSSL。 2. 在 PDF 文档中添加数字证书需要使用 PDF 签名工具,比如 Adobe Acrobat 或者 iText。 3. 在代码中使用 PDF 签名工具加载 PDF 文档,然后使用数字证书对 PDF 文档进行签名。 以下是使用 iText 实现添加数字证书的示例代码: ``` import java.io.*; import java.security.*; import java.security.cert.Certificate; import java.security.cert.CertificateException; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Rectangle; import com.itextpdf.text.pdf.*; import com.itextpdf.text.pdf.security.*; public class AddDigitalSignature { public static void main(String[] args) throws IOException, DocumentException, NoSuchAlgorithmException, KeyStoreException, CertificateException, UnrecoverableKeyException { String src = "input.pdf"; String dest = "output.pdf"; String keystorePath = "keystore.jks"; char[] password = "password".toCharArray(); String alias = "alias"; String certificatePath = "certificate.crt"; // Load keystore KeyStore ks = KeyStore.getInstance("JKS"); ks.load(new FileInputStream(keystorePath), password); // Get private key PrivateKey pk = (PrivateKey) ks.getKey(alias, password); // Get certificate chain Certificate[] chain = ks.getCertificateChain(alias); // Load certificate CertificateFactory cf = CertificateFactory.getInstance("X.509"); InputStream certStream = new FileInputStream(certificatePath); Certificate cert = cf.generateCertificate(certStream); // Create signature appearance PdfReader reader = new PdfReader(src); Rectangle rect = new Rectangle(36, 648, 200, 100); PdfSignatureAppearance appearance = PdfStamper.createSignature(reader, new FileOutputStream(dest), '\0'); appearance.setReason("Test"); appearance.setLocation("Test"); appearance.setSignatureCreator("Test"); appearance.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED); appearance.setVisibleSignature(rect, 1, null); // Create signature ExternalDigest digest = new BouncyCastleDigest(); ExternalSignature signature = new PrivateKeySignature(pk, "SHA-256", "BC"); MakeSignature.signDetached(appearance, digest, signature, chain, null, null, null, 0, CryptoStandard.CMS); certStream.close(); } } ``` 在上面的代码中,我们首先加载 keystore 文件,获取私钥证书链。然后加载数字证书文件,创建 PDF 签名外观,设置签名属性,包括签名位置、原因、位置、签名者等。接着创建签名,将签名结果保存到输出 PDF 文档中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值