C # There are two solutions to call the dephi Interface Method
1. Compile the dephi program into a COM component and reference the COM component in C,
II. The code for the unmanaged dephi DLL file is as follows:
Code
Dephi interface method:
Function rsadecrypt (apkey, adkey, aData: pchar; abuffer: pointer; abuffersize: DWORD): integer; stdcall;
Function rsaencrypt (apkey, aData: pchar; abuffer: pointer; abuffersize: DWORD): integer; stdcall;
Function aesdecrypt (aData: pchar; abuffer: pointer; abuffersize: DWORD): integer;
Code
// C # Call dephi
Public static class drawchartfromdll
{
// Call an unmanaged DLL,
[Dllimport ("rsaadpter. dll", entrypoint = "rsaencrypt", charset = charset. ANSI, callingconvention = callingconvention. stdcall)]
/// RSA Encryption
Public static extern int rsaencrypt (string apkey, string aData, intptr abuffer, int abuffersize );
[Dllimport ("rsaadpter. dll", entrypoint = "rsadecrypt", charset = charset. ANSI, callingconvention = callingconvention. stdcall)]
/// RSA decryption
Public static extern int rsadecrypt (string apkey, string adkey, string aData, intptr abuffer, int abuffersize );
[Dllimport ("rsaadpter. dll", entrypoint = "aesdecrypt", charset = charset. ANSI, callingconvention = callingconvention. stdcall)]
/// Common AES decryption
Public static extern int aesdecrypt (string aData, intptr abuffer, int abuffersize );
}
Code
// Use ASP. NET pointer to implement the dephi Interface Method
String publickey = DT. Rows [0] ["rsapkey"]. tostring (); // Local Public Key
String privatekey = DT. Rows [0] ["rsadkey"]. tostring (); // local private key
Intptr PTR = marshal. allochglobal (2000); // unmanaged definition pointer, with a length of 2000
Int resu = drawchartfromdll. rsadecrypt (publickey, privatekey, Data, PTR, 2000); // dephi interface method, RSA decryption private key
Data = tostringfromintptr (PTR, resu); // obtain the RSA decrypted string
/// <Summary>
/// Obtain the string from the intptr pointer
/// </Summary>
/// <Param name = "PTR"> pointer </param>
/// <Param name = "result"> obtain the number of bytes of a string </param>
/// <Returns> </returns>
Private string tostringfromintptr (intptr, int result)
{
If (result <= 0)
{
Marshal. freehglobal (PTR); // release the memory allocated by the pointer.
Return "0 ";
}
Byte [] B = new byte [Result];
Marshal. Copy (PTR, B, 0, result );//
Marshal. freehglobal (PTR); // release the memory allocated by the pointer.
Return System. Text. encoding. Default. getstring (B, 0, result); // obtain the decrypted private key string
}