带原始密钥的RSA密码术,c#

时间:2017-04-06 01:07:45

标签: c# cryptography rsa

我得到了256字节模数(' n'),256字节私有指数(' d')和3字节{1,0,1}公共指数(' E&#39)。我正在尝试签署32字节的数据。

我试图创建一个新的RSAParameters。

byte[] n = new byte[256]; //populated in my code 
byte[] d = new byte[256]; //populated in my code
byte[] e = new byte[]{1,0,1}; 
byte[] junkData = new byte[32]; //populated in my code

RSAParameters rsaParam = new RSAParameters();
rsaParam.Modulus = n;
rsaParam.Exponent = e;
rsaParam.D = d;

然后我创建一个RSACrytoServiceProvider,将参数导入其中,并尝试对数据进行签名。

var csp = new RSACryptoServiceProvider(2048);
csp.ImportParameters(rsaParam);
csp.SignData(junkData, new SHA1CryptoServiceProvider());

问题是RSACryptoServiceProvider似乎只是公开的,当我尝试签名时,我收到一个' Keyset不存在'密码学例外。

我是否还需要P和Q元素来正确签署数据,或者我做的事情显然是错误的?谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

I'm surprised that the ImportParameters didn't throw (are you using Mono?).

RSACryptoServiceProvider (and all of the RSA implementations in .NET Framework and .NET Core) require a fully populated RSAParameters structure for private keys.

https://stackoverflow.com/a/42117655/6535399 gives the answer in a bit more detail.