Пытаюсь расшифровать сообщение таким образом:
IntPtr store = IntPtr.Zero;
IntPtr phProv = IntPtr.Zero;
var res = Crypto.CryptAcquireContext(ref phProv,
@"FAT12\24997833\le-5c699.000\2983",
"Crypto-Pro GOST R 34.10-2001 Cryptographic Service Provider",
75,
Crypto.CRYPT_SILENT);
// открываем хранилище
store = Crypto.CertOpenStore(Crypto.CERT_STORE_PROV_SYSTEM,
Crypto.MY_ENCODING_TYPE,
phProv,
Crypto.CERT_SYSTEM_STORE_CURRENT_USER,
"MY");
var dpair = new Crypto.CRYPT_DECRYPT_MESSAGE_PARA
{
dwFlags = Crypto.CRYPT_MESSAGE_SILENT_KEYSET_FLAG,
dwMsgEncodingType = Crypto.MY_ENCODING_TYPE,
cCertStore = 1,
rghCertStore = new[] { store }
};
dpair.cbSize = Marshal.SizeOf(typeof (Crypto.CRYPT_DECRYPT_MESSAGE_PARA));
int dlength = 0;
// получаем размер сообщения
res = Crypto.CryptDecryptMessage(ref dpair, encRes, encRes.Length, null, ref dlength, IntPtr.Zero);
var dresult = new byte[dlength];
// расшифровка
res = Crypto.CryptDecryptMessage(ref dpair, encRes, encRes.Length, dresult, ref dlength, IntPtr.Zero);
Все функции возвращают положительный результат, кроме последней CryptDecryptMessage, которая говорит, что
"Cannot find the certificate and private key to use for decryption", хотя ключ и сертификат есть. Подскажите в чем дело.
Dll импорты CryptoAPI такие:
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool CryptAcquireContext(
ref IntPtr hProv,
String pszContainer,
String pszProvider,
Int32 dwProvType,
Int32 dwFlags
);
[DllImport("Crypt32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CertOpenStore(
Int32 lpszStoreProvider,
Int32 dwMsgAndCertEncodingType,
IntPtr hCryptProv,
Int32 dwFlags,
String pvPara
);
[DllImport("Crypt32.dll", SetLastError = true)]
public static extern bool CryptDecryptMessage(
ref CRYPT_DECRYPT_MESSAGE_PARA pDecryptPara,
byte[] pbEncryptedBlob,
int cbEncryptedBlob,
[In, Out] byte[] pbDecrypted,
ref int pcbDecrypted,
IntPtr ppXchgCert);