[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int PK11SDR_DecryptDelegate(ref TSECItem data, ref TSECItem result, int cx);
Импорт PK11SDR_Decrypt в C# и постоянный результат -1 при вызове
Импорт:
Код:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int PK11SDR_DecryptDelegate(TSECItem data, ref TSECItem result, int cx);
public static int PK11SDR_Decrypt( TSECItem data, ref TSECItem result, int cx)
{
IntPtr pProc = GetProcAddress(NSS3, "PK11SDR_Decrypt");
PK11SDR_DecryptDelegate dll = (PK11SDR_DecryptDelegate)Marshal.GetDelegateForFunctionPointer(pProc, typeof(PK11SDR_DecryptDelegate));
return dll(data, ref result, 0);
}
public delegate int PK11SDR_DecryptDelegate(TSECItem data, ref TSECItem result, int cx);
public static int PK11SDR_Decrypt( TSECItem data, ref TSECItem result, int cx)
{
IntPtr pProc = GetProcAddress(NSS3, "PK11SDR_Decrypt");
PK11SDR_DecryptDelegate dll = (PK11SDR_DecryptDelegate)Marshal.GetDelegateForFunctionPointer(pProc, typeof(PK11SDR_DecryptDelegate));
return dll(data, ref result, 0);
}
Код:
TSECItem:
[StructLayout(LayoutKind.Sequential)]
public struct TSECItem
{
public int SECItemType;
public int SECItemData;
public int SECItemLen;
}
[StructLayout(LayoutKind.Sequential)]
public struct TSECItem
{
public int SECItemType;
public int SECItemData;
public int SECItemLen;
}
Код:
StringBuilder se = new StringBuilder(encryptedString);
int hi2 = FFHelper.NSSBase64_DecodeBuffer(IntPtr.Zero, IntPtr.Zero, se, se.Length);
TSECItem item = (TSECItem)Marshal.PtrToStructure(new IntPtr(hi2), typeof(TSECItem));
var error = FFHelper.PK11SDR_Decrypt(item, ref tSecDec, 0);
if (error == 0)
{
if (tSecDec.SECItemLen != 0)
{
bvRet = new byte[tSecDec.SECItemLen];
Marshal.Copy(new IntPtr(tSecDec.SECItemData), bvRet, 0, tSecDec.SECItemLen);
result = System.Text.Encoding.ASCII.GetString(bvRet);
}
}
int hi2 = FFHelper.NSSBase64_DecodeBuffer(IntPtr.Zero, IntPtr.Zero, se, se.Length);
TSECItem item = (TSECItem)Marshal.PtrToStructure(new IntPtr(hi2), typeof(TSECItem));
var error = FFHelper.PK11SDR_Decrypt(item, ref tSecDec, 0);
if (error == 0)
{
if (tSecDec.SECItemLen != 0)
{
bvRet = new byte[tSecDec.SECItemLen];
Marshal.Copy(new IntPtr(tSecDec.SECItemData), bvRet, 0, tSecDec.SECItemLen);
result = System.Text.Encoding.ASCII.GetString(bvRet);
}
}
Код: