将 .Net RSA xml 密钥移植到 Java
我有来自 .Net 系统的 xml 格式的私钥和公钥。我必须使用这个密钥在 Java 中执行加密/解密。有什么办法可以做到吗?
公钥看起来像这样:
<RSAKeyValue>
<Modulus>jHIxcGzzpByFv...pvhxFnP0ssmlBfMALis</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
私钥:
<RSAKeyValue>
<Modulus>4hjg1ibWXHIlH...ssmlBfMAListzrgk=</Modulus>
<Exponent>AQAB</Exponent>
<P>8QZCtrmJcr9uW7VRex+diH...jLHV5StmuBs1+vZZAQ==</P>
<Q>8CUvJTv...yeDszMWNCQ==</Q>
<DP>elh2Nv...cygE3657AQ==</DP>
<DQ>MBUh5XC...+PfiMfX0EQ==</DQ>
<InverseQ>oxvsj4WCbQ....LyjggXg==</InverseQ>
<D>KrhmqzAVasx...uxQ5VGZmZ6yOAE=</D>
</RSAKeyValue>
我已经编写了一些代码来加密数据,但我不确定它是否正确。
Element modulusElem = root.getChild("Modulus");
Element exponentElem = root.getChild("Exponent");
byte[] expBytes = decoder.decodeBuffer(exponentElem.getText().trim());
byte[] modBytes = decoder.decodeBuffer(modulusElem.getText().trim());
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(new BigInteger(1, modBytes), new BigInteger(1, expBytes));
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey pubKey = fact.generatePublic(keySpec);
如何从 xml 中生成私钥来解密数据?
I have private and public keys from the .Net system in the xml format. I have to use this keys to perform encryption/decryption in Java. Is there any way to do it?
Public key looks something like this:
<RSAKeyValue>
<Modulus>jHIxcGzzpByFv...pvhxFnP0ssmlBfMALis</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
Private key:
<RSAKeyValue>
<Modulus>4hjg1ibWXHIlH...ssmlBfMAListzrgk=</Modulus>
<Exponent>AQAB</Exponent>
<P>8QZCtrmJcr9uW7VRex+diH...jLHV5StmuBs1+vZZAQ==</P>
<Q>8CUvJTv...yeDszMWNCQ==</Q>
<DP>elh2Nv...cygE3657AQ==</DP>
<DQ>MBUh5XC...+PfiMfX0EQ==</DQ>
<InverseQ>oxvsj4WCbQ....LyjggXg==</InverseQ>
<D>KrhmqzAVasx...uxQ5VGZmZ6yOAE=</D>
</RSAKeyValue>
I have written a bit of code to encrypt data but I am not sure if its correct.
Element modulusElem = root.getChild("Modulus");
Element exponentElem = root.getChild("Exponent");
byte[] expBytes = decoder.decodeBuffer(exponentElem.getText().trim());
byte[] modBytes = decoder.decodeBuffer(modulusElem.getText().trim());
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(new BigInteger(1, modBytes), new BigInteger(1, expBytes));
KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey pubKey = fact.generatePublic(keySpec);
How can I make a private key from the xml to decrypt the data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的示例中的
decoder
是在进行 Base64 解码吗?看起来您可能依赖于sun.misc.BASE64Decoder
并且依赖这些内部类通常不是一个好主意(例如其他 JVM 不会有它)。您可以使用具有 Base64 类进行解码的 Apache Commons Codec。以下是 RSA 加密和解密所需的其余内容:Is that
decoder
in your example doing the Base64 decoding? It looks like you might be relying onsun.misc.BASE64Decoder
and it's generally not a good idea to depend on those internal classes (other JVM's won't have it for instance). You could use Apache Commons Codec that has a Base64 class to decode with. Here's the rest of what you need though for RSA encryption and decryption:尝试使用
并添加所有私钥组件
Try to use
and add all private key component