"Буфер, предоставленный функции, был слишком маленьким" криптографическое исключение с подписью сертификата
Я пытаюсь подписать файл сертификатом, хранящимся в хранилище сертификатов. Сертификат хранится в хранилище локального компьютера.
Во время вызова
signedCms.ComputeSignature(cmsSigner, false);
Я получаю следующее CryptographicException: "Буфер, предоставленный функции, был слишком мал"
ОБНОВИТЬ
Пример кода:
using System;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
using System.Text;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
const string ENC_STR = "this is my message to encrypt цшер ÄÄÄ";
var oid = new System.Security.Cryptography.Oid("sha256");
X509Store store = new X509Store("MY", StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
X509Certificate2Collection cers = store.Certificates.Find(X509FindType.FindBySubjectName, args[0], false);
if (cers.Count > 0)
{
Console.WriteLine(" {0} certificates are found", cers.Count);
var cer = cers[0];
Console.WriteLine("Certificate {0} is found", cer.Subject);
byte[] dataToSign = Encoding.ASCII.GetBytes(ENC_STR);
System.Security.Cryptography.Pkcs.ContentInfo contentInfo = new System.Security.Cryptography.Pkcs.ContentInfo(dataToSign);
System.Security.Cryptography.Pkcs.SignedCms signedCms = new System.Security.Cryptography.Pkcs.SignedCms(contentInfo, true);
System.Security.Cryptography.Pkcs.CmsSigner cmsSigner = new System.Security.Cryptography.Pkcs.CmsSigner(cer);
cmsSigner.IncludeOption = X509IncludeOption.None;
cmsSigner.DigestAlgorithm = oid;
try
{
signedCms.ComputeSignature(cmsSigner, false);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
//return the signed message
byte[] signatureValue = signedCms.Encode();
Console.WriteLine("Signature is: {0} ", Encoding.ASCII.GetString(signatureValue));
}
else
{
Console.WriteLine("No certificates found");
}
store.Close();
}
}
}
Возможно эта информация будет полезна:
- Сертификат имеет символы "аао" в общем названии и организации
- Исключение не выдается, если алгоритм хеширования SHA1. Для ша256 (и других) исключение брошено.
- Если я портирую определенную часть кода на ядро .net или стандарт.net, исключение не выдается.