C#从公钥证书密钥创建RSACryptoServiceProvider,该密钥是字符串

时间:2015-06-26 18:52:08

标签: c#

是否可以从公共证书密钥生成RSACryptoServiceProvider,该密钥是字符串。

我尝试:

RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
var publicKey = PublicKey.GetPublicKey();
provider.FromXmlString("<RSAKeyValue><Modulus>" + publicKey + "</Modulus></RSAKeyValue>");

但我得到的异常是无效的base64

  at Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.Security.Cryptography.CryptographicException: Couldn't decode XML ---> System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.

我该怎么办?

1 个答案:

答案 0 :(得分:1)

两件事。

首先:你的结束标签是倒退的......

</RSAKeyValue></Modulus>

应该阅读

</Modulus></RSAKeyValue>

第二:你错过了指数参数。

请参阅此工作代码:

RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
//var publicKey = PublicKey.GetPublicKey();
provider.FromXmlString(
    "<RSAKeyValue>"+
        "<Modulus>CmZ5HcaYgWjeerd0Gbt/sMABxicQJwB1FClC4ZqNjFHQU7PjeCod5dxa9OvplGgXARSh3+Z83Jqa9V1lViC7qw==</Modulus>"+
        "<Exponent>AQAB</Exponent>"+
    "</RSAKeyValue>");