Статус: Новичок
Группы: Участники
Зарегистрирован: 02.12.2014(UTC) Сообщений: 8 Сказал(а) «Спасибо»: 2 раз Поблагодарили: 1 раз в 1 постах
|
Здравствуйте. Пытаюсь расшифровать XML с помощью класса System.Security.Cryptography.Xml.EncryptedXml. При выполнении метода DecryptDocument() возникает исключение "The parameter is incorrect". Ситуация воспроизводится даже в тестовом примере, код которого приведен ниже: Код:
using System;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Xml;
using System.Xml;
namespace EncryptedXmlTest
{
class Program
{
static void Main()
{
var document = new XmlDocument();
document.LoadXml("<data>Some data.</data>");
X509Certificate2 certificate; // Получение сертификата, например, из хранилища машины
Encrypt(document, certificate);
Console.WriteLine("Encrypted XML:");
Console.WriteLine(document.OuterXml);
Decrypt(document);
Console.WriteLine("Decrypted XML:");
Console.WriteLine(document.OuterXml);
Console.ReadLine();
}
private static void Encrypt(XmlDocument document, X509Certificate2 certificate)
{
var encryptedXml = new EncryptedXml();
var encryptedElement = encryptedXml.Encrypt(document.DocumentElement, certificate);
EncryptedXml.ReplaceElement(document.DocumentElement, encryptedElement, false);
}
private static void Decrypt(XmlDocument document)
{
var encryptedXml = new EncryptedXml(document);
encryptedXml.DecryptDocument(); // System.Security.Cryptography.CryptographicException, CryptoPro.Sharpei.Base.dll, Additional information: The parameter is incorrect.
}
}
}
Пример зашифрованного сообщения: Код:
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#"><EncryptionMethod Algorithm="urn:ietf:params:xml:ns:cpxmlsec:algorithms:gost28147" /><KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"><EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#"><EncryptionMethod Algorithm="urn:ietf:params:xml:ns:cpxmlsec:algorithms:transport-gost2001" /><KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"><X509Data><X509Certificate>MIIHPTCCBuqgAwIBAgIQAc8sarDN2jAAAAABBbgD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NCQ0KYiMTAwLgYDVQQLDCfQo9C00L7RgdGC0L7QstC10YDRj9GO0YnQuNC5INGG0LXQvdGC0YAxKDAmBgNVBAwMH9CQ0LTQvNC40L3QuNGB0YLRgNCw0YLQvtGAINCj0KYxKDAmBgNVBAMMH9CQ0LTQvNC40L3QuNGB0YLRgNCw0YLQvtGAINCj0KaCEAHOhFAQ4KFQAAAD1QW4AAIwCgYGKoUDAgIDBQADQQAtHbf6W24e9ww1VFlGULuRimNcRWP8EA5olpxEfezAUJrwwsuSRTUJq0kk4+ZJY6TcijE1/Zf/7wh4b2I9hi4w</X509Certificate></X509Data></KeyInfo><CipherData><CipherValue>MIGkMCgEIHF9Nam4Mqp0gJpKJZl0W91cAIst8oxEUUYB9l2HIlUQBATmyxYaoHgGByqFAwICHwGgYzAcBgYqhQMCAhMwEgYHKoUDAgIkAAYHKoUDAgIeAQNDAARAm1Z7IU6Iv21qvvsExhkuJ8XqU+iXfckcopdFWWOt6SfepdXhwEMroGNvvgkX6DEn/ulf4rhQTid7EQxNPFNefAQIqCcPiv1eaZQ=</CipherValue></CipherData></EncryptedKey></KeyInfo><CipherData><CipherValue>eXrnQpz06NhNTRNNJ7x3t8OSXdn0xjdH19qzcP6oOQw=</CipherValue></CipherData></EncryptedData>
Стек исключения: Код:
at CryptoPro.Sharpei.COMCryptography.SetKeyParamDw(SafeKeyHandleCP hKey, Int32 param, Int32 dwValue)
at CryptoPro.Sharpei.CPCryptoAPITransform..ctor(Int32 cArgs, Int32[] rgArgIds, Object[] rgArgValues, SafeKeyHandleCP hKey, PaddingMode padding, CipherMode cipherChainingMode, Int32 blockSize, CPCryptoAPITransformMode encDecMode)
at CryptoPro.Sharpei.Gost28147CryptoServiceProvider._NewEncryptor(SafeKeyHandleCP hKey, CipherMode mode, Byte[] rgbIV, Int32 feedbackSize, CPCryptoAPITransformMode encryptMode)
at CryptoPro.Sharpei.Gost28147CryptoServiceProvider.CreateDecryptor()
at System.Security.Cryptography.Xml.EncryptedXml.DecryptData(EncryptedData encryptedData, SymmetricAlgorithm symmetricAlgorithm)
at System.Security.Cryptography.Xml.EncryptedXml.DecryptDocument()
at EncryptedXmlTest.Program.Decrypt(XmlDocument document)
at EncryptedXmlTest.Program.Main()
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Самое интересное. Если использовать RSA-сертификат (не ГОСТ 3410), все работает отлично. Заранее спасибо за ответы!
|