首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何利用PKCS 7和SHA算法在C#中创建数字签名并进行验证

如何利用PKCS 7和SHA算法在C#中创建数字签名并进行验证
EN

Stack Overflow用户
提问于 2017-07-03 07:24:45
回答 1查看 9.5K关注 0票数 1

我试图对xml文档进行数字签名,并使用带有公钥和签名文档的原始xml文件验证签名。我有一个可引用的java代码。我需要将java代码转换为C#,这里有如下的java代码:

代码语言:javascript
复制
   certList = new ArrayList<X509Certificate>();
   certList.add(signerCert);
   certStore = new JcaCertStore(certList);
   signedDataGenerator = new CMSSignedDataGenerator();
   ContentSigner sha2Signer = new JcaContentSignerBuilder("SHA512with" + privateKey.getAlgorithm()).build(privateKey);

   ignedDataGenerator.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().build()).setDirectSignature(true).build(sha2Signer, signerCert));
   signedDataGenerator.addCertificates(certStore);
   CMSSignedData sigData = signedDataGenerator.generate(new CMSProcessableFile(inputXmlFile), false);
   signedBytes = sigData.getEncoded();

我已经将java代码转换为C#,如下所示:

代码语言:javascript
复制
        X509Store my = new X509Store(StoreName.My, StoreLocation.LocalMachine);
        my.Open(OpenFlags.ReadOnly);
        // Find the certificate we’ll use to sign
        RSACryptoServiceProvider csp = null;
        foreach (X509Certificate2 cert in my.Certificates)
        {
            if (cert.Subject.Contains(certSubject))
            {
                // We found it.
                // Get its associated CSP and private key
                csp = (RSACryptoServiceProvider)cert.PrivateKey;                  
            }
        }
        if (csp == null)
        {
            throw new Exception("oppose no valid application was found");
        }
        // Hash the data
        SHA512Managed sha1 = new SHA512Managed();
        UnicodeEncoding encoding = new UnicodeEncoding();
        byte[] data = encoding.GetBytes(text);
        byte[] hash = sha1.ComputeHash(data);
        // Sign the hash
        return csp.SignHash(hash, CryptoConfig.MapNameToOID("SHA1"));

我试图转换它从两天,它是生成符号字节数组,但无法验证。在验证它正在抛出坏散列\r\n错误时,我将非常感谢您的任何帮助。我知道我在将java代码转换为C#方面有些错误。我能够验证代码,但无法对文档签名。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-03 11:21:06

我使用这样的System.Security.Cryptography.Pkcs库生成了签名

代码语言:javascript
复制
    public static byte[] Sign(byte[] data, X509Certificate2 certificate)
    {
        if (data == null)
            throw new ArgumentNullException("data");
        if (certificate == null)
            throw new ArgumentNullException("certificate");

        // setup the data to sign
        ContentInfo content = new ContentInfo(data);
        SignedCms signedCms = new SignedCms(content, false);
        CmsSigner signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, certificate);
        // create the signature
        signedCms.ComputeSignature(signer);
        return signedCms.Encode();
    }

然后像这样验证签名

代码语言:javascript
复制
  private static bool VerifySignatures(FileInfo contentFile, Stream signedDataStream)
    {
        CmsProcessable signedContent = null;
        CmsSignedData cmsSignedData = null;
        Org.BouncyCastle.X509.Store.IX509Store store = null;
        ICollection signers = null;
        bool verifiedStatus = false;
        try
        {
            //Org.BouncyCastle.Security.addProvider(new BouncyCastleProvider());
            signedContent = new CmsProcessableFile(contentFile);
            cmsSignedData = new CmsSignedData(signedContent, signedDataStream);
            store = cmsSignedData.GetCertificates("Collection");//.getCertificates();
            IX509Store certStore = cmsSignedData.GetCertificates("Collection");
            signers = cmsSignedData.GetSignerInfos().GetSigners();
            foreach (var item in signers)
            {
                SignerInformation signer = (SignerInformation)item;
                var certCollection = certStore.GetMatches(signer.SignerID);
                IEnumerator iter = certCollection.GetEnumerator();
                iter.MoveNext();
                var cert = (Org.BouncyCastle.X509.X509Certificate)iter.Current;
                verifiedStatus = signer.Verify(cert.GetPublicKey());
            }

        }
        catch (Exception e)
        {
            throw e;
        }

        return verifiedStatus;
    }

对我来说很管用

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44879897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档