为什么我收到"指定的无效算法"异常

sco*_*ott 17 c# cryptography

这是我的代码.

X509Certificate pXCert = new X509Certificate2(@"keyStore.p12", "password");
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)pXCert.PrivateKey;
string id = CryptoConfig.MapNameToOID("SHA256");
return csp.SignData(File.ReadAllBytes(filePath), id);
Run Code Online (Sandbox Code Playgroud)

在最后一行,我得到了例外:

System.Security.Cryptography.CryptographicException"指定的算法无效."

我究竟做错了什么?

更新:

id = 2.16.840.1.101.3.4.2.1

Pat*_*ins 13

.NET代码或您提供的CSP代码没有问题.

您的问题是CSP不支持SHA 256.您可以在此处获取更多信息

  • http://hintdesk.com/c-how-to-fix-invalid-algorithm-specified-when-signing-with-sha256/ (2认同)

小智 12

对于 dot net framework 4.7.0 或更高版本不采用 sha1,因此在应用程序启动时配置以下内容。它对我来说很好用。

 AppContext.SetSwitch("Switch.System.Security.Cryptography.Xml.UseInsecureHashAlgorithms", true);
 AppContext.SetSwitch("Switch.System.Security.Cryptography.Pkcs.UseInsecureHashAlgorithms", true);
Run Code Online (Sandbox Code Playgroud)


Yas*_*sel 8

您可能是在将应用程序从 .NET Framework 4.7 和更早版本迁移到 4.7.1 或更高版本时来到这里的。
如果您遇到异常System.Security.Cryptography.CryptographicException: Invalid algorithm specified.,原因是针对 .NET Framework 4.7.1 和更高版本的应用程序的默认 SignedXML 和 SignedXMS 算法更改为 SHA256(来自 Microsoft .NET 迁移指南

在该指南中,您还将找到解决方案:

对于面向 .NET Framework 4.7.1 及更高版本的应用程序,如果不希望使用 SHA256,您可以通过将以下配置开关添加到应用程序配置文件的运行时部分来将默认值恢复为 SHA1:

<AppContextSwitchOverrides value="Switch.System.Security.Cryptography.Xml.UseInsecureHashAlgorithms=true;  
                  Switch.System.Security.Cryptography.Pkcs.UseInsecureHashAlgorithms=true" />
Run Code Online (Sandbox Code Playgroud)

但这可能并不总是有效,尤其是对于 Web 应用程序,正如您可以在这篇博客文章中阅读的那样,幸运的是,还有答案。只需要在其中添加一些行Application_Start

protected void Application_Start(object sender, EventArgs e)
{
   [...]
   AppContext.SetSwitch("Switch.System.Security.Cryptography.Xml.UseInsecureHashAlgorithms", true);
   AppContext.SetSwitch("Switch.System.Security.Cryptography.Pkcs.UseInsecureHashAlgorithms", true);
}
Run Code Online (Sandbox Code Playgroud)


cma*_*tin 6

请注意,我使用SHA512,但SHA256将使用以下示例:

"指定的算法无效"让我永远想弄清楚,我几乎尝试了一切.向Gonzalo Gallotti发布道具,发布了帮助我的代码链接.我评论了我的代码以显示每个步骤正在做什么.注意:如果没有在代码示例下面发布的正确生成的证书,此代码将无法运行:

public void GetCertificate() {

    // Get the Machine Cert Store
    var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

    string alg = CryptoConfig.MapNameToOID("SHA512");

    // Open the cert store
    store.Open(OpenFlags.ReadWrite);

    // Loop through each certificate within the store
    foreach (X509Certificate2 myCert in store.Certificates)
    {
        // Get the certificate we are looking for
        if (myCert.IssuerName.Name.Contains("CN=YourSite"))
        {
            // Check if the certificate has a private key
            if (myCert.HasPrivateKey)
            {
                // Get your custom signature as a string
                string mySignature = GetSignatureString();

                // Convert signature to byte array
                byte[] originalData = Encoding.UTF8.GetBytes(mySignature);

                // Create RSA provider from private key
                RSACryptoServiceProvider rsaProvider = (RSACryptoServiceProvider)myCert.PrivateKey;

                // Sign the signature with SHA512
                byte[] signedSignature = signedSignature = rsaProvider.SignData(originalData, alg);

                if (rsaProvider.VerifyData(originalData, alg, signedSignature))
                {
                    // Signature is verified Do Stuff
                }
                else
                {
                    throw new Exception("The data does not match the signature.");
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

接下来 - 证书必须是SHA512并使用具有SHA512功能的CSP(加密服务提供程序).以下是CSP及其功能的列表.如果您寻找SHA512,您将找到"Microsoft增强型RSA和AES加密提供商".默认情况下,生成证书不会使用此功能(至少在Windows中),因此您必须在创建证书时指定它.

创建私钥和证书 - 此步骤将询问您问题,州,地区等.

openssl req -x509 -nodes -sha512 -newkey rsa:2048 -keyout 512key.pem -out 512cert.pem -days 3650
Run Code Online (Sandbox Code Playgroud)

使用Microsoft增强RSA和AES加密提供程序创建要导入证书存储区的PFX文件:

openssl pkcs12 –export –in 512cert.pem –inkey 512key.pem –CSP “Microsoft Enhanced RSA and AES Cryptographic Provider” –out 512pfx.pfx
Run Code Online (Sandbox Code Playgroud)