Ошибка декодирования для рутокена ГОСТ3410 - 256 C# библиотека
Не получается дешифровать сообщение с рутокена ГОСТ3410 - 256, выдает следующую ошибку при использовании метода DECRYPT
Net.Pkcs11Interop.Common.Pkcs11Exception: Method C_EncryptInit returned CKR_KEY_TYPE_INCONSISTENT
at Net.Pkcs11Interop.HighLevelAPI41.Session.Encrypt(Mechanism mechanism, ObjectHandle keyHandle, Byte[] data)
at Net.Pkcs11Interop.HighLevelAPI.Session.Encrypt(Mechanism mechanism, ObjectHandle keyHandle, Byte[] data)
at SEV_Vplay.Models.LibModel.DecryptTokenMessage(String itemEncryptedMessage) in
Данные полученные методом: GetCertificateInfo
Certificate:
Data:
Version: 3 (0x2)
Signature Algorithm: GOST R 34.10-2012 with GOST R 34.11-2012 (256 bit)
Issuer: ST=Moscow,L=Moscow,O=AO Aktiv-Soft,OU=Rutoken,CN=Rutoken TEST CA GOST
Validity
Not Before: May 29 06:29:26 2024 GMT
Not After : May 29 06:29:26 2025 GMT
Subject: CN=TestCertificate,SN=QWE,GN=ZXC QWE
Subject Public Key Info:
Public Key Algorithm: GOST R 34.10-2012 with 256 bit modulus
Public key:
X:6213D698AAC219330309D2978EC8C03243593333250EAE45827D8091003624AB
Y:6AE225FD44242A7DA2FB78E3673C32D3BCB2600FDF18B90A85A62FDD1F9211EE
Parameter set: id-GostR3410-2001-CryptoPro-A-ParamSet
X509v3 extensions:
X509v3 Key Usage:
Digital Signature
X509v3 CRL Distribution Points:
Full Name:
URI:http://ra.rutoken.ru/root_certs/gost.crl
Authority Information Access:
CA Issuers - URI:http://ra.rutoken.ru/root_certs/caGost256.crt
X509v3 Authority Key Identifier:
keyid:F3:D9:D9:0D:6C:3F:B0:10:1D:01:BB:B4:B1:01:56:99:04:47:96:72
DirName:/ST=Moscow/L=Moscow/O=AO Aktiv-Soft/OU=Rutoken/CN=Rutoken TEST CA GOST
serial:96:81:17:07:20:FD:D0:C9
X509v3 Subject Key Identifier:
79:4E:65:EA:AF:23:A1:22:1B:6B:6A:ED:C5:38:E1:10:40:0D:8E:26
Signature Algorithm: GOST R 34.10-2012 with GOST R 34.11-2012 (256 bit)
Signature Value:
f2:57:6a:2f:26:92:00:d3:c3:2a:ba:50:2d:9c:95:43:a7:2f:
34:d1:b3:b0:fe:86:74:11:ef:b3:c7:cf:6a:28:75:18:f3:07:
e3:7b:ae:ed:2f:ab:86:3b:06:21:13:98:4a:c9:b4:87:7f:7b:
2a:4f:84:7e:1b:09:4c:5e:90:71
Код в котором происходит ошибка
public void DecryptTokenMessage(string itemEncryptedMessage)
{
// The module is already initialized to do GetInstance
using (Pkcs11 pkcs11Module = new Pkcs11(RutokenPkcs11Interop.Settings.RutokenEcpDllDefaultPath, AppType.MultiThreaded))
{
Slot selectedSlot = null;
var slots = pkcs11Module.GetSlotList(SlotsType.WithTokenPresent);
selectedSlot = slots[0];
var session = selectedSlot.OpenSession(SessionType.ReadWrite);
session.Login(CKU.CKU_USER, "12345678");
List<ObjectAttribute> privateKeyAttributesWithoutLabel = new List<ObjectAttribute>();
privateKeyAttributesWithoutLabel.Add(new ObjectAttribute(CKA.CKA_CLASS, CKO.CKO_PRIVATE_KEY));
privateKeyAttributesWithoutLabel.Add(new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_GOSTR3410));
List<ObjectHandle> foundPrivateKeysWithoutLabelGost3410 = session.FindAllObjects(privateKeyAttributesWithoutLabel);
Console.WriteLine("Total private Key found : " + foundPrivateKeysWithoutLabelGost3410.Count); //1
List<ObjectAttribute> privateKeyAttributesDes = new List<ObjectAttribute>();
privateKeyAttributesWithoutLabel.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));
privateKeyAttributesWithoutLabel.Add(new ObjectAttribute(CKA.CKA_KEY_TYPE, CKK.CKK_DES3));
List<ObjectHandle> foundPrivateKeysDes = session.FindAllObjects(privateKeyAttributesDes);
Console.WriteLine("Total private Key found : " + foundPrivateKeysDes.Count); //6
var data = "hello";
var data64 = Base64Converter.Base64Encode(data);
var binData = Convert.FromBase64String(data64);
byte[] encryptedData = null;
try
{
var mechanismKeyPairGen = new Mechanism(CKM.CKM_DES3_CBC, new byte[8]);
encryptedData = session.Encrypt(mechanismKeyPairGen, foundPrivateKeysDes.First(), binData);
var decrypted = session.Decrypt(mechanismKeyPairGen, foundPrivateKeysDes.First(), encryptedData);
var decryptedData = decrypted.ToArray();
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}