怎样实现字符串的加密与解密...

Ado1234 2004-08-06 03:08:28
最好能写出完整的源代码.
只要实现功能就行.
(┣可以UP但只UP无分┫)
...全文
242 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
GoldShield 2004-08-09
  • 打赏
  • 举报
回复
另:
const
C1 = 52845; //字符串加密算法的公匙
C2 = 22719; //字符串加密算法的公匙
GoldShield 2004-08-09
  • 打赏
  • 举报
回复

//字符串加密函数

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;
奔跑9999 2004-08-08
  • 打赏
  • 举报
回复
unit register;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB;

type
TFregister = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Label2: TLabel;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
procedure Button2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Fregister: TFregister;

implementation

{$R *.dfm}

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;

end.
yxf 2004-08-08
  • 打赏
  • 举报
回复
tccb() :

那样也太简单了吧,要加也得加几十的

tccb 2004-08-07
  • 打赏
  • 举报
回复
最简单的方法,每个字符+1
jinjazz 2004-08-07
  • 打赏
  • 举报
回复
搜索以下有很多
goodloop 2004-08-07
  • 打赏
  • 举报
回复
加密麻算法自己想一个..不要太容易被别人看出来就行..
没什么很特别的技术的:P
IwantFlay 2004-08-06
  • 打赏
  • 举报
回复
unit unitLockString;

interface

uses
SysUtils,
Classes;

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;

end.

这是转载别人的代码我整理了一下,你参考一下吧

16,742

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧