Форум: "Основная";
Текущий архив: 2003.06.09;
Скачать: [xml.tar.bz2];
Внизпомогите написать управляющую часть для шифратора Найти похожие ветки
← →
Апатин Даниил (2003-05-29 16:39) [0]
unit Unit1;
interface
type TEAKey = array [0..3] of cardinal;
procedure TEA_Encode(Input,Output:pointer;size:integer;key:TEAKey);
procedure TEA_Decode(Input,Output:pointer;size:integer;key:TEAKey);
implementation
type
TEAData = array[0..1] of cardinal;
PTEAKey = ^TEAKey;
PTEAData = ^TEAData;
Procedure TEA_Cipher(v:PTEAData;var w:PTEAData;k:PTEAKey);
var y,z,sum,delta,n:Cardinal;
begin
y:=(v)[0]; z:=(v)[1];
sum:=0; delta:=$9E3779B9;
n:=32;
while (n > 0) do begin
inc(y, (z shl 4 xor z shr 5) + z xor sum + (k)[sum and 3]);
inc(sum,delta);
inc(z,(y shl 4 xor y shr 5) + y xor sum + (k)[sum shr 11 and 3]);
dec(n);
end;
(w)[0]:=y; (w)[1]:=z;
end;
Procedure TEA_DeCipher(v:PTEAData;var w:PTEAData;k:PTEAKey);
var y,z,sum,delta,n:Cardinal;
begin
y:=v[0];
z:=v[1];
sum:=$0C6EF3720;
delta:=$9E3779B9;
n:=32;
while (n > 0) do begin
dec(z,(y shl 4 xor y shr 5) + y xor sum + k[sum shr 11 and 3]);
dec(sum,delta);
dec(y,(z shl 4 xor z shr 5) + z xor sum + k[sum and 3]);
dec(n);
end;
w[0]:=y; w[1]:=z;
end;
procedure TEA_EnDec(encode:boolean;Input,Output:pointer;size:integer;key:TEAKey);
var DataIn,DataOut:TEAData;
DOut:PTEAData;
i,sz:integer;
begin
DOut:=@DataOut;
sz:=(size shr 3) shl 3;
i:=0;
repeat
DataIn[0]:=Cardinal((pointer(Cardinal(Input)+Cardinal(i)))^);
DataIn[1]:=Cardinal((pointer(Cardinal(Input)+Cardinal(i+4)))^);
if encode then TEA_Cipher(@DataIn,DOut,@key) else TEA_DECipher(@DataIn,DOut,@key);
Cardinal(pointer(Cardinal(Output)+Cardinal(i))^):=DataOut[0];
Cardinal(pointer(Cardinal(Output)+Cardinal(i+4))^):=DataOut[1];
inc(i,8);
until i>=sz;
end;
procedure TEA_Encode(Input,Output:pointer;size:integer;key:TEAKey);
begin
TEA_EnDec(true,Input,Output,size,key);
end;
procedure TEA_Decode(Input,Output:pointer;size:integer;key:TEAKey);
begin
TEA_EnDec(false,Input,Output,size,key);
end;
end.
← →
Digitman (2003-05-29 17:01) [1]уж не знаю, что ты там имел ввиду под "управляющей структурой", но - вот код, разбирайся :
type
TCryptTable = array [0..255] of Byte;
PCryptTable = ^TCryptTable;
TCryptKey = DWord;
const
DefaultCryptKey = $6A6C6865;
DataBlockHdrSize = 12; // Signature(DWord) + Context(DWord) + DataSize(DWord)
DefaultCryptTable: TCryptTable =
(
$59, $60, $37, $6B, $65, $62, $46, $48, $53, $61, $4C, $59, $60, $57, $5B, $3D,
$5E, $34, $6D, $36, $50, $3F, $6F, $67, $53, $61, $4C, $59, $40, $47, $63, $39,
$50, $5F, $5F, $3F, $6F, $47, $43, $69, $48, $33, $31, $64, $35, $5A, $4A, $42,
$56, $40, $67, $53, $41, $07, $6C, $49, $58, $3B, $4D, $46, $68, $43, $69, $48,
$33, $31, $44, $65, $62, $46, $48, $53, $41, $07, $6C, $69, $48, $33, $51, $54,
$5D, $4E, $6C, $49, $38, $4B, $55, $4A, $62, $46, $48, $33, $51, $34, $6D, $36,
$50, $5F, $5F, $5F, $3F, $6F, $47, $63, $59, $40, $67, $33, $31, $64, $35, $5A,
$6A, $52, $6E, $3C, $51, $34, $6D, $36, $50, $5F, $5F, $3F, $4F, $37, $4B, $35,
$5A, $4A, $62, $66, $58, $3B, $4D, $66, $58, $5B, $5D, $4E, $6C, $49, $58, $3B,
$4D, $66, $58, $3B, $4D, $46, $48, $53, $61, $4C, $59, $40, $67, $33, $31, $64,
$55, $6A, $32, $3E, $44, $45, $52, $6E, $3C, $31, $64, $55, $6A, $52, $4E, $6C,
$69, $48, $53, $61, $4C, $39, $30, $6F, $47, $63, $59, $60, $57, $5B, $3D, $3E,
$64, $35, $3A, $3A, $5A, $6A, $52, $4E, $6C, $69, $48, $53, $61, $6C, $49, $58,
$3B, $4D, $46, $68, $63, $39, $50, $5F, $5F, $3F, $6F, $67, $53, $41, $25, $41,
$3C, $51, $54, $3D, $5E, $54, $5D, $4E, $4C, $39, $50, $5F, $5F, $5F, $3F, $6F,
$47, $43, $69, $48, $33, $51, $54, $5D, $6E, $3C, $31, $64, $35, $5A, $00, $00
);
type
{ IDataBlock }
IDataBlock = interface(IUnknown)
["{71B66900-D436-11D5-82C2-444553540000}"]
function GetBytesReserved: Integer; stdcall;
function GetMemory: Pointer; stdcall;
function GetSize: Integer; stdcall;
procedure SetSize(Value: Integer); stdcall;
function GetStream: TStream; stdcall;
function GetSignature: Integer; stdcall;
procedure SetSignature(Value: Integer); stdcall;
function GetContext: Integer; stdcall;
procedure SetContext(Value: Integer); stdcall;
procedure Clear; stdcall;
function Write(const Buffer; Count: Integer): Integer; stdcall;
function Read(var Buffer; Count: Integer): Integer; stdcall;
procedure IgnoreStream; stdcall;
function InitData(Data: Pointer; DataLen: Integer; CheckLen: Boolean): Integer; stdcall;
property BytesReserved: Integer read GetBytesReserved;
property Memory: Pointer read GetMemory;
property Signature: Integer read GetSignature write SetSignature;
property Context: Integer read GetContext write SetContext;
property Size: Integer read GetSize write SetSize;
property Stream: TStream read GetStream;
end;
{ IDataCodec }
IDataCodec = interface
["{1DE0B202-8270-11D4-BF6B-006094ACED50}"]
function Get_Enabled: WordBool; stdcall;
procedure Set_Enabled(const Value: WordBool); stdcall;
function Get_CryptKey: TCryptKey; stdcall;
procedure Set_CryptKey(const Value: TCryptKey); stdcall;
function Get_CryptTable: PCryptTable; stdcall;
procedure Set_CryptTable(Value: PCryptTable); stdcall;
procedure EncryptData(const Data: IDataBlock); stdcall;
procedure DecryptData(const Data: IDataBlock); stdcall;
property Enabled: WordBool read Get_Enabled write Set_Enabled;
property CryptKey: TCryptKey read Get_CryptKey write Set_CryptKey;
property CryptTable: PCryptTable read Get_CryptTable write Set_CryptTable;
procedure RandomizeCryptTable; stdcall;
end;
TACSDataCodec = class(TInterfacedObject, IDataCodec)
private
FCryptTable: PCryptTable;
FCryptKey: TCryptKey;
FInStream: TMemoryStream;
FOutStream: TMemoryStream;
FEnabled: Boolean;
protected
function Get_Enabled: WordBool; stdcall;
procedure Set_Enabled(const Value: WordBool); stdcall;
function Get_CryptKey: TCryptKey; stdcall;
procedure Set_CryptKey(const Value: TCryptKey); stdcall;
function Get_CryptTable: PCryptTable; stdcall;
procedure Set_CryptTable(Value: PCryptTable); stdcall;
procedure EncryptData(const Data: IDataBlock); virtual; stdcall;
procedure DecryptData(const Data: IDataBlock); virtual; stdcall;
procedure RandomizeCryptTable; virtual; stdcall;
public
constructor Create; overload;
constructor Create(ACryptTable: PCryptTable; const ACryptKey: TCryptKey = DefaultCryptKey); overload;
destructor Destroy; override;
end;
← →
Digitman (2003-05-29 17:01) [2]
..........
{ TACSDataCodec }
constructor TACSDataCodec.Create;
begin
inherited Create;
FCryptKey:= DefaultCryptKey;
FCryptTable:= @DefaultCryptTable;
FEnabled:= True;
end;
constructor TACSDataCodec.Create(ACryptTable: PCryptTable; const ACryptKey: TCryptKey);
begin
inherited Create;
if Assigned(ACryptTable) and (ACryptTable <> @DefaultCryptTable) then
Set_CryptTable(ACryptTable)
else
FCryptTable:= @DefaultCryptTable;
FCryptKey:= ACryptKey;
FEnabled:= True;
end;
destructor TACSDataCodec.Destroy;
begin
if FCryptTable <> @DefaultCryptTable then
FreeMem(FCryptTable);
inherited Destroy;
end;
function TACSDataCodec.Get_Enabled: WordBool;
begin
Result:= FEnabled;
end;
procedure TACSDataCodec.Set_Enabled(const Value: WordBool);
begin
FEnabled:= Value;
end;
procedure TACSDataCodec.DecryptData(const Data: IDataBlock);
var
i, x, k, FullPacketSize, DataPacketSize, CheckCode, EncryptCode: DWORD;
Packet: PByteArray;
begin
if Assigned(Data) and (Data.Size > 0) and FEnabled then
with Data do begin
Packet:= PByteArray(Integer(Memory) + BytesReserved);
FullPacketSize:= Stream.Size;
DataPacketSize:= FullPacketSize - BytesReserved - SizeOf(CheckCode);
CheckCode:= PInteger(@Packet[DataPacketSize])^;
CheckCode:=
((CheckCode and $0001F000) shr $0C) or
((CheckCode and $07C007C0) shr $01) or
((CheckCode and $003E0001) shl $0A) or
((CheckCode and $F8000000) shr $10) or
((CheckCode and $0000083E) shl $0F);
EncryptCode:= FullPacketSize * FCryptKey + CheckCode;
i:= 0;
while i < DataPacketSize do begin
k:= Min(DataPacketSize - i, SizeOf(DWORD));
Move(Packet[i], x, k);
x:= x xor (EncryptCode + FCryptTable[i and $FF]);
Move(x, Packet[i], k);
Inc(i, k);
end;
Size:= DataPacketSize;
end;
end;
procedure TACSDataCodec.EncryptData(const Data: IDataBlock);
var
i, x, z, k, R1, R2, N1, N2, N3, HeaderSize, FullPacketSize, DataPacketSize, CheckCode, EncryptCode: DWORD;
Packet: PByteArray;
begin
if Assigned(Data) and (Data.Size > 0) and FEnabled then
with Data do begin
HeaderSize:= BytesReserved;
Write(CheckCode, SizeOf(CheckCode));
FullPacketSize:= Stream.Size;
DataPacketSize:= FullPacketSize - HeaderSize - SizeOf(CheckCode);
Packet:= PByteArray(Memory);
i:= (Packet[1] shl 24) or (Packet[4] shl 16) or (Packet[0] shl 8) or Packet[5];
x:= HeaderSize + Random(FullPacketSize - HeaderSize);
z:= Random(SizeOf(TCryptTable));
k:= ((x shl 24) or (Packet[x] shl 16) or (z shl 8) or Packet[z]) xor $00FF00FF;
CheckCode:= i xor k;
EncryptCode:= FullPacketSize * FCryptKey + CheckCode;
Packet:= PByteArray(Integer(Packet) + HeaderSize);
i:= 0;
while i < DataPacketSize do begin
k:= Min(DataPacketSize - i, SizeOf(DWORD));
Move(Packet[i], x, k);
x:= x xor (EncryptCode + FCryptTable[i and $FF]);
Move(x, Packet[i], k);
Inc(i, k);
end;
PInteger(@Packet[i])^:=
((CheckCode and $0000001F) shl $0C) or
((CheckCode and $03E003E0) shl $01) or
((CheckCode and $F8000400) shr $0A) or
((CheckCode and $0000F800) shl $10) or
((CheckCode and $041F0000) shr $0F);
end;
end;
function TACSDataCodec.Get_CryptKey: TCryptKey;
begin
Result:= FCryptKey;
end;
procedure TACSDataCodec.Set_CryptKey(const Value: TCryptKey);
begin
FCryptKey:= Value;
end;
function TACSDataCodec.Get_CryptTable: PCryptTable;
begin
Result:= FCryptTable;
end;
procedure TACSDataCodec.Set_CryptTable(Value: PCryptTable);
var
TempCryptTable: PCryptTable;
begin
GetMem(TempCryptTable, SizeOf(TCryptTable));
try
Move(Value^, TempCryptTable^, SizeOf(TCryptTable));
if FCryptTable <> @DefaultCryptTable then
FreeMem(FCryptTable);
FCryptTable:= TempCryptTable;
except
FreeMem(TempCryptTable);
raise;
end;
end;
procedure TACSDataCodec.RandomizeCryptTable;
var
rn: Integer;
RTable: PCryptTable;
begin
Randomize;
rn:= 1 + Random(SizeOf(TCryptTable) - 2);
GetMem(RTable, SizeOf(TCryptTable));
FillChar(RTable^, SizeOf(TCryptTable), 0);
try
Move(DefaultCryptTable[rn], RTable^, SizeOf(TCryptTable) - rn);
Move(DefaultCryptTable, RTable[SizeOf(TCryptTable) - rn], rn);
if FCryptTable <> @DefaultCryptTable then
FreeMem(FCryptTable);
FCryptTable:= RTable;
except
FreeMem(RTable);
raise;
end;
end;
← →
Апатин Даниил (2003-05-29 21:03) [3]Попробую разобраться
← →
Апатин Даниил (2003-05-29 21:10) [4]Извиняюсь, быть может меня не правильно поняли, но я хотел, привязать к коду который написал, TMemo, и 2 кнопки (зашифровать и расшифровать).
← →
Digitman (2003-05-30 09:13) [5]Ах вон оно что !)
Я не вдавался в подробности твоего алгоритма, поэтому прокомментируй - закодированный исходный текст у тебя остается текстом или становится просто массивом бинарных данных ? Т.е. твой алгоритм подразумевает, что шифрованный текст будет так же текстом, но содержать абракадабру ?
← →
Skier (2003-05-30 09:18) [6]>Апатин Даниил
А такой "шифратор" не устроит ?
(С) Марко Кэнту
unit EncodStr;
interface
uses
Classes;
type
TEncodedStream = class (TFileStream)
private
FKey: Char;
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: Char read FKey write FKey default "A";
end;
implementation
constructor TEncodedStream.Create(
const FileName: string; Mode: Word);
begin
inherited Create (FileName, Mode);
FKey := "A";
end;
function TEncodedStream.Write(const Buffer;
Count: Longint): Longint;
var
pBuf, pEnc: PChar;
I, EncVal: Integer;
begin
// allocate memory for the encoded buffer
GetMem (pEnc, Count);
try
// use the buffer as an array of characters
pBuf := PChar (@Buffer);
// for every character of the buffer
for I := 0 to Count - 1 do
begin
// encode the value and store it
EncVal := ( Ord (pBuf[I]) + Ord(Key) ) mod 256;
pEnc [I] := Chr (EncVal);
end;
// write the encoded buffer to the file
Result := inherited Write (pEnc^, Count);
finally
FreeMem (pEnc, Count);
end;
end;
function TEncodedStream.Read(var Buffer; Count: Longint): Longint;
var
pBuf, pEnc: PChar;
I, CountRead, EncVal: Integer;
begin
// allocate memory for the encoded buffer
GetMem (pEnc, Count);
try
// read the encoded buffer from the file
CountRead := inherited Read (pEnc^, Count);
// use the output buffer as a string
pBuf := PChar (@Buffer);
// for every character actually read
for I := 0 to CountRead - 1 do
begin
// decode the value and store it
EncVal := ( Ord (pEnc[I]) - Ord(Key) ) mod 256;
pBuf [I] := Chr (EncVal);
end;
finally
FreeMem (pEnc, Count);
end;
// return the number of characters read
Result := CountRead;
end;
end.
Страницы: 1 вся ветка
Форум: "Основная";
Текущий архив: 2003.06.09;
Скачать: [xml.tar.bz2];
Память: 0.49 MB
Время: 0.01 c