惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
Google DeepMind News
Google DeepMind News
博客园 - 【当耐特】
量子位
博客园 - 司徒正美
爱范儿
爱范儿
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
腾讯CDC
大猫的无限游戏
大猫的无限游戏
V
Visual Studio Blog
I
InfoQ
D
Docker
Recent Announcements
Recent Announcements
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
宝玉的分享
宝玉的分享
G
Google Developers Blog
GbyAI
GbyAI
Y
Y Combinator Blog
有赞技术团队
有赞技术团队
H
Help Net Security

博客园 - KeithDan

Windows 8开发 WinRT 对ZIP文件解压缩及文件夹的ZIP压缩 wp应用发布,金宝贝点读 WP7游戏分裂细胞 WP7游戏方块夺宝 C#手工解析XML xeno tactic 2 完整版 Lua扫雷 惊人!天下奇闻“鹏” 今天很郁闷 HighLightCode1.2——开源项目 代码着色--关于循环与分组 代码着色--开源系列 非托管dll的需要注意的地方 page_load执行两次 transfer object (3) transfer object (2) transfer object (1) 中国象棋(网络版) 人生的阶梯
利用非托管dll实现多平台加密
KeithDan · 2007-11-30 · via 博客园 - KeithDan

我们使用delphi作为win32开发,编写的rsa加密,需要在服务器使用公钥加密,而在客户端使用私钥解密.
本程序使用的加密dll为delphi所写,包含3个函数,其函数原形如下:

function CreateKey(var key:RsaKey):boolean;export; stdcall;
  function EncryptRsa(key:pchar;commkey:pchar;text:pchar):pchar;export; stdcall;
  function DecryptRsa(key:pchar;commkey:pchar;text:pchar):pchar;export; stdcall;

其中,在rsakey类型原形为

type RsaKey=packed record
publickey:pChar;
privatekey:pChar;
commkey:pchar;
end;

在C#中首先需要DllImport将dll导入,并声明其外部方法.

 public struct RsaKey
        
{
            
public string publicKey;
            
public string privateKey;
            
public string commKey;
        }

        [DllImport(
@"Security.dll")]
        
public static extern string EncryptRsa(string key, string commkey, string text);
        [DllImport(
@"Security.dll")]
        
public static extern string DecryptRsa(string key, string commkey, string text);
        [DllImport(
@"Security.dll")]
        
public static extern bool CreateKey(ref RsaKey key);


其中Rsakey对应struct.
其中的测试demo大家可以下载,包含c#以及delphi

--------------------------------
C#Demo DelphiDemo