Rocky Mishra
Rocky Mishra

Reputation: 105

Decrypt and verify PKCS7 message in C#

I am trying to decrypt and verify PKCS7 response I am getting from client using C#. Initially I tried Enveloping and signing my payload and followed the answer mentioned here.

Now I am getting response in PKCS7 again and having trouble in Decrypting and verifying the response.

I tried using EnvelopedCMS:

        ecms.Decode(Convert.FromBase64String(payloadContent));
        ecms.Decrypt(new X509.X509Certificate2Collection { _signerCert });
        string decodedContent = Encoding.UTF8.GetString(ecms.ContentInfo.Content);

Here _signerCert is my own certificate with private key.

I could see my required response in the decodedContent along with some of the client's information and some unknown ASCII characters. Immediate window reference from Visual studio

Does anyone know how can I achieve decryption and verification of incoming response?

Solution found:

I used SignedCms like this as suggested by @bartonjs

SignedCms signedCMS = new SignedCms();

signedCMS.Decode(ecms.ContentInfo.Content);

Upvotes: 1

Views: 1129

Answers (1)

A.M
A.M

Reputation: 129

ICryptoManager objCM = new CryptoManager();

ICryptoContext objContext = objCM.OpenContext( "", true, Missing.Value );

ICryptoMessage objMsg = objContext.CreateMessage( true );

// Obtain encryption certificate
ICryptoCert objCert = objCM.ImportCertFromFile( @"c:\path\mycert.cer" );
objMsg.AddRecipientCert( objCert );


txtResult.Text = objMsg.EncryptText("my secret phrase");

Upvotes: 1

Related Questions