当前位置:主页>Delphi教程>文章内容
一个很简单的加密算法
来源: 作者: 发布时间:2007-04-29  
 

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

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.


 
上一篇:字幕图标控件   下一篇:WinAPI编程关闭QQ登录窗体
 
  相关文章
·字幕图标控件
·WinAPI编程关闭QQ登录窗体
·关于中文折行及相关问题的解决方法
·组件开发方式
·Delphi托盘编程实战演练
·用Ehlib二次开发报表打印程序,实现财
·DELPHI的通配符比较(第五版)
·用XML做为数据存贮格式
·DELPHI的奇异菜单的编写
·如何实现应用程序中的”回车”成TAB?
·DELPHI中OPENGL程序设计
·如何在启动机器时自动运行adsl拨号(1
 
【关闭窗口】
推荐本站资源
最新文章