Форум: "Основная";
Текущий архив: 2003.12.30;
Скачать: [xml.tar.bz2];
ВнизШифрование Найти похожие ветки
← →
Serg7077 (2003-12-16 15:02) [0]Где нибудь есть исходные тексты по реализации алгоритма шифрования с открытым ключом ? Дайте ссылочку пожулуйста )))
← →
Reindeer Moss Eater (2003-12-16 15:31) [1]Алгоритма шифрования с открытым ключем нет.
Есть системы криптозащиты с открытыми ключами.
Они внутри себя используют самые обычные блочные и потоковые алгоритмы шифров на симметричных секретных ключах.
← →
Digitman (2003-12-16 18:13) [2]только в кач-ве иллюстрации к широкоизвестному ICQ-алгоритму шифрования, адаптированному к некоторым элементам MIDAS-технологии :
const
DataBlockHdrSize = 12; // Signature(DWord) + Context(DWord) + DataSize(DWord)
type
...
{ TACSDataCodec }
const
{ таблица констант, испольуемых для шифрации пакетов по умолчанию }
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
);
{ константа, используемая в качестве ключа шифрования по умолчанию }
DefaultCryptKey = $6A6C6865;
type
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;
...
implementation
...
{ 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;
...
end.
Страницы: 1 вся ветка
Форум: "Основная";
Текущий архив: 2003.12.30;
Скачать: [xml.tar.bz2];
Память: 0.48 MB
Время: 0.008 c