I'm facing a new situation involving a program, written in .NET Framework 4.0/C#, which has to encode/decode and sign messages to be sent/received in a particular kind of WAN. This is the current scenario, which is working fine. We have a .p12 file (which contains the sender certificate) and a .cer file (which contains the receiver certificate), which are both installed in the pc. The scenario is working using SHA1 and PKCS#7 at 1024 bit, so the following piece of code works:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography;
using System.Windows;
using System.Windows.Controls;
using System.Collections;
private static string senderCert = "sendername"; //the name registered in .p12 file
private static string receiverCert = "receivername"; //the name registered in .cer file
/// ///verify that the receiving message is signed
///and returns the data without sign
///
/// dataflow with sign
///
public static byte[] Verify(this byte[] signedData)
{
X509Certificate2 certPub = GetReceiverCert();
if (certPub == null) return null;
ContentInfo decodeContentInfo = new ContentInfo(signedData);
SignedCms decodeCMS = new SignedCms(decodeContentInfo, false);
try
{
//decode the message, if it isn't signed, raise an exception
decodeCMS.Decode(signedData);
SignerInfo signerInfo = decodeCMS.SignerInfos\[0\];
X509Certificate2Collection certCollection = new
X509Certificate2Collection(certPub);
return decodeCMS.ContentInfo.Content;
}
catch (CryptographicException err)
{
Logger.Log(err);
return null;
}
}
/// /// Returns the certificate used to sign the sending messages
///
///
private static X509Certificate2 GetSenderCert()
{
//Open the personal certificates folder
X509Store storeMy = new X509Store(StoreName.My, StoreLocation.CurrentUser);
storeMy.Open(OpenFlags.ReadOnly);
//find the proper certificate
X509Certificate2Collection certColl = storeMy.Certificates.Find
(X509FindType.