阅读内容 

delphi三十六之加密篇

[日期:2001-01-06] 来源:yesky  作者:陈立平 [字体: ]
  1.最简单最有效实现一个程序一次开机只运行一个实例,用于限制版或注册版;稍加修改可知是否已有本程序的实例在运行:

 //在窗体的OnShow事件中书写如下代码

  Const
   AtomStr = "OnlyOnce";
   ReMind = "此程序版本一次开机只能运行一次,请注册后再使用!";
  var
   atom : integer;
  begin
   if GlobalFindAtom(AtomStr) = 0 then
    atom := GlobalAddAtom(AtomStr)
   else
  begin
   Application.MessageBox(ReMind, "提示", Mb_Ok + Mb_IconInformation);
   Close;
  end;

 end;

  2. 一个字符串加密解密程序(此程序在程序员98和2000光盘中多次出现,但都有错误,以下为修改
稿)


function TransChar(AChar: Char): Integer;

  begin
  if AChar in ["0".."9"] then
  Result := Ord(AChar) - Ord("0")
  else
   Result := 10 + Ord(AChar) - Ord("A");

  end;

function StrToHex(AStr: string): string;

  var
   I : Integer;
   Tmp: string;

  begin
   Result := "";
   For I := 1 to Length(AStr) do

  begin
   Result := Result + Format("%2x", [Byte(AStr[I])]);

  end;

   I := Pos(" ", Result);
   While I $#@60;$#@62; 0 do

  begin
   Result[I] := "0";
   I := Pos(" ", Result);

  end;

 end;

function HexToStr(AStr: string): string;

  var

   I : Integer;
   CharValue: word;

  begin
   Result := "";
   For I := 1 to Trunc(Length(Astr)/2) do

  begin
   Result := Result + " ";
   CharValue := TransChar(AStr[2*I-1])*16 + TransChar(AStr[2*I]);
   Result[I] := Char(CharValue);
  end;

 end;

function Encrypt(const S: String; Key: word): String;

 var
  I : Integer;

 begin
  Result := S;
  for I := 1 to Length(S) do
  begin
   Result[I] := char(byte(S[I]) xor (Key shr 8));
   Key := (byte(Result[I]) + Key) * C1 + C2;
   if Result[I] = Chr(0) then
   Result[I] := S[I];
 end;
  Result := StrToHex(Result);

 end;


function Decrypt(const S: String; Key: word): String;

  var
   I: Integer;
   S1: string;

  begin

   S1 := HexToStr(S);
   Result := S1;
  for I := 1 to Length(S1) do  

  begin
  if char(byte(S1[I]) xor (Key shr 8)) = Chr(0) then

  begin

  Result[I] := S1[I];
   Key := (byte(Chr(0)) + Key) * C1 + C2; //保证Key的正确性  end

  else

  begin
   Result[I] := char(byte(S1[I]) xor (Key shr 8));
   Key := (byte(S1[I]) + Key) * C1 + C2;
  end;

 end;

end;

阅读:
录入:

推荐 】 【 打印
相关新闻      
本文评论       全部评论
发表评论
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款


点评: 字数
姓名:
Advertisement
内容查询


Advertisement