Intercepor的问题,现在改了demo中的程序,加了加密程序,但是不成功,请赐教,300分(300分)

G

gsfxm

Unregistered / Unconfirmed
GUEST, unregistred user!

{*******************************************************}
{ }
{ DataSnap Socket Server Intercepor Demo }
{ }
{*******************************************************}
unit intrcptu;
{
NOTE: This demo requires the ZLib units found in the extras directory on the
CD.
The Socket Server has the ability to install an interception COM object that
can be called whenever it receives or sends data. Using this feature, you
can encrypt or compress data using any method you wish. This demo uses the
ZLib compression units that ship on the CD to compress/uncompress all data
going over the wire.
To use this demo;
1) Make sure you have copied the ZLib units from the CD to a directory and
have added that directory to this projects search path.
2) Compile Intrcpt.dpr.
3) Register Intrcpt.DLL using REGSVR32 or TREGSVR on both the client and the
server.
4) On the Server: Bring up the properties for the Socket Server (right click
on the icon in the task bar and select properties) and put the GUID for
Intrcpt.DLL in the Interceptor GUID edit control. The GUID is defined
below as Class_DataCompressor.
5) On the Client: Set the TSocketConnection.InterceptName property to
Intrcpt.DataCompressor. This will set the InterceptGUID property to the
Class_DataCompressor GUID. Recompile your client.
}
interface
uses
Windows, ActiveX, ComObj, SConnect,StdCtrls;
type
{
The interception object needs to implement IDataIntercept defined in
SConnect.pas. This interface has 2 procedures DataIn and DataOut described
below.
}
TDataCompressor = class(TComObject, IDataIntercept)
protected
procedure DataIn(const Data: IDataBlock);
stdcall;
procedure DataOut(const Data: IDataBlock);
stdcall;
end;

const
Class_DataCompressor: TGUID = '{B249776C-E429-11D1-AAA4-00C04FA35CFA}';
implementation
uses ComServ, SysUtils, ZLib, Classes, MidConst;
{
DataIn is called whenever data is coming into the client or server. Use this
procedure to uncompress or decrypt data.
}
function encode(source:TMemoryStream):TMemoryStream;
var
Dest : TMemoryStream;
Ch : byte;
begin
if source.Size=0 then
begin
result:=source;
exit;
end;
Dest := TMemoryStream.Create;
while Source.Position <> Source.Sizedo
begin
Source.Read(Ch,1);
Ch := Ch XOR Ord('a');
Dest.Write(ch,1);
end;
result:=dest;
end;

function decode(source:TMemoryStream):TMemoryStream;
var
Dest : TMemoryStream;
Ch : byte;
begin
Dest := TMemoryStream.Create;
if source.Size=0 then
begin
result:=source;
exit;
end;
while Source.Position <> Source.Sizedo
begin
Source.Read(Ch,1);
Ch := Ch XOR Ord('a');
Dest.Write(ch,1);
end;
result:=dest;
end;

procedure TDataCompressor.DataIn(const Data: IDataBlock);
var
Size: Integer;
InStream, OutStream: TMemoryStream;
ZStream: TDecompressionStream;
p: Pointer;
begin
InStream := TMemoryStream.Create;
try
{ Skip BytesReserved bytes of data }
p := Pointer(Integer(Data.Memory) + Data.BytesReserved);
Size := PInteger(p)^;
if Size = 0 then
Exit;
p := Pointer(Integer(p) + SizeOf(Size));
InStream.Write(p^, Data.Size - SizeOf(Size));
instream:=decode(instream);//先解密再解压
OutStream := TMemoryStream.Create;
try
InStream.Position := 0;
ZStream := TDecompressionStream.Create(InStream);//解压
try
OutStream.CopyFrom(ZStream, Size);
finally
ZStream.Free;
end;
{ Clear the datablock, then
write the uncompressed data back into the
datablock }
Data.Clear;
Data.Write(outStream.Memory^, outStream.Size);
finally
OutStream.Free;
end;
finally
InStream.Free;
end;
end;

{
DataOut is called whenever data is leaving the client or server. Use this
procedure to compress or encrypt data.
}
procedure TDataCompressor.DataOut(const Data: IDataBlock);
var
InStream, OutStream: TMemoryStream;
ZStream: TCompressionStream;
Size: Integer;
begin
InStream := TMemoryStream.Create;
try
{ Skip BytesReserved bytes of data }
InStream.Write(Pointer(Integer(Data.Memory) + Data.BytesReserved)^, Data.Size);
Size := InStream.Size;
if Size = 0 then
Exit;
OutStream := TMemoryStream.Create;
try
ZStream := TCompressionStream.Create(clFastest, OutStream);//先加压
try
ZStream.CopyFrom(InStream, 0);
finally
ZStream.Free;
end;
{ Clear the datablock, then
write the compressed data back into the
datablock }
outstream:=encode(outstream);
Data.Clear;
Data.Write(Size, SizeOf(Integer));
Data.Write(outStream.Memory^, outStream.Size);
finally
OutStream.Free;
end;
finally
InStream.Free;
end;
end;

initialization
{ Use this class factory to allow for easy identification of Interceptors }
TPacketInterceptFactory.Create(ComServer, TDataCompressor, Class_DataCompressor,
'DataCompressor', 'SampleInterceptor', ciMultiInstance, tmApartment);
end.
 
不加加密解密程序,该程序运行ok,加了以后就不行了,但是编译没有错,我对流操作
不熟,请给位高手指教,这段程序是为socket连接写的,在互联网络上面用,请各位指教
还有没有更好的加密方法,谢谢,最好能够附加源代码,或者在我的程序上进行修改。[:)]
 
他上面写的五个要求你都照做了吗?
 
hehe,这个问题简单,关键在你的Encode和Decode函数上,而且确实是因为对流操作不熟导致的。
你应该在Encode和Decode函数中先调用Source.Position = 0,把读指针移回流的头部,否则现在指针位于流的末尾。
另外,建议你不要把Encode和Decode实现为函数,而应该实现为过程:
procedure Encode(SrcStream: TStream;
DstStream: TStream);
procedure Decode(SrcStream: TStream;
DstStream: TStream);
 
异或加密是非常容易被破解的,可以使用SSL或者DES、RAS等,不过这些我也不熟,你自己去查相关的技术吧。
 
下面是我改的部分关键代码
procedure Encode(SrcStream: TStream);
var
Buffer : TMemoryStream;
Ch : byte;
begin
if SrcStream.Size=0 then
exit;
// 内部创建临时的缓冲区,结果写回SrcStream, 也可以外部传入DstStream接收结果。
Buffer := TMemoryStream.Create;
try
SrcStream.Position := 0;
// 原来这里用<>判断是一个危险的方式,因为它可能会导致死循环或异常
while SrcStream.Position < SrcStream.Sizedo
begin
SrcStream.Read(Ch,1);
Ch := Ch XOR Ord('a');
Buffer.Write(ch,1);
end;
Buffer.Position := 0;
SrcStream.Size := 0;
SrcStream.CopyFrom(Buffer, 0);
finally
Buffer.Free;
end;
end;

procedure Decode(SrcStream: TStream);
var
Buffer : TMemoryStream;
Ch : byte;
begin
if SrcStream.Size=0 then
exit;
Buffer := TMemoryStream.Create;
try
SrcStream.Position := 0;
while SrcStream.Position < SrcStream.Sizedo
begin
SrcStream.Read(Ch,1);
Ch := Ch XOR Ord('a');
Buffer.Write(ch,1);
end;
Buffer.Position := 0;
SrcStream.Size := 0;
SrcStream.CopyFrom(Buffer, 0);
finally
Buffer.Free;
end;
end;

 
顶部