如何使用RSAEncryption创建带有SHA1摘要的PKCS7 / CMS?

时间:2011-12-21 02:03:51

标签: c# rsa pkcs#7

我创建了一个pkcs7块,可以验证自己,但结果与使用OpenSSL的伙伴不一样。我创建的p7块无法由我的伙伴验证。

我们仔细检查代码,找到在c#中找不到对应代码的代码,

OPENSSL:

 signInfo->digest_enc_alg->algorithm=OBJ_nid2obj(NID_rsaEncryption);

以下是我们C#in .net 4.0中的代码,大家都知道如何在 p7 中使用 RSAEncryption 吗?

public static string Sign(byte[] data, X509Certificate2 certificate)
        {
            if (data == null)
                throw new ArgumentNullException("data");
            if (certificate == null)
                throw new ArgumentNullException("certificate");

            //1 setup the data to sign           
            Oid digestOid = new Oid("1.2.840.113549.1.7.2");//pkcs7 signed 
            ContentInfo content = new ContentInfo(digestOid, data);           
            try
            {      
                //2,SignerCms
                SignedCms signedCms = new SignedCms(content, true); //detached = true           

                //3. CmsSigner
                CmsSigner signer = new CmsSigner(SubjectIdentifierType.IssuerAndSerialNumber, certificate);       

                signer.DigestAlgorithm = new Oid("1.3.14.3.2.26");//sha1

                //4.create signature
                signedCms.ComputeSignature(signer);          

                //5,to Base64
                byte[] signEnv = signedCms.Encode();                
                return  Convert.ToBase64String(signEnv);               

            }catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return null;
        }

0 个答案:

没有答案