为什么RSAParameters Modulus不等于P和Q的乘积? [英] Why is RSAParameters Modulus not equal product of P and Q?

查看:18
本文介绍了为什么RSAParameters Modulus不等于P和Q的乘积?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

P 和 Q 的值与 .Net RSAParameters 的模量值不匹配.根据 RSA 算法和 MSDN 文档,它应该是:P * Q = Modulus

The values of P and Q do not match value of the Modulus of the .Net RSAParameters. According to RSA algorithm and MSDN documentation it should be: P * Q = Modulus

我生成了一个 512 位 RSA 密钥对并通过调用将其导出为 XML:

I generated a 512bit RSA keypair and exported it to XML by invoking:

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(512);
rsa.ToXmlString(true);

这给了我以下 XML:

This gave me the following XML:

<RSAKeyValue>
  <Modulus>rcLI1XTfmXtX05zq67d1wujnUvevBu8dZ5Q5uBUi2mKndH1FZLYCKrjFaDTB/mXW1l5C74YycVLS6msY2NNJYw==</Modulus>
  <Exponent>AQAB</Exponent>
  <P>1dwGkK5POlcGCjQ96Se5NSPu/hCm8F5EYwyqRpLVzgk=</P>
  <Q>0AAEMHBj7CP2XHfCG/RzGldw1GdsW13rTo3uEE9Dtws=</Q>
  <DP>PO4jMLV4/TYuElowCW235twGC3zTE0jIUzAYk2LiZ4E=</DP>
  <DQ>ELJ/o5fSHanBZCjk9zOHbezpDNQEmc0PT64LF1oVmIM=</DQ>
  <InverseQ>NyCDwTra3LiUin05ZCGkdKLwReFC9L8Zf01ZfYabSfQ=</InverseQ>
  <D>EWwFTPmx7aajULFcEJRNd2R4xSXWY8CX1ynSe7WK0BCH42wf/REOS9l8Oiyjf587BhGa3y8jGKhUD7fXANDxcQ==</D>
</RSAKeyValue>

现在我成功编写了一个小测试程序来加密、解密、签名和验证数据.

Now I successfully wrote a litte test programm to encrypt, decrypt, sign and verify data.

最后我加了一点测试代码:

At the end I added a little test code:

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(512);
rsa.FromXmlString(key); // key = string with XML above

RSAParameters param = rsa.ExportParameters(true);
BigInteger p = new BigInteger(param.P);
BigInteger q = new BigInteger(param.Q);
BigInteger n = new BigInteger(param.Modulus);
BigInteger myN = BigInteger.Multiply(p, q);
Console.WriteLine("n   = " + n.ToString());
Console.WriteLine("myN = " + myN.ToString());

这给了我们以下输出:

<代码> N = 5200154866521200075264779234483365112866265806746380532891861717388028374942014660490111623133775661411009378522295439774347383363048751390839618325234349MYN = 23508802329434377088477386089844302414021121047189424894399694701810500376591071843028984420422297770783276119852460021668188142735325512873796040092944

为什么 P 和 Q 相乘不等于模数?

Why does multiplying P and Q not equal the Modulus?

我已经检查了很多东西,比如字节序、编码、BigInteger 类,使用上述 XML 密钥成功加密、解密、签名、验证,但找不到任何解释为什么 P 和 Q 相乘不等于模数...

I already checked a lot of things like endian, encoding, BigInteger class, successfully encrypted, decrypted, signed, verified with the above XML keys but cannot find any explanation why P and Q multiplied is not equaling the Modulus...

谁能帮我解释一下为什么 P*Q 不是模数?

Can anybody help me explain why P*Q is not the Modulus ?

可读格式的所有值:

<代码>模量= 5200154866521200075264779234483365112866265806746380532891861717388028374942014660490111623133775661411009378522295439774347383363048751390839618325234349指数 = 65537P = 4436260148159638728185416185189716006279182659244174640493183003717504785621Q = 5299238895894527538601438806093945941831432623367272568173893997325464109264DP = -57260184070162652127728137041376572684067529466727954512100856352006444159428DQ = -56270397953566513533764063103154348713259844205844432469862161942601135050224逆Q = -5297700950752995201824767053303055736360972826004414985336436365496709603273d = 5967761894604968266284398550464653556930604493620355473531132912985865955601309375321441883258487907574824598936524238049397825498463180877735939967118353

更新:

根据答案,我为 .Net BigInteger 类编写了一个小扩展方法,以便与 RSAParameters 一起正常工作:

According to the answer I wrote a little extension method for the .Net BigInteger class to work correctly with the RSAParameters:

public static class BigIntegerExtension
{
    public static BigInteger FromBase64(this BigInteger i, string base64)
    {
        byte[] p = Convert.FromBase64String(base64).Reverse().ToArray();
        if (p[p.Length - 1] > 127)
        {
            Array.Resize(ref p, p.Length + 1);
            p[p.Length - 1] = 0;
        }
       return new BigInteger(p);
    }

    public static BigInteger FromBigEndian(this BigInteger i, byte[] p)
    {
        p = p.Reverse().ToArray();
        if (p[p.Length - 1] > 127)
        {
            Array.Resize(ref p, p.Length + 1);
            p[p.Length - 1] = 0;
        }
        return new BigInteger(p);
    }
}

使用示例:

BigInteger modulus1 = new BigInteger().FromBase64("rcLI1XTfmXtX05zq67d1wujnUvevBu8dZ5Q5uBUi2mKndH1FZLYCKrjFaDTB/mXW1l5C74YycVLS6msY2NNJYw==");

BigInteger modulus2 = new BigInteger().FromBigEndian(param.Modulus);

希望这可以帮助其他有同样问题的人:-)

Hope this helps others with the same problem :-)

推荐答案

这是我对你提供的 XML 参数的解析:

Here is my parsing of the XML parameters you provided:

<代码> N = 9100595563660672087698322262735024483609782000266155222822537546670463733453350686171384417480667378838400923087358115007100900745853538273602044437940579P = 96731388413554317303099785843734797692955743043844132225634400270674214374921Q = 94081101418218318334927154927633016498744568046568114230258529096538660255499

如您所见,N 确实等于 P * Q.

As you can verify, N does indeed equal P * Q.

您不能像现在这样使用 BigInteger(byte []) 构造函数,因为它期望字节数组采用小端序,并且因为 Microsoft 已经做了一些事情来表示赞同.相反,颠倒字节的顺序.最后,因为字节数组应该是二进制补码并且您的数字保证为正数,您必须在数组的高位字节中添加一个零字节 if 否则高位字节将大于或等于 128.

You cannot use the BigInteger(byte []) constructor the way you are because it expects the byte array to be in little-endian, and because Microsoft has done things bass ackwards. Instead, reverse the order of the bytes. And finally, because the bytes arrays are supposed to be twos complement and your numbers are guaranteed to be positive you must add a zero byte to the high order byte of the array if the high-order byte would otherwise be greater than or equal to 128.

这篇关于为什么RSAParameters Modulus不等于P和Q的乘积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆