Blue
Red
Green
Orange
Voilet
Slate
Dark

UP有分 ★★★扩展blobstream类的问题 (用dbexpress+interbase) (200分)

T

tom28

Unregistered / Unconfirmed
GUEST, unregistred user!
关于加密流的问题,想实现以下功能:
把图片、word文档、pdf文档等内容(这些内容大小未知)保存在interbase
数据库表中的某个 blob类型的字段里,这个是加密保存,
不能用一般的方法读取,如何实现?(就是专有格式)
另外实现的方法要考虑速度问题,加密流对读写速度的影响大不大?
最好给出相关源码参考。
用dbexpress+interbase
刚注册,只有200分,都用了.请帮忙,谢谢。
 
Y

yanghaijun

Unregistered / Unconfirmed
GUEST, unregistred user!
简单的加密,在流的头部随便加一段你自己的信息,这样便搞乱流文档的
原来的格式了...
 
T

tom28

Unregistered / Unconfirmed
GUEST, unregistred user!
to yanghaijun 如何加?
 

远帆

Unregistered / Unconfirmed
GUEST, unregistred user!
变来变去,实在是没劲。
 
T

tom28

Unregistered / Unconfirmed
GUEST, unregistred user!
我看别人的贴子:
来自:鱼片干子, 时间:2000-10-1 9:01:00, ID:354182
我自定义了一个加密流encodestream,从tfilestream 中继承,
并重载了tfilestream的read,write方法.
在memo中输入文字,通过memo.lines.savetostream(encodestream)
能将文字以加密的形式存入文件中, 再通过memo.lines.loadfromstream(encodestream)
读入加密过的文件,也能正确以原文显示(不以加密显示)
................
...............
请问以上这种处理方式是否比函数加密更有效?如何实现?
 

远帆

Unregistered / Unconfirmed
GUEST, unregistred user!
那样做应该也可以,不过跟据你的要求得从tblobstream继承。
不如上面的通用。处理是一样的。
 
T

tom28

Unregistered / Unconfirmed
GUEST, unregistred user!
to 远帆:主要还要考虑一个读取速度问题,不知哪个更快?
 

爱元元的哥哥

Unregistered / Unconfirmed
GUEST, unregistred user!
// 司马华鹏
//------------------------------------------------------------------------------
//解密模块
function TUserInfoAccess.myDecryptStream(const Source:TStream;
Dest: TStream;
Password:string):Boolean;
var
Buffer: array[0..8191] of byte;
Hash: TDCP_sha1;
HashDigest, HashRead: array[0..31] of byte;
Decrypt: TDCP_blockcipher;
Read: integer;
Blowfish:TDCP_blowfish;
begin
Result:=True;
try
FillChar(HashDigest,Sizeof(HashDigest),$FF);
// fill the digest with $FF as the actual digest may not fill the entire digest
Hash:= TDCP_sha1.Create(nil);
Hash.Init;
// hash the passphrase to get the key
Hash.UpdateStr(Password);
Hash.Final(HashDigest);
Hash.Free;
Blowfish:=TDCP_blowfish.Create(nil);
Decrypt:= TDCP_blockcipher(TComponent(Blowfish));
// get the component from the combo box
if (Sizeof(HashDigest)*8)> Decrypt.MaxKeySize then
Decrypt.Init(HashDigest,Decrypt.MaxKeySize,nil) // make sure the key isn't too big
else
Decrypt.Init(HashDigest,Sizeof(HashDigest)*8,nil);
// initialize the cipher with the digest, IV= nil to generate one automatically (note: size in bits ie. sizeof(x)*8)
try
try
Decrypt.EncryptCBC(HashDigest,HashDigest,Sizeof(HashDigest));
// encrypt the hash to use as confirmation
Decrypt.Reset;
Source.ReadBuffer(HashRead,Sizeof(HashRead));
// read the other hash from the file and compare it
if not CompareMem(@HashRead,@HashDigest,Sizeof(HashRead)) then
Exception.Create('Failed to Decrypt');
repeat
Read:=Source.Read(Buffer,Sizeof(Buffer));
Decrypt.DecryptCBC(Buffer,Buffer,Read);
// read from the source decrypt and write out the result
Dest.WriteBuffer(Buffer,Read);
until Read<> Sizeof(Buffer);
finally
Decrypt.Burn;
// burn the key data (note:do
n't free it as we placed it on the form at design time)
Blowfish.Free;
end;
except
On e:exceptiondo
begin
Result:=False;
end;
end;
except
On e:exceptiondo
begin
Result:=False;
end;
end;
end;

//------------------------------------------------------------------------------
//加密文件单元
function TUserInfoAccess.myEncryptStream(const Source:TStream;
Dest: TStream;
Password:string):Boolean;
var
Buffer: array[0..8191] of byte;
Hash: TDCP_sha1;
HashDigest: array[0..31] of byte;
Encrypt: TDCP_blockcipher;
Read: integer;
Blowfish:TDCP_blowfish;
begin
Result:=True;
try
FillChar(HashDigest,Sizeof(HashDigest),$FF);
// fill the digest with $FF as the actual digest may not fill the entire digest
Hash:= TDCP_sha1.Create(nil);
Hash.Init;
Hash.UpdateStr(Password);
// hash the passphrase to get the key
Hash.Final(HashDigest);
Hash.Free;
Blowfish:=TDCP_blowfish.Create(nil);
Encrypt:= TDCP_blockcipher(TComponent(Blowfish));
// get the component from the combo box
try
try
if (Sizeof(HashDigest)*8)> Encrypt.MaxKeySize then
Encrypt.Init(HashDigest,Encrypt.MaxKeySize,nil) // make sure the key isn't too big
else
Encrypt.Init(HashDigest,Sizeof(HashDigest)*8,nil);
// initialize the cipher with the digest, IV= nil to generate one automatically (note: size in bits ie. sizeof(x)*8)
Encrypt.EncryptCBC(HashDigest,HashDigest,Sizeof(HashDigest));
// encrypt the hash and write it to the file to use as passphrase
Encrypt.Reset;
// confirmation
Dest.Write(HashDigest,Sizeof(HashDigest));
repeat
Read:=Source.Read(Buffer,Sizeof(Buffer));
Encrypt.EncryptCBC(Buffer,Buffer,Read);
// read from the source encrypt and write out the result
Dest.Write(Buffer,Read);
until Read<>Sizeof(Buffer);
finally
Encrypt.Burn;
// burn the key data (note:do
n't free it as we placed it on the form at design time)
Blowfish.Free;
end;
except
on e:exceptiondo
begin
Result:=False;
end;
end;
except
on e:exceptiondo
begin
Result:=False;
end;
end;
end;
 
T

tom28

Unregistered / Unconfirmed
GUEST, unregistred user!
我查了别人的贴子如下:

------------------------------------------------------------------------------
来自:飞来石, 时间:2001-8-19 19:44:00, ID:601683
在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
mykey:pchar;
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
mykey:pchar;
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.

以上程序均在windows 98、Delphi4.0环境中,通过编译,运行正常。
------------------------------------------------------------------------------

★★★我现在想知道,如何修改这个类,使它从tblobstream继承,符合我的流加密的要求?
 
D

dacom

Unregistered / Unconfirmed
GUEST, unregistred user!
没那么复杂吧 我相信简单就好,
var ms:tmemorystream;
begin
ms:=tmemorystram.create;
try
ms.loadformstream('c:/word.doc');
database1.starttranst//不知道怎么写了,就是事务了
table1.edit;
table1nr.loadfromstram(ms);//tablenr为一个永久字段(就是oracle表中的一个blob段)
table1.post;
database1.commit;
finally
ms.free;
end;

end;
 
T

tom28

Unregistered / Unconfirmed
GUEST, unregistred user!
to dacom:要加密
 
C

chnplzh

Unregistered / Unconfirmed
GUEST, unregistred user!
你讲的其实就是文件如何加密,我想只要算法想好了,实现起来应该很
简单的。
我编写过字符串加密,也就是是密码加密,这方面的算法很多,处理应该不会麻烦。
 
顶部 底部