code_junkie

P/Invoking CryptImportKey and marshaling structs

Nov 14th, 2011
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. // typedef struct _PUBLICKEYSTRUC
  2. // {
  3. // BYTE bType;
  4. // BYTE bVersion;
  5. // WORD reserved;
  6. // ALG_ID aiKeyAlg;
  7. // } BLOBHEADER, PUBLICKEYSTRUC;
  8. [StructLayout(LayoutKind.Sequential)]
  9. public struct PUBLICKEYSTRUC
  10. {
  11. public Byte bType;
  12. public Byte bVersion;
  13. public Int16 reserved;
  14. public Int32 aiKeyAlg;
  15. }
  16.  
  17. //typedef struct __KEYBLOB
  18. //{
  19. // BLOBHEADER hdr;
  20. // DWORD cbKeySize;
  21. // BYTE* rgbKeyData;
  22. //} KEYBLOB;
  23.  
  24. [StructLayout(LayoutKind.Sequential)]
  25. public struct KEYBLOB
  26. {
  27. public PUBLICKEYSTRUC hdr;
  28. public Int16 cbKeySize;
  29. public Byte[] rgbKeyData;
  30. }
  31.  
  32. int len = (Marshal.SizeOf(typeof(PUBLICKEYSTRUC) + Marshal.SizeOf(typeof(KEYBLOB)) + KeySize;
  33. byte[] arr = new byte[len];
  34. IntPtr ptr = Marshal.AllocHGlobal(len);
  35. Marshal.StructureToPtr(keyBlob, ptr, true);
  36. Marshal.Copy(ptr, arr, 0,len);
  37. Marshal.FreeHGlobal(ptr);
Add Comment
Please, Sign In to add comment