delphi中加密的一个串在C#中进行解密
delphi编写的dll中的源码:
const
MyKey='meetingzdr2011pcen';
function test3(SrcStr: pchar): pchar;stdcall;//字符串加密函数
//对字符串加密(Src:源 Key:密匙)
var KeyLen :Integer;
KeyPos :Integer;
offset :Integer;
dest :string;
SrcPos :Integer;
SrcAsc :Integer;
Range :Integer;
Src :string;
begin
Src:=string(SrcStr);
KeyLen:=Length(MyKey);
// if KeyLen = 0 then key:='delphi';
KeyPos:=0;
Range:=256;
Randomize;
offset:=Random(Range);
dest:=format('%1.2x',[offset]);
for SrcPos := 1 to Length(Src) do
begin
SrcAsc:=(Ord(Src[SrcPos]) + offset) MOD 255;
if KeyPos < KeyLen
then KeyPos:= KeyPos + 1
else KeyPos:=1;
SrcAsc:= SrcAsc xor Ord(MyKey[KeyPos]);
dest:=dest + format('%1.2x',[SrcAsc]); //pchar(string(result)+string(char(j)));//result+char(j);
offset:=SrcAsc;
end;
Result:=pchar(Dest);
end;
function test4(SrcStr: pchar): pchar;stdcall;//字符串解密函数
//对字符串解密(Src:源 Key:密匙)
var KeyLen :Integer;
KeyPos :Integer;
offset :Integer;
dest :string;
SrcPos :Integer;
SrcAsc :Integer;
TmpSrcAsc :Integer;
Src:AnsiString;
begin
Src:=string(SrcStr);
KeyLen:=Length(MyKey);
// if KeyLen = 0 then key:='delphi';
KeyPos:=0;
offset:=StrToInt('$'+ copy(src,1,2));
SrcPos:=3;
repeat
SrcAsc:=StrToInt('$'+ copy(src,SrcPos,2));
if KeyPos < KeyLen
Then KeyPos := KeyPos + 1
else KeyPos := 1;
TmpSrcAsc := SrcAsc xor Ord(MyKey[KeyPos]);
if TmpSrcAsc <= offset
then TmpSrcAsc := 255 + TmpSrcAsc - offset
else TmpSrcAsc := TmpSrcAsc - offset;
dest:=dest + chr(TmpSrcAsc);
offset:=srcAsc;
SrcPos:=SrcPos + 2;
until SrcPos >= Length(Src);
Result:=pchar(Dest);
end;
exports
test3,test4;
此方法在delphi中直接调用时,需要改成如下的方法:
const
MyKey='meetingzdr2011pcen';
function test3(SrcStr: AnsiString): AnsiString;stdcall;//字符串加密函数
//对字符串加密(Src:源 Key:密匙)
var KeyLen :Integer;
KeyPos :Integer;
offset :Integer;
dest :string;
SrcPos :Integer;
SrcAsc :Integer;
Range :Integer;
Src :string;
begin
Src:=string(SrcStr);
KeyLen:=Length(MyKey);
// if KeyLen = 0 then key:='delphi';
KeyPos:=0;
Range:=256;
Randomize;
offset:=Random(Range);
dest:=format('%1.2x',[offset]);
for SrcPos := 1 to Length(Src) do
begin
SrcAsc:=(Ord(Src[SrcPos]) + offset) MOD 255;
if KeyPos < KeyLen
then KeyPos:= KeyPos + 1
else KeyPos:=1;
SrcAsc:= SrcAsc xor Ord(MyKey[KeyPos]);
dest:=dest + format('%1.2x',[SrcAsc]); //pchar(string(result)+string(char(j)));//result+char(j);
offset:=SrcAsc;
end;
Result:=Dest;
end;
//此函数可供C#调用
function test4(SrcStr: AnsiString): AnsiString;stdcall;//字符串解密函数
//对字符串解密(Src:源 Key:密匙)
var KeyLen :Integer;
KeyPos :Integer;
offset :Integer;
dest :string;
SrcPos :Integer;
SrcAsc :Integer;
TmpSrcAsc :Integer;
Src:AnsiString;
begin
Src:=string(SrcStr);
KeyLen:=Length(MyKey);
// if KeyLen = 0 then key:='delphi';
KeyPos:=0;
offset:=StrToInt('$'+ copy(src,1,2));
SrcPos:=3;
repeat
SrcAsc:=StrToInt('$'+ copy(src,SrcPos,2));
if KeyPos < KeyLen
Then KeyPos := KeyPos + 1
else KeyPos := 1;
TmpSrcAsc := SrcAsc xor Ord(MyKey[KeyPos]);
if TmpSrcAsc <= offset
then TmpSrcAsc := 255 + TmpSrcAsc - offset
else TmpSrcAsc := TmpSrcAsc - offset;
dest:=dest + chr(TmpSrcAsc);
offset:=srcAsc;
SrcPos:=SrcPos + 2;
until SrcPos >= Length(Src);
Result:=Dest;
end;
delphi直接调用:
edit2.Text:= test3(edit1.Text);//加密
edit3.Text:=test4(edit2.Text); //解密
生成的dll在C#中调用方法:
[DllImport("MeetingMsgEn.dll")]
public static extern StringBuilder test3(StringBuilder AA);
[DllImport("MeetingMsgEn.dll")]
public static extern StringBuilder test4(StringBuilder AA);
//带key的加密和解密
StringBuilder S = new StringBuilder();
S.Append(this.textBox1.Text);
StringBuilder result = test3(S);
this.textBox2.Text = result.ToString();
StringBuilder SS = new StringBuilder();
SS.Append(this.textBox2.Text);
result = test4(SS);
this.textBox3.Text = result.ToString();