开发者

CKR_BUFFER_TOO_SMALL = 0x00000150

开发者 https://www.devze.com 2023-03-06 01:21 出处:网络
I want to PInvoke C_Encrypt() \"pkcs#11\" from a .dll : [DllImport(\"cryptoki.dll\", SetLastError = true)]

I want to PInvoke C_Encrypt() "pkcs#11" from a .dll :

[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.

  1. Your P/Invoke signature is wrong. The final two parameters are not out parameters. The C_Encrypt function will write the encrypted data to those parameters, but you need to allocate and pass them yourself.
  2. You need to allocate data for chif, and then pass the size that you allocated for chif as the final param t. This is the root cause of the error that you're seeing.
  3. Nit: Your variable names are confusing, and you seem to have mixed up tt and t somewhere, since you assign to tt twice.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消