RSACryptoServiceProvider
以如下格式接受密钥:
对于公钥:
<RSAKeyValue>
<Modulus>21wEnTU+mcD2w0Lfo1Gv4rtcSWsQJQTNa6gio05AOkV/Er9w3Y13Ddo5wGtjJ19402S71HUeN0vbKILLJdRSES5MHSdJPSVrOqdrll/vLXxDxWs/U0UT1c8u6k/Ogx9hTtZxYwoeYqdhDblof3E75d9n2F0Zvf6iTb4cI7j6fMs=</Modulus>
<Exponent>AQAB</Exponent>
</RSAKeyValue>
私钥:
<RSAKeyValue>
<Modulus>21wEnTU+mcD2w0Lfo1Gv4rtcSWsQJQTNa6gio05AOkV/Er9w3Y13Ddo5wGtjJ19402S71HUeN0vbKILLJdRSES5MHSdJPSVrOqdrll/vLXxDxWs/U0UT1c8u6k/Ogx9hTtZxYwoeYqdhDblof3E75d9n2F0Zvf6iTb4cI7j6fMs=</Modulus>
<Exponent>AQAB</Exponent>
<P>/aULPE6jd5IkwtWXmReyMUhmI/nfwfkQSyl7tsg2PKdpcxk4mpPZUdEQhHQLvE84w2DhTyYkPHCtq/mMKE3MHw==</P>
<Q>3WV46X9Arg2l9cxb67KVlNVXyCqc/w+LWt/tbhLJvV2xCF/0rWKPsBJ9MC6cquaqNPxWWEav8RAVbmmGrJt51Q==</Q>
<DP>8TuZFgBMpBoQcGUoS2goB4st6aVq1FcG0hVgHhUI0GMAfYFNPmbDV3cY2IBt8Oj/uYJYhyhlaj5YTqmGTYbATQ==</DP>
<DQ>FIoVbZQgrAUYIHWVEYi/187zFd7eMct/Yi7kGBImJStMATrluDAspGkStCWe4zwDDmdam1XzfKnBUzz3AYxrAQ==</DQ>
<InverseQ>QPU3Tmt8nznSgYZ+5jUo9E0SfjiTu435ihANiHqqjasaUNvOHKumqzuBZ8NRtkUhS6dsOEb8A2ODvy7KswUxyA==</InverseQ>
<D>cgoRoAUpSVfHMdYXW9nA3dfX75dIamZnwPtFHq80ttagbIe4ToYYCcyUz5NElhiNQSESgS5uCgNWqWXt5PnPu4XmCXx6utco1UVH8HGLahzbAnSy6Cj3iUIQ7Gj+9gQ7PkC434HTtHazmxVgIR5l56ZjoQ8yGNCPZnsdYEmhJWk=</D>
</RSAKeyValue>
我用Java创建我的密钥对:
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024);
KeyPair kp = kpg.genKeyPair();
PublicKey publicKey = kp.getPublic();
PrivateKey privateKey = kp.getPrivate();
修改
我想知道如何将我在Java中创建的密钥转换为C#XML密钥? 像Porting .Net RSA xml keys to Java这样的问题在反方向上做了我想要的。它从C#中读取密钥并生成它们以便在Java中使用。 我需要将java密钥转换为C#XML密钥。
答案 0 :(得分:0)
从RSAPublicKey (5.24 API Documentation)开始,Java类RSAPublicKey上有方法
java.math.BigInteger getModulus() Returns the modulus of the public key.
java.math.BigInteger getPublicExponent() Returns the public exponent of the public key.
所以从那里你应该能够连接格式良好的Xml字符串。
这也可以用How to Generate RSA Keys in Java _ Novixys Software Dev Blog
这个also quite close,它显示了在C#代码的两个组件之间传递的Xml密钥,尽管它略有不同,因为它是Elliptic Curve和Not RSA。关键方法是FromXmlString
很简单,您调用Java对象上的方法来提取模数和指数,构建字符串,将其保存到文件中,然后在C#中从文件加载到字符串中并传递到FromXmlString
。< / p>