列出几个加密的算法:
{1、32位加密法:}
unit Crypt32;
{
*************************************************************************
* Name: Crypt32.Pas *
* Description: 32 bits encode/decode module *
* 2^96 variants it is very high to try hack *
* Purpose: Good for encrypting passwors and text *
* Security: avoid use StartKey less than 256 *
* if it use only for internal use you may use default *
* key, but MODIFY unit before compiling *
* Call: Encrypted := Encrypt(InString,StartKey,MultKey,AddKey) *
* Decrypted := Decrypt(InString,StartKey) *
* Parameters: InString = long string (max 2 GB) that need to encrypt *
* decrypt *
* MultKey = MultKey key *
* AddKey = Second key *
* StartKey = Third key *
* (posible use defaults from interface) *
* Return: OutString = result string *
* Editor: Besr viewed with Tab stops = 2, Courier new *
* Started: 01.08.1996 *
* Revision: 22.05.1997 - ver.2.01 converted from Delphi 1 *
* and made all keys as parameters, before only start key *
* Platform: Delphi 2.0, 3.0 *
* work in Delphi 1.0, 2^48 variants, 0..255 strings *
* Author: Anatoly Podgoretsky *
* Base alghoritm from Borland *
* Address: Vahe 4-31, Johvi, Estonia, EE2045, tel. 61-142 *
* kvk@estpak.ee *
* Status: Freeware, but any sponsor help will be appreciated here *
* need to buy books, shareware products, tools etc *
*************************************************************************
* Modified: Supports Delphi 1.0 &
2.0 *
* Overflow checking removed *
* By: Martin Djern鎠 *
* e-mail: djernaes@einstein.ot.dk *
* web: einstein.ot.dk/~djernaes *
*************************************************************************
}
interface
const
StartKey = 981;
{Start default key}
MultKey = 12674; {Mult default key}
AddKey = 35891; {Add default key}
function Encrypt(const InString:string;
StartKey,MultKey,AddKey:Integer): string;
function Decrypt(const InString:string;
StartKey,MultKey,AddKey:Integer): string;
implementation
{$R-}
{$Q-}
{*******************************************************
* Standard Encryption algorithm - Copied from Borland *
*******************************************************}
function Encrypt(const InString:string;
StartKey,MultKey,AddKey:Integer): string;
var
I : Byte;
begin
Result := '';
for I := 1 to Length(InString)do
begin
Result := Result + CHAR(Byte(InString
) xor (StartKey shr 8));
StartKey := (Byte(Result) + StartKey) * MultKey + AddKey;
end;
end;
{*******************************************************
* Standard Decryption algorithm - Copied from Borland *
*******************************************************}
function Decrypt(const InString:string;
StartKey,MultKey,AddKey:Integer): string;
var
I : Byte;
begin
Result := '';
for I := 1 to Length(InString)do
begin
Result := Result + CHAR(Byte(InString) xor (StartKey shr 8));
StartKey := (Byte(InString) + StartKey) * MultKey + AddKey;
end;
end;
{$R+}
{$Q+}
end.
////////////////////////////////////////////////////////////////
//2、安全加密
Program CryptoDemo;
{
Copyright (c) 1994 by Andrew Eigus Fidonet: 2:5100/33
Demonstrates the use of unit CRYPTO.PAS
}
uses Crypto;
var
Str, Key : string;
begin
Str := 'This is text to encrypt with Encrypt procedure';
{ text to encrypt }
Key := 'ExCaLiBeR';
{ key string to use;
longer -> safer ;I }
WriteLn(#13#10'Original string: ''', Str, '''');
Encrypt(Str[1], Length(Str), Key, ecEncode);
WriteLn('Encrypted string: ''', Str, '''');
Encrypt(Str[1], Length(Str), Key, ecExtract);
WriteLn('Decrypted string: ''', Str, '''')
end.
///////////////////////////////////////////////////////////////////////
//3、编写定制的文件流实现文件读写加密
在 Delphi 中预定义了 Tfilestream 类 ,通过它可以对磁盘文件进行读写 ,笔者选定 Tfilestream 为基类 ,通过对其核心的两个读、写方法进行重载 ,编写定制的文件流 ,实现对文件的读、写进行加密。
首先 ,来看一下定制文件流 (Tmystream) 的声明 :
type
Tmystream=class(Tfilestream)
private
fkey:string;
public
constructor create(const filename:string;mode:word);
function read(var buffer;count:longint):longint;override;
function write(const buffer;count:longint):longint;override;
property key:string read fkey write fkey ;
end;
---- 在 Tmystream 的声名中 ,我们对 read 、 write 两个方法进行了重载 ,并添加了一个新的特性 key,用以存储对文件进行加密时所需的密码。为实现文件读写的加密 ,在 write 方法中 ,将 key 的每个字符依次与 buffer 中的字符相加 ,将得到的结果写入文件 ,实现加密 ;
在 read 方法中 ,将读出的内容依次与 key 的每个字符相减 ,实现解密。加密及解密的方法多种多样 ,读者可以通过改写相关代码 ,得到不同的加密方法。
程序清单如下 :
function Tmystream.write(const buffer;count:longint):longint;
var
Pbu,Pmy,mykeychar;
i,enc:integer;
begin
getmem(pmy,count);
// 为 pmy 分配内存
mykey:=pchar(key);
// 将 key 转换为 pchar 指针
try
pbu:=pchar(@buffer);
// 将 buffer 转换为 pchar 指针
for i:=0 to count-1do
// 将 key 的每个字符以此与 buffer 的每个字符循环相加 ,结果放入 pmy 指向的内存区
begin
enc:=(ord(pbu)+ord(mykey[(i mod length(key))])) mod 256;
Pmy:=char(enc);
end;
result:=inherited write(Pmy^,count);
// 将 pmy 指向的内容写入文件
finally
freemem(Pmy,count);
end;
end;
function Tmystream.read(var buffer;count:longint):longint;
var
Pbu,Pmy,mykeychar;
i,mycount,enc:integer;
begin
getmem(Pmy,count);// 为 pmy 分配内存
mykey:=pchar(key);// 将 key 转换为 pchar 指针
try
mycount:=inherited read(Pmy^,count);
// 将文件内容读入 pmy 指向内存区
Pbu:=Pchar(@buffer);
将 buffer 转换为 pchar 指针
for i:=0 to mycount-1do
// 将 key 的每个字符依次与 pmy 的每个字符循环相减 ,结果放入 pbu 指向的变量
begin
enc:=(ord(Pmy)-ord(mykey[(i mod length(key))])) mod 256;
Pbu:=chr(enc);
end;
finally
freemem(Pmy,count);
end;
result:=mycount;
end;
---- 完成定制文件流的编写后 ,便可在程序中应用 ,实现文件的读写加密 ,例程如下 :
unit Unit1;
interface
uses Windows, Messages, SysUtils, Classes,Graphics, Controls, Forms, Dialogs,ExtCtrls, StdCtrls,unit2,unit3;
//unit2 定义了 Tmystream
//unit3 定义了输入密码对话框 form3
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
Panel1: TPanel;
Panel2: TPanel;
Memo1: TMemo;
Splitter1: TSplitter;
Memo2: TMemo;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button2Click(Sender: TObject);
// 将选定的加密文件解开 ,读入 memo2
var
encstr:Tmystream;
begin
if opendialog1.Execute and (form3.showmodal=mrok) then
begin
encstr:=Tmystream.create(opendialog1.filename,fmopenread);
encstr.key:=form3.Edit1.Text;
try
memo2.lines.LoadFromStream(encstr);
finally
encstr.Free;
end;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
// 将 memo1 中的内容加密 ,用指定文件名另存
var
encstr:Tmystream;
begin
if savedialog1.Execute and (form3.showmodal=mrok) then
begin
encstr:=Tmystream.create(savedialog1.filename,fmcreate);
encstr.key:=form3.Edit1.Text;
try
memo1.lines.SaveToStream(encstr);
finally
encstr.Free;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
// 将指定文件读入 memo1
var
mystr:Tfilestream;
begin
if opendialog1.Execute then
begin
mystr:=Tfilestream.create(opendialog1.filename,fmopenread);
try
memo1.lines.LoadFromStream(mystr);
finally
mystr.Free;
end;
end;
end;
end.
/////////////////////////////////////////////////////////////////////////
//4、更好的加密算法:
Good Encryption Algorithm...
> I would like a Delphi unit algorithm that I can compile in
>
>I realize that home-brewed algorithms can be relatively weak.... but
>can someone just send me some algorithms to start with?
Try this one from Borland Allen, I've used it and itdo
es the job
const
C1 = 52845;
{Used for encryption of Master Password string}
C2 = 11719;
Key = 1234;
{ Standard Decryption algorithm - Copied from Borland}
function Decrypt(const S: String;
Key: Word): String;
var
I: byte;
begin
Result[0] := S[0];
for I := 1 to Length(S)do
begin
Result := char(byte(S) xor (Key shr 8));
Key := (byte(S) + Key) * C1 + C2;
end;
end;
{ Standard Encryption algorithm - Copied from Borland}
function Encrypt(const S: String;
Key: Word): String;
Var
I: byte;
begin
Result[0] := S[0];
for I := 1 to Length(S)do
begin
Result := char(byte(S) xor (Key shr 8));
Key := (byte(Result) + Key) * C1 + C2;
end;
end;
///////////////////////////////////////////////////////////////////////////
//字符串加密解密
type
TDynByteArray = array of byte;
const
SeedA = 5678;
/// 常量 ,你可以修改
SeedB = 5432;
/// 常量 ,你可以修改
/// 对数组加密
function Crypt(const s: TDynByteArray;
Key: Word;
const bEncrypt: boolean = true): TDynByteArray;
overload;
var
i : integer;
begin
SetLength(Result, Length(s));
for i := Low(s) to High(s)do
begin
Result := s xor (key shr 8);
if bEncrypt then
Key := (Result + key) * SeedA + SeedB
else
Key := (s + Key) * SeedA + SeedB;
end;
end;
/// 字符串
function Crypt(const s: string;
Key: Word;
const bEncrypt: boolean = True): string;
overload;
var
i : integer;
ps, pr : ^byte;
begin
SetLength(Result, Length(s));
ps := @s[1];
pr := @Result[1];
for i := 1 to length(s)do
begin
pr^ := ps^ xor (Key shr 8);
if bEncrypt then
Key := (pr^ + Key) * SeedA + SeedB
else
Key := (ps^ + Key) * SeedA + SeedB;
pr := pointer(integer(pr) + 1);
ps := pointer(integer(ps) + 1);
end
end;
/// 也可以对记录进行加密 ,只要把 TResultData 改成你的记录类型即可!!!!!!
function Crypt(const s: TResultData;
Key: Word;
const bEncrypt: boolean = True): TResultData;
overload;
var
i : integer;
ps, pr : ^byte;
begin
ps := @s;
pr := @Result;
for i := 1 to SizeOf(s)do
begin
pr^ := ps^ xor (Key shr 8);
if bEncrypt then
Key := (pr^ + Key) * SeedA + SeedB
else
Key := (ps^ + Key) * SeedA + SeedB;
pr := pointer(integer(pr) + 1);
ps := pointer(integer(ps) + 1);
end;
end;
***************************
function cryptstr(const s:string;
stype: dword):string;
var
i: integer;
fkey: integer;
begin
result:='';
case stype of
0:
begin
randomize;
fkey := random($ff);
for i:=1 to length(s)do
result := result+chr( ord(s) xor i xor fkey);
result := result + char(fkey);
end;
1:
begin
fkey := ord(s[length(s)]);
for i:=1 to length(s) - 1do
result := result+chr( ord(s) xor i xor fkey);
end;
end;
等等………………………………………太多了!!!