[英]CKR_BUFFER_TOO_SMALL = 0x00000150
I want to PInvoke C_Encrypt() "pkcs#11" from a .dll : 我想从.dll PInvoke C_Encrypt()“ pkcs#11”:
[DllImport("cryptoki.dll", SetLastError = true)]
private static extern UInt32 C_Encrypt(CK_SESSION_HANDLE hSession,IntPtr pData,CK_ULONG ulDataLen,out IntPtr pEncryptedData,out CK_ULONG pulEncryptedData);
/*
.... Main
in which I initialize the encryption parametrs with C_EncyptInit
*/
CK_BYTE[] text = new CK_BYTE[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08, 0x09 };
System.UInt32 t, tt = (System.UInt32)text.Length;
IntPtr pdata = Marshal.AllocHGlobal(text.Length);
Marshal.Copy(text, 0, pdata, text.Length);
IntPtr chif = IntPtr.Zero;
tt = (System.UInt32)Marshal.SizeOf(pdata);
rv = C_Encrypt(h, pdata, tt, out chif, out t);
help please 请帮助
There's a variety of different problems here. 这里有各种各样的问题。
out
parameters. out
参数。 The C_Encrypt
function will write the encrypted data to those parameters, but you need to allocate and pass them yourself. C_Encrypt
函数会将加密的数据写入这些参数,但是您需要自己分配和传递它们。 chif
, and then pass the size that you allocated for chif
as the final param t
. chif
分配数据,然后将为chif
分配的大小作为最终参数t
传递。 This is the root cause of the error that you're seeing. tt
and t
somewhere, since you assign to tt
twice. tt
和t
,因为您两次分配了tt
。 I resolved the problem By my self: 我自己解决了这个问题:
[DllImport("D:/Program Files/Eracom/ProtectToolkit C SDK/bin/sw/cryptoki.dll", SetLastError = true)]
private static extern UInt32 C_Encrypt(CK_SESSION_HANDLE hSession, CK_BYTE[] pData, CK_ULONG ulDataLen, CK_BYTE[] pEncryptedData,ref CK_ULONG pulEncryptedData);
enjoy 请享用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.