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[i]:=Chr(Round(Ord(aKey[i])*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[i]:=Chr(Ord(aCryptograph[i])+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[i]:=Chr(Ord(aCryptograph[i])-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.