2011-04-03 52 views
12

我从.NET系统的XML格式的私钥和公钥。我必须使用这些密钥来执行Java中的加密/解密。有什么办法可以做到吗?移植的.Net RSA 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私钥来解密数据?

+0

能否请你也分享你的工作样本?我被困在一个非常类似的问题,并没有找到关于它的网络。 – c0d3Junk13 2013-02-20 21:01:33

+0

你用什么来解析XML? – ecem 2013-03-26 15:30:20

+0

好吧,我觉得这是JDOM,我不应该使用Google之前问的事情。 – ecem 2013-03-26 15:35:41

回答

23

那是decoder在您的例子做的Base64解码?它看起来像你可能会依靠sun.misc.BASE64Decoder和它通常是不依赖于这些内部类是一个好主意(其他JVM的不会有它的实例)。您可以使用具有Base64类的Apache Commons Codec来解码。下面是你需要什么,其余尽管对于RSA加密和解密:

byte[] expBytes = Base64.decodeBase64(exponentElem.getText().trim())); 
byte[] modBytes = Base64.decodeBase64(modulusElem.getText().trim()); 
byte[] dBytes = Base64.decodeBase64(dElem.getText().trim()); 

BigInteger modules = new BigInteger(1, modBytes); 
BigInteger exponent = new BigInteger(1, expBytes); 
BigInteger d = new BigInteger(1, dBytes); 

KeyFactory factory = KeyFactory.getInstance("RSA"); 
Cipher cipher = Cipher.getInstance("RSA"); 
String input = "test"; 

RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(modules, exponent); 
PublicKey pubKey = factory.generatePublic(pubSpec); 
cipher.init(Cipher.ENCRYPT_MODE, pubKey); 
byte[] encrypted = cipher.doFinal(input.getBytes("UTF-8")); 
System.out.println("encrypted: " + new String(encrypted)); 

RSAPrivateKeySpec privSpec = new RSAPrivateKeySpec(modules, d); 
PrivateKey privKey = factory.generatePrivate(privSpec); 
cipher.init(Cipher.DECRYPT_MODE, privKey); 
byte[] decrypted = cipher.doFinal(encrypted); 
System.out.println("decrypted: " + new String(decrypted)); 
+0

谢谢。它运作良好。只是有点好奇我们为什么没有使用私钥Xml中的任何其他元素? (P,Q,DP,DQ等) – dvl 2011-04-03 06:05:25

+1

没问题。这些其他元素是用于密钥生成的原始素数(p和q)以及一些预先计算的值。它们可以用来以另一种方式进行解密(使用中国剩余定理),速度更快。你可以通过创建一个具有所有元素的'RSAPrivateCrtKeySpec'来实际使用它。 – WhiteFang34 2011-04-03 07:06:53

+1

嘿,只是一个简单的问题,我应该怎么用解析这个公钥/私钥XML文件?我想要一些非常容易使用的东西,而且我感觉就像在淹没在XML解析器的海洋中。 – ecem 2013-03-26 15:32:25

0

尝试使用

RSAPrivateKeySpec privSpec = new RSAPrivateCrtKeySpec(iModulus,iExponentBytes,iDBytes,iPBytes,iQBytes,iDPBytes,iDQBytes,iInverseQBytes) 

,并添加所有私有密钥组件