■■■如何对大型的文本文件进行加密?要快速的算法。■■■

goomoo 2003-02-09 01:13:49
如何对大型的文本文件进行加密?要快速的算法

最好有源码。
...全文
123 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
DelphiToby 2003-03-11
  • 打赏
  • 举报
回复
收藏
Wally_wu 2003-02-09
  • 打赏
  • 举报
回复
to goomoo(古木)
不错,直接对文件操作,不用转成字符串。
ricetons 2003-02-09
  • 打赏
  • 举报
回复
位移+xor

eg.

s = <<<s;
s = s xor key;

goomoo 2003-02-09
  • 打赏
  • 举报
回复
大家别笑我啊~~ 虽然加密强度不够高,还是挺实用的,如下:

procedure cryptFile(srcFile,dstFile:TFilename);
var
srcf,dstf:file of byte;
buf:byte;
begin
assign(srcf,srcfile);
assign(dstf,dstfile);
reset(srcf); rewrite(dstf);
while not eof(srcf) do
begin
read(srcf,buf);
buf:=buf+1; //这里可以修改成其他的数值
write(dstf,buf);
end;
closefile(srcf);
closefile(dstf);
end;

procedure decryptFile(srcFile,dstFile:TFilename);
var
srcf,dstf:file of byte;
buf:byte;
begin
assign(srcf,srcfile);
assign(dstf,dstfile);
reset(srcf); rewrite(dstf);
while not eof(srcf) do
begin
read(srcf,buf);
buf:=buf-1; //这里可以作相应修改
write(dstf,buf);
end;
closefile(srcf);
closefile(dstf);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
cryptfile('c:\jjj.txt','c:\zzz.dat');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
decryptFile('c:\zzz.dat','c:\zzz.txt');
end;
xjl 2003-02-09
  • 打赏
  • 举报
回复
拿出来看看呀
goomoo 2003-02-09
  • 打赏
  • 举报
回复
我有好算法了,加密一个300多K的文件,1秒钟左右!!! 文件大小不变!
Linux2001 2003-02-09
  • 打赏
  • 举报
回复
加密算法有很多,但是最好是不改变文件大小的加密算法
goomoo 2003-02-09
  • 打赏
  • 举报
回复
还有好的算法吗?
goomoo 2003-02-09
  • 打赏
  • 举报
回复
总的来说,不能采取一个字符一个字符来操作的方法,这样太慢。
goomoo 2003-02-09
  • 打赏
  • 举报
回复
to cg1120: 太慢,我加密一个20K的文件要花上5秒多的时间,而且加密后的文件尺寸增大了许多。
Wally_wu 2003-02-09
  • 打赏
  • 举报
回复
直接加密和解密算法:

{*******************************************************}
{ }
{ Decrypt }
{ }
{ bitwise compare of each characters XOR 27 }
{ }
{ Return string which after bitwise compare }
{ }
{*******************************************************}
function Decrypt(s: string; Key: Integer = 27): string;
var
i: Integer;
begin
Result := s;
for i := 1 to Length(s) do
Result[i] := Chr(Ord(s[i]) xor Key);
end;

{*******************************************************}
{ }
{ Encrypt }
{ }
{ Call again Decrypt to back to origin }
{ }
{ Return string which after bitwise compare }
{ }
{*******************************************************}
function Encrypt(s: string; Key : Integer =27): string;
begin
Result := Decrypt(s, Key);
end;


够不够快??
mrfanghansheng 2003-02-09
  • 打赏
  • 举报
回复
学习呀……
goomoo 2003-02-09
  • 打赏
  • 举报
回复
我先看看够不够快。
Billy_Chen28 2003-02-09
  • 打赏
  • 举报
回复
伴水的一个函数:

(*//
标题:字符串加密;pascal字符表示
说明:应用于文件加密
设计:Zswang
日期:2002-02-19
支持:wjhu111@21cn.com
//*)

///////Begin Source
function 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 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; { DisplayToString }

function StringEncrypt(mStr: string; 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; { StringEncrypt }

function StringDecrypt(mStr: string; 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; { StringDecrypt }
///////End Source

///////Begin Demo
const
cKey = '给你这一把钥匙,只能打开这一把锁';

procedure TForm1.Button1Click(Sender: TObject);
begin
Memo2.Text := StringToDisplay(StringEncrypt(Memo1.Text, cKey));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Text := StringDecrypt(DisplayToString(Memo2.Text), cKey);
end;
///////End Demo
Billy_Chen28 2003-02-09
  • 打赏
  • 举报
回复
program Crypt;
uses WinCRT;
const
C1 = 52845;
C2 = 22719;
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[I] := char(byte(S[I]) xor (Key shr 8));
Key := (byte(Result[I]) + Key) * C1 + C2;
end;
end;

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[I] := char(byte(S[I]) xor (Key shr 8));
Key := (byte(S[I]) + Key) * C1 + C2;
end;
end;

var
S: string;
begin
Write('>');
ReadLn(S);
S := Encrypt(S,12345);
WriteLn(S);
S := Decrypt(S,12345);
WriteLn(S);
end.
//////////////////////////////////////////////////////
unit Unit2;

interface

Const Allchar: string = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789';

procedure Encrypt( var ss: string );

implementation

procedure Encrypt( var ss: string );
var l, lac, // string length
sp, // ss char pointer
cp: integer; // allchar pointer
begin
l := Length(ss);
lac := Length( Allchar );
sp := 1;
while sp <= l do begin
cp := 1;
while (allchar[cp] <> ss[sp]) and ( cp <= lac ) do inc( cp );
{ match char and find the encrypted counterpart in the reverse
order in position }
if cp > lac then ss[sp]:= '*'
{ Mark illegal char - use only char not in allchar }
else begin
{ Un-remark next line will further enhance security...
such that same character will appear as
different after encrypt }

// cp := (( cp + sp*2 ) mod lac) + 1;

ss[sp] := allchar[ lac - cp + 1 ]; //first char result in the last
end;
inc(sp);
end;
end;


end.

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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