Как реализовать. Net код команды для утилиты Openssl:
openssl smime -sign -in req. xml -nointern -nodetach -nocerts -nochain -outform PEM -out req_signed .txt -signer you.cer -inkey private.key -passin pass: HIDDEN
начальная строка:
<?xml version="1.0" encoding="utf-16"?>
<root>
<test atr1="1" atr2="2"/>
</root>
пример выхода (случайные символы):
-----BEGIN PKCS7-----
MIIDGYAJKoIZvhNcQAcCoIIDCCTACwUCAQExDAzBNglhggkBZQMEAEgAFBD/Bkg
hki9G0wBBGwgcgRwQ29uGdVudC1UXBlO0BZXh03LsBWYluDQoNCjw/e1GIsZH
lcnNpb294jIEuCMgIZW5jb2Rpbmc9In0VZix0iN/IPg0KPHvJb3Q+DQPJRHclQ3g
XYyRMT0iMSIgYXyRMj0iiMIvgPK0PC9y2b90jPCGmAwwggoJgABEMFoQwEzMSBAG
gCSmJoTm8ixkARWknA1JRMwcQFYKZCImZPyLGQBGRHYeFWbt52leETUMBIAG1U
EAxMLkTJDyTBZTSBJbnQCExcAAEH3mR4gPUdDU8QAAAAATcwcDQJYIYIZWADUBAIBB
QCggwQGAJYoKZIvhNcQADkMQGsCSSGbID3QEAHTABckgkhG9w0CBQUxDxcN
jMAwjMA5jMzMDN2UWjvAgBqkkhiG9w0BCQQIxgQLgdKuqMqsBylKuIFfAZQdC6n2
hOG7fJfYunRKXGR6/ULewQYJKoZIhvNcAQPkMWwwAjBLlghggkZBMQAESowCYwJ
YIZIWAUDABEWAMGsWCCGSAFlAwQBjAAKBgqgkhG9w0BDzAOBggqhki9G0wADIgC
AIwADQIYKoZIvhNcwACIUAAwBwYFKw4DAgcwQDYIKoZIhvcNwAICSAwgDQYJoKZIh
cvNAQEBBQAEggEAEse9SsBSFtL4ETWsZP1iGJntAE5UtlrmKxZC+dcPds9VPmDm
8yyH8HPubOtBMMVkNN2w+HeTCg3+8IlIKHXQggwuzzKb2Nq/Fw+TW0pAI3dsnQ
pjC0OEoIk8zSVcPjhHt4VOngEA7mzTvTBk0bC7bLqG+CqjX1MCMsJ6kk2dXXG
Tp0+P3CJGrEd5pityOIdCRm5brRfFW4OK+2slRIyn232XBk1YDUjfjUly9GZJWvG
Baiy58e5ySv8gH2QUZgI39z0gCnwERg4dlXpDu31oFvzTPXgFtfZwfiY1QViOeF1
t8lM87bm+qEwgT9V1PsPM0e+eLUgvfJJhKOg3==
-----END PKCS7-----
Я пытался реализовать, как описано в msdn, но в итоге получаю неверные данные. Почему? сертификат включает в себя закрытый ключ.
var store = new X509Store(StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2 cert = store.Certificates.Find(X509FindType.FindByThumbprint, "000F00003DF12000000300005C82600039BDF000", false)[0];
XDocument docreq =
new XDocument(new XElement("root",
new XElement("test",
new XAttribute("atr1", "1"),
new XAttribute("atr2", "2"))));
CmsSigner _signer = new CmsSigner(cert);
ContentInfo _contentInfo = new ContentInfo(System.Text.Encoding.UTF8.GetBytes(docreq.ToString()));
EnvelopedCms envelopedCms = new EnvelopedCms(_contentInfo);
envelopedCms.Encrypt(new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, cert));
var data = envelopedCms.Encode();