帮我看看我段代码是关于加密,解密的(50分)

  • 主题发起人 主题发起人 你若有情
  • 开始时间 开始时间

你若有情

Unregistered / Unconfirmed
GUEST, unregistred user!
//我想用来加密网页!然后解密!解密是解不出来
unit MainUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
C1 = 1;
C2 = 0;
type
TForm1 = class(TForm)
bt_Encrypt: TButton;
OpenDialog: TOpenDialog;
EncryptFile: TEdit;
BrowserFile: TButton;
Button1: TButton;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure BrowserFileClick(Sender: TObject);
procedure bt_EncryptClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
TmpBeEncryptFileText:TStringList;
implementation
uses
BrowserUnit;
{$R *.dfm}
function Encrypt(const S: String;
Key: Word): String;
var
I: Integer;
begin
Result := S;
for I := 1 to Length(S) do
begin
Result := char(byte(S) xor (Key shr 8));
Key := (byte(Result) + Key) * C1 + C2;
end;
end;

function Decrypt(const S: String;
Key: Word): String;
var
I: Integer;
begin
Result := S;
for I := 1 to Length(S) do
begin
Result := char(byte(S) xor (Key shr 8));
Key := (byte(S) + Key) * C1 + C2;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
TmpBeEncryptFileText:=TStringList.Create;
end;

procedure TForm1.BrowserFileClick(Sender: TObject);
begin
if FileExists(EncryptFile.Text) then
OpenDialog.FileName:=EncryptFile.Text;
if OpenDialog.Execute then
EncryptFile.Text:=OpenDialog.FileName;
end;

procedure TForm1.bt_EncryptClick(Sender: TObject);
var
tmpEncryptStr:String;
begin
if EncryptFile.Text<>'' then
begin
try
TmpBeEncryptFileText.LoadFromFile(EncryptFile.Text);
tmpEncryptStr:=Encrypt(TmpBeEncryptFileText.Text,5);
Showmessage(tmpEncryptStr);
Memo1.Lines.Add(tmpEncryptStr);
TmpBeEncryptFileText.Clear;
TmpBeEncryptFileText.Add(tmpEncryptStr);
//TmpBeEncryptFileText.SaveToFile(EncryptFile.Text);
Showmessage('加密成功!');
except
ShowMessage('文件操作错误!');
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
tmpDecryptStr:String;
begin
if EncryptFile.Text<>'' then
begin
try
TmpBeEncryptFileText.Clear;
TmpBeEncryptFileText.LoadFromFile(EncryptFile.Text);
ShowMessage(TmpBeEncryptFileText.Text);
Memo1.Lines:=TmpBeEncryptFileText;
Memo1.Lines.SaveToFile(EncryptFile.Text+'tmp');
{TmpBeEncryptFileText.Clear;
TmpBeEncryptFileText.LoadFromFile(EncryptFile.Text);
TmpDecryptStr:=Decrypt(TmpBeEncryptFileText.Text,5);
TmpBeEncryptFileText.Clear;
TmpBeEncryptFileText.Add(TmpDecryptStr);
TmpBeEncryptFileText.SaveToFile(ExtractFilePath(EncryptFile.Text)+'TmpHtml.htm');
Application.CreateForm(TBrowserForm,BrowserForm);
BrowserForm.WebBrowser.Navigate(ExtractFilePath(EncryptFile.Text)+'TmpHtml.htm');
BrowserForm.Show;}
except
ShowMessage('文件操作错误!');
end;
end;
end;

end.
 
unit Cl_crypt32;
{
*************************************************************************
*名称: Cl_crypt32.pas
*
*说明: Cl_crypt32.pas为一支编解码模组,乃修改‘Martin Djern犘’先生所修改
* 的《crypt32.pas 32 bits encode/decode module》而成(修改的理由请详
* 见下述)。
*
*函数原型: function cl_encrypt(s:string):string;
* function cl_decrypt(s:string):string;
*
*使用方法: 《编码》encrypted:=cl_encrypt('password');
* 《解码》decrypted:=cl_decrypt(encrypted);
*
*注意: 使用时,请先修改cl_crypt32.pas内StartKey,MultKey,AddKey三个key值。
* (其中startkey的值最好不要小于256)。
*
*修改的理由: 仅管‘Martin Djern犘’先生的《crypt32.pas 32 bits encode/decode module》
* 已经很好用,但是我在实际应用时,却发现下列的问题:
* 1.无法把编码后的字串指定给edit元件。
* 2.无法把编码后的字串存放在.ini档内。
* 上述两点可以举下例两个例子证明:
* (例一〉
* edit1.text:=Encrypt('password',StartKey,MultKey,AddKey);
* edit2.text:=decrypt(edit1.text,StartKey,MultKey,AddKey);
* 结果: edit2.text的内容不等于'password'
* (例二)
* var
* IniFile: TIniFile;
* begin
* IniFile := TIniFile.Create('test.INI');
* IniFile.WriteString('demo', 'password', Encrypt('password',StartKey,MultKey,AddKey));
* IniFile.Free;
*
* IniFile := TIniFile.Create('test.INI');
* edit2.text:=decrypt(IniFile.readString('demo', 'password', ''),StartKey,MultKey,AddKey);
* IniFile.Free;
* 结果: edit2.text的内容不等于'password'
* 上述两种情况的发生原因,大概是因为编码后的字元码大于127,
* 因此无法被‘具有处理文字型态能力的元件’正确处理。
* 基于上述理由,我把它修改了一下,并加上以系统时间值为参数,
* 使其编码后的值可以成功存放在edit.text及.ini files内。
*
*修改者: cloudy@tpts4.seed.net.tw
*
*************************************************************************
}

{
*************************************************************************
* 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
uses
SysUtils;
const
StartKey = 956;
{Start default key}
MultKey = 58645;
{Mult default key}
AddKey = 28564;
{Add default key}
function cl_encrypt(s:string):string;
function cl_decrypt(s:string):string;
//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+}
{Coded by cloudy}
Function cl_intto0str(int1:integer;
len:integer):string;
var
i,j:integer;
begin
if length(inttostr(int1))>=len then
result:=inttostr(int1)
else
begin
result:='';
i:=len-length(inttostr(int1));
for j:=1 to i do
result:=result+'0';
result:=result+inttostr(int1);
end;
end;

{Coded by cloudy}
function cl_chartobytestr(s:string):string;
var
i:byte;
begin
result:='';
for i:=1 to length(s) do
result:=result+cl_intto0str(byte(s),3);
end;

function cl_bytetocharstr(s:string):string;
var
i:integer;
begin
i:=1;
result:='';
if (length(s) mod 3)=0 then
while i<length(s) do
begin
result:=result+char(strtoint(copy(s,i,3)));
i:=i+3;
end;
end;

{Coded by cloudy}
function cl_encrypt(s:string):string;
var
years, months, days, hours, mins, secs, msec:word;
cl_StartKey, cl_MultKey, cl_AddKey: longint;
begin
decodedate(now, years, months, days);
decodetime(now, hours, mins, secs, msec);
cl_StartKey:=msec;
if cl_StartKey<256 then
cl_StartKey:=cl_StartKey+256;
cl_Multkey:=((years-1900)*12+months)*30+days+cl_StartKey*10+cl_StartKey;
cl_AddKey:=(23*hours+mins)*60+secs+cl_StartKey*10+cl_StartKey;
result:=cl_chartobytestr(Encrypt(cl_intto0str(cl_StartKey,3),StartKey,MultKey,AddKey))+cl_chartobytestr(Encrypt(cl_intto0str(cl_Multkey,5),StartKey,MultKey,AddKey))+cl_chartobytestr(Encrypt(cl_intto0str(cl_Addkey,5),StartKey,MultKey,AddKey))+cl_chartobytestr(Encrypt(s,cl_StartKey,cl_MultKey,cl_AddKey));
end;

{Coded by cloudy}
function cl_decrypt(s:string):string;
var
cl_StartKey, cl_Multkey, cl_AddKey:longint;
begin
cl_StartKey:=strtoint(decrypt(cl_bytetocharstr(copy(s, 1, 9)),StartKey,MultKey,AddKey));
cl_MultKey:=strtoint(decrypt(cl_bytetocharstr(copy(s, 10, 15)),StartKey,MultKey,AddKey));
cl_AddKey:=strtoint(decrypt(cl_bytetocharstr(copy(s, 25, 15)),StartKey,MultKey,AddKey));
result:=decrypt(cl_bytetocharstr(copy(s, 40, length(s)-39)),cl_StartKey,cl_MultKey,cl_AddKey);
end;

end.

 
接受答案了.
 

Similar threads

I
回复
0
查看
576
import
I
I
回复
0
查看
399
import
I
I
回复
0
查看
600
import
I
I
回复
0
查看
724
import
I
后退
顶部