CKR_BUFFER_TOO_SMALL = 0x00000150

0

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

c#
pinvoke
asked on Stack Overflow May 16, 2011 by loupoo • edited May 16, 2011 by JSBձոգչ

2 Answers

0

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.
answered on Stack Overflow May 16, 2011 by JSBձոգչ
0

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

answered on Stack Overflow May 19, 2011 by loupoo • edited May 19, 2011 by Teoman Soygul

User contributions licensed under CC BY-SA 3.0