Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用RsaCng作为SignedXml的SigningKey?_C#_Cryptography_.net 4.5 - Fatal编程技术网

C# 如何使用RsaCng作为SignedXml的SigningKey?

C# 如何使用RsaCng作为SignedXml的SigningKey?,c#,cryptography,.net-4.5,C#,Cryptography,.net 4.5,我试图遵循下面文章中的解决方案,以便能够使用CNG私钥作为我的SignedXml的签名密钥 CustomSignedXml派生自SignedXml。然而,当我这样做并调用ComputeSignature方法时,我得到了异常 方法不受支持。at System.Security.Cryptography.RSA.DecryptValue(字节[]rgb) 位于System.Security.Cryptography.Xml.SignedXml.ComputeSignature() 正如堆栈跟踪所

我试图遵循下面文章中的解决方案,以便能够使用CNG私钥作为我的SignedXml的签名密钥

CustomSignedXml派生自SignedXml。然而,当我这样做并调用ComputeSignature方法时,我得到了异常

方法不受支持。at System.Security.Cryptography.RSA.DecryptValue(字节[]rgb) 位于System.Security.Cryptography.Xml.SignedXml.ComputeSignature()

正如堆栈跟踪所表明的那样,方法不受支持,是从DecryptValue中抛出的,我看到它是RSA基类中一个不推荐使用的方法。我无法重写它以使用RSACng.Decrypt,因为RSACng是一个密封类


我这里缺少什么?

在CLR安全问题页面中,SignedXml仍然缺少对RSACng的支持。但是,该评论将我引向了4.6.2预览版。您可以使用GetRSAPrivateKey()获取密钥(我认为它在4.6.1中已经可用),但是这次我没有得到“不支持的方法”异常。因此,最好的解决方案是将框架更新为4.6.2,下面的代码可以工作

private CustomSignedXml CreateSignedXml()
    {
        var signedXml = new CustomSignedXml(_documentToSign)
        {
            SigningKey = _signingCertificate.GetRSAPrivateKey(),
            KeyInfo = CreateKeyInfo()
        };

        Reference reference = new Reference("");
        reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
        reference.DigestMethod = SecurityAlgorithms.Sha256Digest; // SignedXml.XmlDsigSHA256Url;


        signedXml.SignedInfo.CanonicalizationMethod = SecurityAlgorithms.ExclusiveC14n;
        signedXml.SignedInfo.SignatureMethod = SecurityAlgorithms.RsaSha256Signature; // SignedXml.XmlDsigRSASHA256Url
        signedXml.AddReference(reference);

        return signedXml;
    }