CryptographicException in RSA.ImportParameters() -特殊1024密钥的坏

本文关键字:1024 特殊 密钥 in RSA ImportParameters CryptographicException | 更新日期: 2023-09-27 18:04:03

我们有c#/。. Net 4.0应用程序,从WebService接收的Base64字符串中导入RSA私钥。

此应用程序适用于1024位的rsa - key,但不适用特殊类型的rsa私钥(约占密钥的1%)。

字节长度如下:

工作重点:

  • 模量=> 128 Bytes
  • index => 3 Bytes
  • D => 128 Bytes
  • P => 64 Bytes
  • Q => 64 Bytes
  • DP => 64 Bytes
  • DQ => 64 Bytes
  • IQ => 64 Bytes

Not-Working-Key:

  • 模量=> 128 Bytes
  • index => 3 Bytes
  • D => 127 Bytes
  • P => 64 Bytes
  • Q => 64 Bytes
  • DP => 64 Bytes
  • DQ => 64 Bytes
  • IQ => 64 Bytes

差异在于D的长度(128有效,127不有效)。非工作密钥比工作密钥短1字节。

参数是设置的,但是当做RSA.ImportParameters(rsaParams)时,它抛出一个带有"坏数据"消息的CryptographicException。

应该包括什么来解决这个问题?

CryptographicException in RSA.ImportParameters() -特殊1024密钥的坏

RSACryptoServiceProvider对数据长度有如下假设:

  • 模量:任意偶数大小,我们称长度为n
  • Exponent: (<= 4 bytes;尽管rsacg允许"任意大小"),让我们将长度称为e
  • d: n
  • p: n/2
  • q: n/2
  • dp: n/2
  • dq: n/2
  • InverseQ: n/2

所以,假设你的第二个键实际上是Modulus: 128字节(因为64字节的p乘以64字节的Q不是256字节的数字),你只需要在D数组左填充一个0,使其达到适当的长度。

byte[] newD = new byte[modulus.Length];
Buffer.BlockCopy(d, 0, newD, newD.Length - d.Length, d.Length);

。. NET Core有源代码可以显示这种关系。在。net框架中,它隐藏在CLR中,因此在参考资源中不可用。