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;
procedure TFregister.Button2Click(Sender: TObject);
begin
close;
end;
function GetHDSerialNumber(Drv : String): String;
var
VolumeSerialNumber : DWORD;
MaximumComponentLength : DWORD;
FileSystemFlags : DWORD;
begin
if Drv[Length(Drv)] =':' then Drv := Drv + '\';
GetVolumeInformation(pChar(Drv),
nil,
0,
@VolumeSerialNumber,
MaximumComponentLength,
FileSystemFlags,
nil,
0);
Result := IntToHex(HiWord(VolumeSerialNumber), 4) +
'-' +
IntToHex(LoWord(VolumeSerialNumber), 4);
end;
function GetHardDiskSerial(const DriveLetter: Char): string;
var
NotUsed: DWORD;
VolumeFlags: DWORD;
VolumeInfo: array[0..MAX_PATH] of Char;
VolumeSerialNumber: DWORD;
begin
GetVolumeInformation(PChar(DriveLetter + ':\'),
nil, SizeOf(VolumeInfo), @VolumeSerialNumber, NotUsed,
VolumeFlags, nil, 0);
Result := Format('%8.8X',
[VolumeSerialNumber])
end;
procedure TFregister.FormCreate(Sender: TObject);
begin
edit1.Text:=GetHardDiskSerial('c');
end;
procedure TFregister.Button1Click(Sender: TObject);
var
str:string;
inti:integer;
begin
str:='';
for inti:=0 to length(edit1.Text)do
begin
str:=str+chr(ord(edit1.Text[inti])+5);
end;
str:=trim(str);
if str<>edit2.Text then
begin
showmessage('注册号输入错误!');
exit;
end;
memo1.Lines.Clear;
memo1.Lines.Text:=edit2.Text;
memo1.Lines.SaveToFile(extractfiledir(application.exename)+'\register.txt');
showmessage('注册成功!');
close;
end;
type TLockString=class
private
function StringToDisplay(mString: String):String;
function DisplayToString(mDisplay: String):String;
function StringEncrypt(mStr, mKey: String):String;
function StringDecrypt(mStr, mKey: String):String;
public
function encryptString(strSource, strPKey: String):String;
function decryptString(strSource, strPKey: String):String;
end;
implementation
//*************************************************************
//先将要加密的字符串中不符合标准的字符转换成[#32..#127]中的字符
//*************************************************************
function TLockString.StringToDisplay(mString: String):String;
var
I: Integer;
S: string;
begin
Result := ''; S := '';
for I := 1 to Length(mString) do
if mString[I] in [#32..#127] then
S := S + mString[I]
else begin
if S <> '' then begin
Result := Result + QuotedStr(S);
S := '';
end;
Result := Result + Format('#$%x', [Ord(mString[I])]);
end;
if S <> '' then Result := Result + QuotedStr(S);
end;
//StringToDisplay的反过程
function TLockString.DisplayToString(mDisplay: String):String;
var
I: Integer;
S: string;
B: Boolean;
begin
Result := '';
B := False;
mDisplay := mDisplay;
for I := 1 to Length(mDisplay) do
if B then
case mDisplay[I] of
'''': begin
if S <> '' then Result := Result + StringReplace(S, '''''', '''', [rfReplaceAll]);
if Copy(mDisplay, I + 1, 1) = '''' then Result := Result + '''';
S := '';
B := False;
end;
else
S := S + mDisplay[I];
end
else
case mDisplay[I] of
'#', '''': begin
if S <> '' then Result := Result + Chr(StrToIntDef(S, 0));
S := '';
B := mDisplay[I] = '''';
end;
'$', '0'..'9', 'a'..'f', 'A'..'F': S := S + mDisplay[I];
end;
if (not B) and (S <> '') then Result := Result + Chr(StrToIntDef(S, 0));
end;
//********************************************************************
//加密,将mStr中的字符依次与mKey的每个字任进行xor操作,得到一个新的字符
//********************************************************************
function TLockString.StringEncrypt(mStr, mKey: String):String;
var
I, J: Integer;
begin
J := 1;
Result := '';
for I := 1 to Length(mStr) do
begin
Result := Result + Char(Ord(mStr[I]) xor Ord(mKey[J]));
if J + 1 <= Length(mKey) then
Inc(J)
else
J := 1;
end;
end;
//********************************************************************
//解密,将mStr中的字符依次与mKey的每个字任进行xor操作,得到一个新的字符
//********************************************************************
function TLockString.StringDecrypt(mStr, mKey: String):String;
var
I, J: Integer;
begin
J := 1;
Result := '';
{ 自己加步骤 }
for I := 1 to Length(mStr) do
begin
Result := Result + Char(Ord(mStr[I]) xor Ord(mKey[J]));
if J + 1 <= Length(mKey) then
Inc(J)
else
J := 1;
end;
end;
//******************************************************************
// 加密字符串 返回一个加密后的值,类型为PChar
// sourceStr --需要加密的字符串
// pKey --密钥
//******************************************************************
function TLockString.encryptString(strSource, strPKey: String):String;
begin
result :=StringToDisplay(StringEncrypt(strSource,strPKey));
end;
//******************************************************************
// 解密字符串 返回一个解密后的值,类型为PChar
// sourceStr --需要解密的字符串
// pKey --密钥
//******************************************************************
function TLockString.decryptString(strSource, strPKey: String):String;
begin
result :=StringDecrypt(DisplayToString(strSource),strPKey);
end;