Я хотел бы подписать файл с сертификатом. Я написал следующий код, но я получил «Ошибка содержимого файла», а также я всегда спрашивал секретный ключ. Что я сделал не так? Как я могу отправить закрытый ключ? Спасибо всем.Подпишите файл с X509Certificate2 и личным ключом
string cSerial = "0C4744041F40B761322124EB691C5F32";
//Find my certificate with serial
X509Store my = new X509Store(StoreName.My, StoreLocation.CurrentUser);
my.Open(OpenFlags.ReadOnly);
System.Security.Cryptography.RSACryptoServiceProvider csp = null;
foreach (X509Certificate2 cert in my.Certificates)
{
if (cert.SerialNumber.Trim() == cSerial)
{ csp = (System.Security.Cryptography.RSACryptoServiceProvider)cert.PrivateKey; }
}
//Here i have the certificate, it's ok.
System.Security.Cryptography.SHA1Managed sha1 = new System.Security.Cryptography.SHA1Managed();
UnicodeEncoding encoding = new UnicodeEncoding();
//////////byte[] data = encoding.GetBytes("test.xml");
byte[] data = File.ReadAllBytes("test.xml")
byte[] hash = sha1.ComputeHash(data);
byte[] aa = csp.SignHash(hash, System.Security.Cryptography.CryptoConfig.MapNameToOID("SHA1"));
File.WriteAllBytes("text.p7m", aa);
my.Close();
UnicodeEncoding.GetBytes не читает файл. Он просто кодирует байты в строке «text.xml» в массив байтов. – Kevin
@Kevin, я редактирую свой источник, но результат не меняется ....... спасибо. – Alexander
Используйте отладчик и узнайте, какая строка создает ошибку. – Kevin