Использую нугет для Windows
using System.Security.Cryptography.Pkcs;
using System.Security.Cryptography.X509Certificates;
using System.Text;
LibCore.Initializer.Initialize();
var dataToSign = "ee34f56f-48c9-4a74-87c1-addcda61df38";
var certificateSerial = "046a396e008eaf628c49780521d8f62d6c";
var bytes = Encoding.UTF8.GetBytes(dataToSign);
var contentInfo = new ContentInfo(bytes);
var signedCms = new SignedCms(contentInfo, true);
byte[] signature = null;
using (var myStore = new X509Store(StoreName.My, StoreLocation.LocalMachine))
{
myStore.Open(OpenFlags.ReadOnly);
var certColl = myStore.Certificates.Find(X509FindType.FindBySerialNumber, certificateSerial, false);
var cert = certColl[0];
var cmsSigner = new CmsSigner(cert);
signedCms.ComputeSignature(cmsSigner);
signature = signedCms.Encode();
}
ContentInfo contentInfoVerify = new ContentInfo(bytes);
// Создаем SignedCms для декодирования и проверки.
SignedCms signedCmsVerify = new SignedCms(contentInfoVerify, true);
// Декодируем подпись
signedCmsVerify.Decode(signature);
// Проверяем подпись
signedCmsVerify.CheckSignature(true);
Сертификат находит, подпись формирует, но проверка подписи не проходит.
System.Security.Cryptography.CryptographicException: Invalid signature.
at LibCore.Security.Cryptography.Pkcs.Detours.SignerInfoDetour.Prefix(X509Certificate2Collection extraStore, X509Certificate2 certificate, Boolean verifySignatureOnly, SignerInfo __instance)
at System.Security.Cryptography.Pkcs.SignerInfo.Verify_Patch1(SignerInfo this, X509Certificate2Collection extraStore, X509Certificate2 certificate, Boolean verifySignatureOnly)
at System.Security.Cryptography.Pkcs.SignerInfo.CheckSignature(X509Certificate2Collection extraStore, Boolean verifySignatureOnly)
at System.Security.Cryptography.Pkcs.SignedCms.CheckSignatures(SignerInfoCollection signers, X509Certificate2Collection extraStore, Boolean verifySignatureOnly)
at System.Security.Cryptography.Pkcs.SignedCms.CheckSignature(X509Certificate2Collection extraStore, Boolean verifySignatureOnly)
at System.Security.Cryptography.Pkcs.SignedCms.CheckSignature(Boolean verifySignatureOnly)
at Program.<Main>$(String[] args) in C:\Work\Vostok.EPGU\vostok.epgu\api\Signer.Console\Program.cs:line 37
Подскажите пожалуйста в чем может быть проблема.
Отредактировано пользователем 7 февраля 2023 г. 15:39:36(UTC)
| Причина: Не указана