c# - 从 bouncycaSTLe 导入 RSA key 有时会抛出 "Bad Data"

标签 c# rsa bouncycastle

有时,(经常足以成为 .NET 和 bouncy caSTLe 之间的严重问题),bouncycaSTLe 生成的 key 不会导入 dotnet RSA 加密提供程序。

它只会抛出“Données Incorrectes”;没有更多的细节。 (“坏数据”)。我无法调试问题,因为有罪的函数实现似乎隐藏在 CLR 中(Utils._ImportKey() ;引用源 RSACryptoServiceProvider.cs:297)。

我尝试更改“提供者”但没有成功。

这里有同样的问题。通过更改 key 或 key 大小在某种程度上解决了:BouncyCastle RSAPrivateKey to .NET RSAPrivateKey ;它在我的单元测试中失败, key 大小从 512 位到 2048 位不等。

如何解决/调试此类问题? 坏数据是什么意思?

这是一个测试用例,它的值失败了:

[TestCase(
        "3130061425891827008704201520933220266588903615593292093008732204896232681270200769431823371565724812996700795538563485957721923348815282268698793938491993",//mod
        "65537",//pe
        "3130061425891827008704201520933220266588903615593292093008732204896232681270200769431823371565724812996700795538563485957721923348815282268698793938491993",//priv e
        "108172619413453999338304010966268975159507181290909920458641813606026415083917",//p
        "75249617574313725168879024231390763478340191084309820124417146187514704207891",//q
        "46308055148448439895562160789624828220320330169183342667312429963694967752481", //dp
        "237677507940292370873826357872619864199100043554818389089435727311526981263", //dq
        "4755193289666548078142536433103759575424135202658906348751587662200087509503"//qinv
    )]
    public void TestBadKeyForMicrosoft(string mo, string pe, string prive, string p, string q, string dp, string dq, string qinv)
    {
        var k = new RsaPrivateCrtKeyParameters(
            new BigInteger(mo),//mod
            new BigInteger(pe),//pe
            new BigInteger(prive),//priv e
            new BigInteger(p),//p
            new BigInteger(q),//q
            new BigInteger(dp),//dp
            new BigInteger(dq),//dq
            new BigInteger(qinv)//qinv
            );

        var dotNetRsa = Org.BouncyCastle.Security.DotNetUtilities.ToRSAParameters(k);
        //var rsaCsp = new RSACryptoServiceProvider(new CspParameters(24 /*PROV_RSA_AES */)) { PersistKeyInCsp = false };
        var rsaCsp = new RSACryptoServiceProvider() {PersistKeyInCsp = false};
        rsaCsp.ImportParameters(dotNetRsa);
    }

最佳答案

我想这只是一个填充问题。

Bouncy-caSTLe 最新的 GIT 版本有以下代码:

注意:“Nuget”版本 (2011) 中未修复

public static RSAParameters ToRSAParameters(RsaPrivateCrtKeyParameters privKey)
{
   RSAParameters rp = new RSAParameters();
   rp.Modulus = privKey.Modulus.ToByteArrayUnsigned();
   rp.Exponent = privKey.PublicExponent.ToByteArrayUnsigned();
   rp.P = privKey.P.ToByteArrayUnsigned();
   rp.Q = privKey.Q.ToByteArrayUnsigned();
   rp.D = ConvertRSAParametersField(privKey.Exponent, rp.Modulus.Length);
   rp.DP = ConvertRSAParametersField(privKey.DP, rp.P.Length);
   rp.DQ = ConvertRSAParametersField(privKey.DQ, rp.Q.Length);
   rp.InverseQ = ConvertRSAParametersField(privKey.QInv, rp.Q.Length);
   return rp;
}

private static byte[] ConvertRSAParametersField(BigInteger n, int size)
{
   byte[] bs = n.ToByteArrayUnsigned();
   if (bs.Length == size)
      return bs;
   if (bs.Length > size)
      throw new ArgumentException("Specified size too small", "size");
   byte[] padded = new byte[size];
   Array.Copy(bs, 0, padded, size - bs.Length, bs.Length);
   return padded;
}

此代码不同于您在其他任何地方看到的代码,后者基本上是复制/粘贴关键参数,并且不执行额外的填充步骤。

关于c# - 从 bouncycaSTLe 导入 RSA key 有时会抛出 "Bad Data",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28370414/

相关文章:

c# - 如何正确重写此linq查询?

c# - 从 dll 构建 C# 项目

java - 使用给定公钥的 RSA 加密(Java 中)

java - 作为供应商的 Bouncy CaSTLe 与 Bouncy CaSTLe API

java - 使用 BouncycaSTLe 生成数字证书

c# - 在不影响 C# 8 可空分析器的情况下测试空值

c# - 使用 BeginInvoke 动态添加 ListView 项时出现 "The calling thread must be STA, because many UI components require this."

git - 如何将 RSA key 与远程 Git 存储库关联?

python - Python 中的 openssl_csr_get_public_key

Android - 放置 "addProvider"/"Security.insertProviderAt"代码行的最佳位置在哪里