我想加密一个文本文件,只要简单加密一下就可以了(50分)

  • 主题发起人 主题发起人 ouyongke
  • 开始时间 开始时间
O

ouyongke

Unregistered / Unconfirmed
GUEST, unregistred user!
我看了一下,这里怎么全是字符串加密呢?我想给一个文本文件倒是找到一个例子,您看罗:
procedure TForm1.Button2Click(Sender: TObject)

var b:byte

I:integer

filestream:tfilestream

begin
FileStream:=TFileStream.Create('c:/a.dat',fmOpenReadWrite)

^^^^^^^^^^^^^^^^^^^^^^^用读写模式打开
for I:=0 to FileStream.size-1 do
begin
filestream.position:=i

filestream.readbuffer(b,1)

b:=b xor $F

filestream.position:=i

filestream.writebuffer(b,1)

end
filestream.Free

end;
一次才处理一个字节,太慢了吧,我改进的请看:
procedure TForm1.btnDecodeClick(Sender: TObject);
var
b:Array[0..72] of Byte;
I,j:Integer;
filestream:TFileStream;
str:string;//密匙
StartTime:TDateTime;
begin
StartTime:=now;
Screen.Cursor:=crHourGlass;
j:=-1;
str:='PHKFZQLZHLJCLJJLDHGCHLOKPHKFZQLZHLJCLJJLDHGCHLOKPHKFZQLZHLJCLJJLDHGCHLOK';
FileStream:=TFileStream.Create('c:/temp/aaaa2.txt',fmOpenReadWrite);
filestream.readbuffer(b,72);
for I:=0 to FileStream.size-1 do
begin
if j<=72 then
begin
j:=j+1;
b[j]:=b[j] xor Byte(str[j]);
end
else
begin
j:=0;
filestream.position:=i;
filestream.writebuffer(b,72);
filestream.position:=i;
if FileStream.size-1-i<72 then
filestream.readbuffer(b,FileStream.size-1-i)
else
filestream.readbuffer(b,72);
end;
//b:=b xor $F;
Gauge1.Progress:= Round(i / FileStream.size*100);
Application.ProcessMessages;
end;
filestream.Free;
Screen.Cursor:=crDefault;
ShowMessage(FormatDateTime('hh:nn:ss zzz', Now - StartTime));
end;
不好意思得很,快是快了,就是上面我的代码是错的!
所以,想请您帮个忙,帮我组合一个速度有点保证的加密文件过程吧
 
转自:delphi葵花宝典
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 := 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: 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;

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.

{ Specail about this procedure:

1. Same procedure to encrypt and decrypt.
( less code to maintain. )
2. Every Allchar set produce a different encryption.
{ a set of 62 char produce 3.147E85 combinations )
3. Full control over character set of encrypted string.
( good for 7-bit fields, barcoding, passwords, magstripe and filenames etc )
4. One table lookup. ( easy to ensure no duplication possible
for every possible allchar. So as a need to remove/add chars )
5. Automatic marking of illegal characters.
( simplified coding needs. )



--------------------------------------------------------------------------------


以下介绍的加密算法强度不高,我编写它是因为我需要既保持文本是由可打印字符组成的,同时又无法直接看到,也许一个孩子花上十分钟就可以破解这个算法......也许吧,这我不清楚,而且也不关心,因为我只要它能达到我的目的就行。



密钥(Key)的长度可以设置为任意长度。在这个函数中,将每个字符的低四位和密钥中字符的低四位进行XOR运算,这样加密后的字符仍然是7位的字符同时也是可打印的。





{注意 :- Key如果为空,则使用缺省密钥}

function DenCrypt(Str : string
Key : string): string;

var

X, Y : Integer;

A : Byte;

begin

if Key = '' then

Key := 'd1duOsy3n6qrPr2eF9u';

Y := 1;

for X := 1 to length(Str) do begin

A := (ord(Str[X]) and $0f) xor (ord(Key[Y]) and $0f);

Str[X] := char((ord(Str[X]) and $f0) + A);

inc(Y);

if Y > length(Key) then

Y := 1;

end;

Result := Str;

end;



由于函数使用的是简单的XOR运算,因此你可以再次以相同的密钥调用此函数就可以解密字符串。
 
不懂 帮顶
 
我知道异或很快,可是,您看我第一段代码,速度奇慢无比!
 
其实文本文件加密就是字符加密
 
关键所在就是ReadBuffer不能读出String,只能读出Array,而这两种类型怎么转换?
就是说,Array[0..4096] of Byte 或者 Array[0..4096] of char 转化成 String,再转化回来的办法是什么?
别告诉我一次把XXMB的文本文件都Load进内存来吧,呵呵

------------------------------------------------------------------------------

对不起,现在问题集中到上面这个点上了,其他部分都已经定下来了。谢谢!
 
TurboPower LockBox 2.07

LbDES1.EncryptFile;
LbDES1.DecryptFile

还有各种加密方法。不错的,开源,免费。
 
看大家这么辛苦,结了吧
问题没解决
 
后退
顶部