Я расшифровываю вложения SMIME.P7M в электронных письмах. В настоящее время у меня есть следующее
EnvelopedCms envDate = new EnvelopedCms(new ContentInfo(data));
envDate.Decode(data);
RecipientInfoCollection recips = envDate.RecipientInfos;
RecipientInfo recipin = recips[0];
X509Certificate2 x509_2 = LoadCertificate2(StoreLocation.CurrentUser, (SubjectIdentifier)recipin.RecipientIdentifier);
И сертификаты загрузки выглядят так
public static X509Certificate2 LoadCertificate2(StoreLocation storeLocation, SubjectIdentifier identifier)
{
X509Store store = new X509Store(storeLocation);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certCollection = store.Certificates;
X509Certificate2 x509 = null;
X509IssuerSerial issuerSerial;
if (identifier.Type == SubjectIdentifierType.IssuerAndSerialNumber)
{
issuerSerial = (X509IssuerSerial)identifier.Value;
}
foreach (X509Certificate2 c in certCollection)
{
Console.WriteLine("{0}Valid Date: {1}{0}", Environment.NewLine, c.NotBefore);
if (c.SerialNumber == issuerSerial.SerialNumber && c.Issuer == issuerSerial.IssuerName)
{
x509 = c;
break;
}
}
if (x509 == null)
Console.WriteLine("A x509 certificate for was not found");
store.Close();
return x509;
}
Приведенный выше код получает только первого получателя RecipientInfo recipin = recips[0]; однако является ли наиболее эффективным методом получения соответствующего сертификата для прохождения каждого получателя и проверки хранилища на наличие SubjectIdentifier?
После получения правильного сертификата я использую это
X509Certificate2Collection col = new X509Certificate2Collection(x509_2);
envDate.Decrypt(col);
decData = envDate.ContentInfo.Content;
Это запрашивает PIN-код, связанный с закрытым ключом сертификата. Как я могу добавить PIN-код перед вызовом расшифровки, чтобы не было запроса?
Decrypt
в дополнение к дополнительным сертификатам, предоставленным в коллекции. - person bartonjs   schedule 21.02.2019