C # Call the dephi Interface Method

Source: Internet
Author: User

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
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.