我正在尝试使用C#运行RSA解密。
我的代码:
byte[] n = BigInteger.Parse("3104649130901425335933838103517383").ToByteArray();
byte[] p = BigInteger.Parse("49662237675630289").ToByteArray();
byte[] q = BigInteger.Parse("62515288803124247").ToByteArray();
byte[] e = BigInteger.Parse("65537").ToByteArray();
byte[] d = BigInteger.Parse("1427000713644866747260499795119265").ToByteArray();
byte[] dp = BigInteger.Parse("15085765714732865").ToByteArray();
byte[] dq = BigInteger.Parse("1326865232237451").ToByteArray();
byte[] iq = BigInteger.Parse("5777070651124236").ToByteArray();
RSAParameters rsaparams = new RSAParameters();
rsaparams.P = p;
rsaparams.Q = q;
rsaparams.Modulus = n;
rsaparams.Exponent = e;
rsaparams.D = d;
rsaparams.DP = dp;
rsaparams.DQ = dq;
rsaparams.InverseQ = iq;
RSACryptoServiceProvider csp = new RSACryptoServiceProvider();
csp.ImportParameters(rsaparams);
当我运行它时,我收到此错误:
有人能告诉我我做错了吗?
答案 0 :(得分:0)
显然RSAParameters
期望其组件byte []数组采用big-endian格式。因此,您必须反转从BigInteger.ToByteArray()
获得的每个字节数组中的字节数。示例性就地反向方法是:
public static void reverse (ref byte[] x)
{
int i = 0;
int j = x.Length - 1;
while (i < j)
{
byte t = x [i];
x [i] = x [j];
x [j] = t;
i++;
j--;
}
}