How to Use RSAEncryption to create PKCS7/CMS with SHA1 digest?

断了今生、忘了曾经 提交于 2019-12-10 10:48:39

问题


I create a pkcs7 block,and can verify myself, but the result is not the same with my partner who use OpenSSL.The p7 block I create cannot verify by my partner.

we check the code carefully, just find the code which cannot find counterpart in c#,

OPENSSL:

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

Here is the code us C# in .net 4.0, can everyone know that how to Use RSAEncryption in p7?

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;
        }

来源:https://stackoverflow.com/questions/8584280/how-to-use-rsaencryption-to-create-pkcs7-cms-with-sha1-digest

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!