c# - Getting exception when calling CryptGetKeyParam -
I am trying to use crypto API (CAPI) to exchange a Diffie Hellman key I am using MSDN documentation.
// Step 0) receives the reference, then (! CAPI.CryptAcquireContext (reference this._cryptographicProvider, null, null, CAPI.PROV_DSS_DH, CAPI.CRYPT_VERIFYCONTEXT)) HandleWin32Error ( "cryptographic context Unable to get "); // Setp 1) Generate Private Key (! CAPI.CryptGenKey (this._cryptographicProvider, CAPI.CALG_DH_EPHEM, DHKEYSIZE & lt; 16) CAPI.CRYPT_EXPORTABLE | CAPI.CRYPT_PREGEN, this referee. _privateKeyPointer) HandleWin32Error ("Generate Unable to do private cryptographic key "); UIT GSize = 0; CAPI.CryptGetKeyParam (this._privateKeyPointer, CAPI.KP_G, blank, referee GSize, (UIT) 0); Byte [] g = new byte [jeezy]; Var res = CAPI.CryptGetKeyParam (this._privateKeyPointer, CAPI.KP_G, g, referee GSize, (UIT) 0);
The first call cryptgetKeyParam works perfectly, i.e. it successfully returns the size of G as 64.
Then the code on the last line fails, neither can return anything with the G-buffer (that is the case in the example) race = true, or an AccessViolationException when I use the following call with:
var res = CAPI.CryptGetKeyParam (this._privateKeyPointer, CAPI.KP_G, Ref G, ref Jisijh (UIT), 0);
Yes, I overloaded P \ invoke methods:
[DllImport ( "advapi32.dll", charset = CharSet.Auto, SetLastError = true )] public static exile bool CryptGetKeyParam (IntPtr HKEY, uint dwParam, ref byte [] pbData, ref uint pdwDataLen, uint dwFlags); [DllImport ( "advapi32.dll", charset = CharSet.Auto, SetLastError = true)] public static exile bool CryptGetKeyParam (IntPtr HKEY, uint dwParam, byte [] pbData, ref uint pdwDataLen, uint dwFlags);
Any ideas / ideas on this?
* Interestingly, I have been able to get a full Diffie Hellman key exchange to work, i.e. successfully successfully connecting between the same secret (S1) two parties, but this pre-defined P & G parameter Together with I am working on the code again because some did not look right because the secret secret was the same for every key exchange, which meant that X was continuously. (I.e. Bob and Alice agreed on S1 each time!?) - Here I was, again, with an issue CryptGetKeyParam as I was unable to determine the size of the session key when using KP_KEYLEN?!
Ahh Ha
This reference was a combination of the flag and overloaded method. Now works!
When P & amp; G is not predefined, then do not set CRYPT_PREGEN flags ... and only one of the two overloaded methods (this one):
[DllImport ("advapi32.dll", Charset = CharSetkAuto, SetLastError = True)] public static bool extension CryptGetKeyParam (IntPtr hKey, uint dwParam, byte [] pbData, ref uint pdwDataLen, uint dwFlags);