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