我正在尝试更新执行数字签名的功能,我想从SHA1切换为SHA256
 这是当前功能:

private byte[] zSignData(Byte[] msg, X509Certificate2 signerCert)
{
    ContentInfo contentInfo = new ContentInfo(msg);
    SignedCms signedCms = new SignedCms(contentInfo, false);
    CmsSigner cmsSigner = new CmsSigner(signerCert);

    cmsSigner.DigestAlgorithm = new Oid("1.3.14.3.2.26"); //SHA1

    signedCms.ComputeSignature(cmsSigner, false);

    return signedCms.Encode();
}


这个功能很好用

为了更新为SHA256,我更改了

cmsSigner.DigestAlgorithm = new Oid("1.3.14.3.2.26"); //SHA1




cmsSigner.DigestAlgorithm = new Oid("2.16.840.1.101.3.4.2.1");//SHA256


但是在

signedCms.ComputeSignature(cmsSigner, false);


我得到以下异常


  System.Security.Cryptography.CryptographicException
     消息=存在内部错误。


有人有建议吗?

我使用VS2010 Professional 64和Win7 Professional 64

最佳答案

我相信CAPIBase中有一个拼写错误:

internal const string szOID_OIWSEC_SHA256 = "2.16.840.1.101.3.4.1";
internal const string szOID_OIWSEC_SHA384 = "2.16.840.1.101.3.4.2";
internal const string szOID_OIWSEC_SHA512 = "2.16.840.1.101.3.4.3";


应该:

internal const string szOID_OIWSEC_SHA256 = "2.16.840.1.101.3.4.2.1";
internal const string szOID_OIWSEC_SHA384 = "2.16.840.1.101.3.4.2.2";
internal const string szOID_OIWSEC_SHA512 = "2.16.840.1.101.3.4.2.3";


关于此的更多讨论在这里:


http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/217776fa-5dcd-41a6-a800-b401509fbbf0/#fbae9811-a5a2-418f-b049-dcbabf18431a
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/0cc90bdd-35f9-4a7d-8025-89f7ea9f9704/#deb21fba-1171-4eee-81ea-6de294abbeeb


blogs.msdn.com/b/alejacma/archive/2012/08/02/10018922.aspx

Todd,是什么让您相信OID 2.16.840.1.101.3.4.2.1不是SHA256?那么正确的OID是什么?

关于c# - 从SHA1到SHA256的数字标牌,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5704906/

10-13 07:39