无法使用 java / keytool 签署挖掘签名

发布于 2024-09-07 12:12:27 字数 1679 浏览 8 评论 0原文

我基本上直接从 keytool 示例页面创建了一个证书:

keytool -genkey -dname "cn=Anything, ou=Anything, o=Anything, c=US" -alias Business -keypass kpi135 -keystore C:\mykeystore -storepass ab987c -validity 1095

我正在尝试访问此证书并使用私钥部分对文本的一部分进行数字签名以向第三方进行身份验证。下面是我正在尝试的代码:

   //Add bouncyCastle as a provider
   Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

   KeyStore keystore = KeyStore.getInstance("JKS");
   keystore.load(new FileInputStream("C:\\mykeystore"), "ab987c".toCharArray());
   Signature sig = Signature.getInstance("MD5withRSA", "BC");
   PrivateKey privateKey = (PrivateKey)keystore.getKey("business", "kpi135".toCharArray()); //Exception here
   sig.initSign(privateKey);
   sig.update("myUID__myNonce".getBytes());
   byte[] digitalSignature = sig.sign();

   System.out.println("Signature Formulated: " + digitalSignature);

我得到以下异常:

java.security.InvalidKeyException: Supplied key (sun.security.provider.DSAPrivat
eKey) is not a RSAPrivateKey instance
        at org.bouncycastle.jce.provider.JDKDigestSignature.engineInitSign(Unkno
wn Source)
        at java.security.Signature$Delegate.engineInitSign(Signature.java:1095)
        at java.security.Signature.initSign(Signature.java:480)
        at MainClass.<init>(MainClass.java:15)
        at MainClass.main(MainClass.java:28)

我认为这是因为我以某种方式使用错误类型的密钥创建了证书,但我不确定我是否通过挖掘找到了我需要的东西密钥工具页面。它确实提到您显然可以使用 -keysig RSA 和 -sigalg RSA 生成密钥,但是当我在创建证书时尝试这些标志时,我得到:

keytool error: java.security.NoSuchAlgorithmException: RSA Signature not availab
le

enter code here

I have created a certificate basically straight from the keytool example page:

keytool -genkey -dname "cn=Anything, ou=Anything, o=Anything, c=US" -alias business -keypass kpi135 -keystore C:\mykeystore -storepass ab987c -validity 1095

I am trying to access this certificate and use the private key portion to digitally sign a portion of text to authenticate with a third party. Below is the code I'm attempting:

   //Add bouncyCastle as a provider
   Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

   KeyStore keystore = KeyStore.getInstance("JKS");
   keystore.load(new FileInputStream("C:\\mykeystore"), "ab987c".toCharArray());
   Signature sig = Signature.getInstance("MD5withRSA", "BC");
   PrivateKey privateKey = (PrivateKey)keystore.getKey("business", "kpi135".toCharArray()); //Exception here
   sig.initSign(privateKey);
   sig.update("myUID__myNonce".getBytes());
   byte[] digitalSignature = sig.sign();

   System.out.println("Signature Formulated: " + digitalSignature);

I get the following exception:

java.security.InvalidKeyException: Supplied key (sun.security.provider.DSAPrivat
eKey) is not a RSAPrivateKey instance
        at org.bouncycastle.jce.provider.JDKDigestSignature.engineInitSign(Unkno
wn Source)
        at java.security.Signature$Delegate.engineInitSign(Signature.java:1095)
        at java.security.Signature.initSign(Signature.java:480)
        at MainClass.<init>(MainClass.java:15)
        at MainClass.main(MainClass.java:28)

I assume it's because I've somehow created the certificate with the wrong type of key, but I'm not sure I'm finding what I need by digging through the keytool page. It does mention that you can apparently generate a key using -keysig RSA and -sigalg RSA however when I try those flags when creating a certificate I get:

keytool error: java.security.NoSuchAlgorithmException: RSA Signature not availab
le

enter code here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2024-09-14 12:12:30

实际上,您似乎可以将“-keyalg RSA”添加到 keygen 命令中,从而缓解不匹配问题。我在同一个 keygen 命令中错误地尝试了 -keyalg 和 -sigalg 。上面的代码现在执行时没有异常。

Actually it seems you can add "-keyalg RSA" to the keygen command which alleviates the mismatch issue. I was incorrectly trying both -keyalg and -sigalg in the same keygen command. The code above now executes without exceptions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文