Moodsky 发表于 2007-2-12 10:29:10

一个很简单的加密算法

当时见到一张帖子即兴写了这段代码,还没认真调试过

program Project1;

{$APPTYPE CONSOLE}

uses
SysUtils;

const
Key='TESTNET';
Cryptograph='有些事还是不知为妙';

//不能为0.5,相加为1
Percent1=0.21;
Percent2=0.79;

var
s1:string;
function GetKey(aKey:string;aPercent:Double):string;
var
i:integer;
begin
SetLength(Result,Length(aKey));
for i:=1 to Length(aKey) do
begin
    Result:=Chr(Round(Ord(aKey)*aPercent));
end;
end;

function EnCode(aCryptograph,aKey:string):string;
var
i,keylen,codelen:integer;
begin
keylen:=Length(akey);
codelen:=Length(aCryptograph);
SetLength(Result, Length(aCryptograph));
for i:=1 to codelen do
begin
    Result:=Chr(Ord(aCryptograph)+Ord(aKey[(i mod KeyLen)+1]));
end;
end;

function DeCode(aCryptograph,aKey:string):string;
var
i,keylen,codelen:integer;
begin
keylen:=Length(akey);
codelen:=Length(aCryptograph);
SetLength(Result, Length(aCryptograph));
for i:=1 to codelen do
begin
    Result:=Chr(Ord(aCryptograph)-Ord(aKey[(i mod KeyLen)+1]));
end;
end;

begin
{ TODO -oUser -cConsole Main : Insert code here }

WriteLn('要加密的文字');
WriteLn(Cryptograph);
WriteLn;

WriteLn('密码经过第一个网络');
s1:=EnCode(Cryptograph,GetKey(Key,Percent1));
WriteLn(s1);
WriteLn;

WriteLn('密码经过第二个网络');
s1:=EnCode(s1,GetKey(Key,Percent2));
WriteLn(s1);
WriteLn;

WriteLn('还原');
s1:=DeCode(s1,Key);
WriteLn(s1);
WriteLn;
WriteLn('如果要在VCL控件中显示,还要对#0进行处理,'+
         '因为VCL中大部分函数以#0作为结束标记');
ReadLn;
end.

VC8 发表于 2007-2-25 11:12:02

看不太明白/:?

yousee 发表于 2007-2-25 15:43:02

看不懂,哈哈!!!

wyh1983 发表于 2007-2-25 20:34:17

DELPHI

xuanyuanhanbing 发表于 2007-2-25 23:21:51

原帖由 yousee 于 2007-2-25 15:43 发表
看不懂,哈哈!!!
、、

;P ;P 是一点都看不懂!1

黑夜彩虹 发表于 2007-2-26 16:45:04

晚点用这个写个CM出来给大家玩玩~~;P

浮云思音 发表于 2007-3-23 20:59:17

这段时间在学D,先收藏了
页: [1]
查看完整版本: 一个很简单的加密算法