⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 字符串加密解密单元,是对程序员982000光盘中错误.txt

📁 自己对DELPHI学习的一点体会
💻 TXT
字号:
字符串加密/解密单元,是对程序员98/2000光盘中错误加密解密程序的纠正 (2001年1月7日) 

网友更新  分类:算法   作者: alaclp(推荐)  推荐:alaclp   阅读次数:435  
(http://www.codesky.net)  

--------------------------------------------------------------------------------
unit CrackUnit;

interface

uses Classes, Dialogs, SysUtils;

Type
TCrackObj = class
private
FKey1: Integer;
FKey2: Integer;
Fkey3: Word;
protected
function TransChar(AChar: Char): Integer;
function StrToHex(AStr: string): string;
function HexToStr(AStr: string): string;
public
constructor Create;
function Encrypt(const S: String): String;
function Decrypt(S: String): String;
property BegKey: Integer read FKey1 write FKey1;
property EndKey: Integer read FKey2 write FKey2;
property LockKey: Word read FKey3 write FKey3;
end;


implementation

constructor TCrackObj.Create;
begin
FKey1 := 53523;
FKey2 := 32768;
FKey3 := 13;
end;

//将16进制数转化为十进制数
function TCrackObj.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 TCrackObj.StrToHex(AStr: string): string;
var
I : Integer;
begin
Result := '';
For I := 1 to Length(AStr) do
Result := Result + IntToHex(Byte(AStr[I]), 2);
end;

function TCrackObj.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 TCrackObj.Encrypt(const S: String): String;
var
I : Integer;
AKey: Word;
Tmp: string;
begin
//得到加密字符
AKey := LockKey;
Result := S;
for I := 1 to Length(S) do
begin
Result[I] := char(byte(S[I]) xor (AKey shr 8));
AKey := (byte(Result[I]) + AKey) * BegKey + EndKey;
if Result[I] = Chr(0) then Result[I] := S[I];
end;
Result := StrToHex(Result);
end;

function TCrackObj.Decrypt(S: String): String;
var
I, J: Integer;
Tmp1, Tmp2: string;
AKey: Word;
begin
S := HexToStr(S);
Result := S;
AKey := Lockkey;
for I := 1 to Length(S) do
begin
if char(byte(S[I]) xor (AKey shr 8)) = Chr(0) then
begin
Result[I] := S[I];
AKey := (byte(Chr(0)) + AKey) * BegKey + EndKey; //保证Key的正确性
end
else
begin
Result[I] := char(byte(S[I]) xor (AKey shr 8));
AKey := (byte(S[I]) + AKey) * BegKey + EndKey;
end;
end;
end;

end.  
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -