Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Основная";
Текущий архив: 2005.01.30;
Скачать: [xml.tar.bz2];

Вниз

Шифрование паролей   Найти похожие ветки 

 
ShimON ©   (2005-01-18 13:59) [0]

Здравствуйте, не могли бы вы мне подсказать какие не самые сложные способы есть. Мне в форму вводят пароль, который потом надо будет сохранить, хотелось бы зашифровать, а то кто знает... Нет ли возможности в Delphi реализовать MD5??? Заранее спасибо за ответ


 
kaZaNoVa ©   (2005-01-18 14:04) [1]

ShimON ©   (18.01.05 13:59)

> Нет ли возможности в Delphi реализовать MD5??? Заранее
> спасибо за ответ

unit DCPmd5;

interface
uses
 Classes, Sysutils, DCPcrypt2, DCPconst;

type
 TDCP_md5= class(TDCP_hash)
 protected
   LenHi, LenLo: longword;
   Index: DWord;
   CurrentHash: array[0..3] of DWord;
   HashBuffer: array[0..63] of byte;
   procedure Compress;
 public
   class function GetId: integer; override;
   class function GetAlgorithm: string; override;
   class function GetHashSize: integer; override;
   class function SelfTest: boolean; override;
   procedure Init; override;
   procedure Burn; override;
   procedure Update(const Buffer; Size: longword); override;
   procedure Final(var Digest); override;
 end;

{******************************************************************************}
{******************************************************************************}
implementation
{$R-}{$Q-}

function LRot32(a, b: longword): longword;
begin
 Result:= (a shl b) or (a shr (32-b));
end;

procedure TDCP_md5.Compress;
var
 Data: array[0..15] of dword;
 A, B, C, D: dword;
begin
 Move(HashBuffer,Data,Sizeof(Data));
 A:= CurrentHash[0];
 B:= CurrentHash[1];
 C:= CurrentHash[2];
 D:= CurrentHash[3];

 A:= B + LRot32(A + (D xor (B and (C xor D))) + Data[ 0] + $d76aa478,7);
 D:= A + LRot32(D + (C xor (A and (B xor C))) + Data[ 1] + $e8c7b756,12);
 C:= D + LRot32(C + (B xor (D and (A xor B))) + Data[ 2] + $242070db,17);
 B:= C + LRot32(B + (A xor (C and (D xor A))) + Data[ 3] + $c1bdceee,22);
 A:= B + LRot32(A + (D xor (B and (C xor D))) + Data[ 4] + $f57c0faf,7);
 D:= A + LRot32(D + (C xor (A and (B xor C))) + Data[ 5] + $4787c62a,12);
 C:= D + LRot32(C + (B xor (D and (A xor B))) + Data[ 6] + $a8304613,17);
 B:= C + LRot32(B + (A xor (C and (D xor A))) + Data[ 7] + $fd469501,22);
 A:= B + LRot32(A + (D xor (B and (C xor D))) + Data[ 8] + $698098d8,7);
 D:= A + LRot32(D + (C xor (A and (B xor C))) + Data[ 9] + $8b44f7af,12);
 C:= D + LRot32(C + (B xor (D and (A xor B))) + Data[10] + $ffff5bb1,17);
 B:= C + LRot32(B + (A xor (C and (D xor A))) + Data[11] + $895cd7be,22);
 A:= B + LRot32(A + (D xor (B and (C xor D))) + Data[12] + $6b901122,7);
 D:= A + LRot32(D + (C xor (A and (B xor C))) + Data[13] + $fd987193,12);
 C:= D + LRot32(C + (B xor (D and (A xor B))) + Data[14] + $a679438e,17);
 B:= C + LRot32(B + (A xor (C and (D xor A))) + Data[15] + $49b40821,22);

 A:= B + LRot32(A + (C xor (D and (B xor C))) + Data[ 1] + $f61e2562,5);
 D:= A + LRot32(D + (B xor (C and (A xor B))) + Data[ 6] + $c040b340,9);
 C:= D + LRot32(C + (A xor (B and (D xor A))) + Data[11] + $265e5a51,14);
 B:= C + LRot32(B + (D xor (A and (C xor D))) + Data[ 0] + $e9b6c7aa,20);
 A:= B + LRot32(A + (C xor (D and (B xor C))) + Data[ 5] + $d62f105d,5);
 D:= A + LRot32(D + (B xor (C and (A xor B))) + Data[10] + $02441453,9);
 C:= D + LRot32(C + (A xor (B and (D xor A))) + Data[15] + $d8a1e681,14);
 B:= C + LRot32(B + (D xor (A and (C xor D))) + Data[ 4] + $e7d3fbc8,20);
 A:= B + LRot32(A + (C xor (D and (B xor C))) + Data[ 9] + $21e1cde6,5);
 D:= A + LRot32(D + (B xor (C and (A xor B))) + Data[14] + $c33707d6,9);
 C:= D + LRot32(C + (A xor (B and (D xor A))) + Data[ 3] + $f4d50d87,14);
 B:= C + LRot32(B + (D xor (A and (C xor D))) + Data[ 8] + $455a14ed,20);
 A:= B + LRot32(A + (C xor (D and (B xor C))) + Data[13] + $a9e3e905,5);
 D:= A + LRot32(D + (B xor (C and (A xor B))) + Data[ 2] + $fcefa3f8,9);
 C:= D + LRot32(C + (A xor (B and (D xor A))) + Data[ 7] + $676f02d9,14);
 B:= C + LRot32(B + (D xor (A and (C xor D))) + Data[12] + $8d2a4c8a,20);

 A:= B + LRot32(A + (B xor C xor D) + Data[ 5] + $fffa3942,4);
 D:= A + LRot32(D + (A xor B xor C) + Data[ 8] + $8771f681,11);
 C:= D + LRot32(C + (D xor A xor B) + Data[11] + $6d9d6122,16);
 B:= C + LRot32(B + (C xor D xor A) + Data[14] + $fde5380c,23);
 A:= B + LRot32(A + (B xor C xor D) + Data[ 1] + $a4beea44,4);
 D:= A + LRot32(D + (A xor B xor C) + Data[ 4] + $4bdecfa9,11);
 C:= D + LRot32(C + (D xor A xor B) + Data[ 7] + $f6bb4b60,16);
 B:= C + LRot32(B + (C xor D xor A) + Data[10] + $bebfbc70,23);
 A:= B + LRot32(A + (B xor C xor D) + Data[13] + $289b7ec6,4);
 D:= A + LRot32(D + (A xor B xor C) + Data[ 0] + $eaa127fa,11);
 C:= D + LRot32(C + (D xor A xor B) + Data[ 3] + $d4ef3085,16);
 B:= C + LRot32(B + (C xor D xor A) + Data[ 6] + $04881d05,23);
 A:= B + LRot32(A + (B xor C xor D) + Data[ 9] + $d9d4d039,4);
 D:= A + LRot32(D + (A xor B xor C) + Data[12] + $e6db99e5,11);
 C:= D + LRot32(C + (D xor A xor B) + Data[15] + $1fa27cf8,16);
 B:= C + LRot32(B + (C xor D xor A) + Data[ 2] + $c4ac5665,23);

 A:= B + LRot32(A + (C xor (B or (not D))) + Data[ 0] + $f4292244,6);
 D:= A + LRot32(D + (B xor (A or (not C))) + Data[ 7] + $432aff97,10);
 C:= D + LRot32(C + (A xor (D or (not B))) + Data[14] + $ab9423a7,15);
 B:= C + LRot32(B + (D xor (C or (not A))) + Data[ 5] + $fc93a039,21);
 A:= B + LRot32(A + (C xor (B or (not D))) + Data[12] + $655b59c3,6);
 D:= A + LRot32(D + (B xor (A or (not C))) + Data[ 3] + $8f0ccc92,10);
 C:= D + LRot32(C + (A xor (D or (not B))) + Data[10] + $ffeff47d,15);
 B:= C + LRot32(B + (D xor (C or (not A))) + Data[ 1] + $85845dd1,21);
 A:= B + LRot32(A + (C xor (B or (not D))) + Data[ 8] + $6fa87e4f,6);
 D:= A + LRot32(D + (B xor (A or (not C))) + Data[15] + $fe2ce6e0,10);
 C:= D + LRot32(C + (A xor (D or (not B))) + Data[ 6] + $a3014314,15);
 B:= C + LRot32(B + (D xor (C or (not A))) + Data[13] + $4e0811a1,21);
 A:= B + LRot32(A + (C xor (B or (not D))) + Data[ 4] + $f7537e82,6);
 D:= A + LRot32(D + (B xor (A or (not C))) + Data[11] + $bd3af235,10);
 C:= D + LRot32(C + (A xor (D or (not B))) + Data[ 2] + $2ad7d2bb,15);
 B:= C + LRot32(B + (D xor (C or (not A))) + Data[ 9] + $eb86d391,21);

 Inc(CurrentHash[0],A);
 Inc(CurrentHash[1],B);
 Inc(CurrentHash[2],C);
 Inc(CurrentHash[3],D);
 Index:= 0;
 FillChar(HashBuffer,Sizeof(HashBuffer),0);
end;


 
kaZaNoVa ©   (2005-01-18 14:04) [2]

class function TDCP_md5.GetHashSize: integer;
begin
 Result:= 128;
end;

class function TDCP_md5.GetId: integer;
begin
 Result:= DCP_md5;
end;

class function TDCP_md5.GetAlgorithm: string;
begin
 Result:= "MD5";
end;

class function TDCP_md5.SelfTest: boolean;
const
 Test1Out: array[0..15] of byte=
   ($90,$01,$50,$98,$3c,$d2,$4f,$b0,$d6,$96,$3f,$7d,$28,$e1,$7f,$72);
 Test2Out: array[0..15] of byte=
   ($c3,$fc,$d3,$d7,$61,$92,$e4,$00,$7d,$fb,$49,$6c,$ca,$67,$e1,$3b);
var
 TestHash: TDCP_md5;
 TestOut: array[0..19] of byte;
begin
 TestHash:= TDCP_md5.Create(nil);
 TestHash.Init;
 TestHash.UpdateStr("abc");
 TestHash.Final(TestOut);
 Result:= CompareMem(@TestOut,@Test1Out,Sizeof(Test1Out));
 TestHash.Init;
 TestHash.UpdateStr("abcdefghijklmnopqrstuvwxyz");
 TestHash.Final(TestOut);
 Result:= CompareMem(@TestOut,@Test2Out,Sizeof(Test2Out)) and Result;
 TestHash.Free;
end;

procedure TDCP_md5.Init;
begin
 Burn;
 CurrentHash[0]:= $67452301;
 CurrentHash[1]:= $efcdab89;
 CurrentHash[2]:= $98badcfe;
 CurrentHash[3]:= $10325476;
 fInitialized:= true;
end;

procedure TDCP_md5.Burn;
begin
 LenHi:= 0; LenLo:= 0;
 Index:= 0;
 FillChar(HashBuffer,Sizeof(HashBuffer),0);
 FillChar(CurrentHash,Sizeof(CurrentHash),0);
 fInitialized:= false;
end;

procedure TDCP_md5.Update(const Buffer; Size: longword);
var
 PBuf: ^byte;
begin
 if not fInitialized then
   raise EDCP_hash.Create("Hash not initialized");

 Inc(LenHi,Size shr 29);
 Inc(LenLo,Size*8);
 if LenLo< (Size*8) then
   Inc(LenHi);

 PBuf:= @Buffer;
 while Size> 0 do
 begin
   if (Sizeof(HashBuffer)-Index)<= DWord(Size) then
   begin
     Move(PBuf^,HashBuffer[Index],Sizeof(HashBuffer)-Index);
     Dec(Size,Sizeof(HashBuffer)-Index);
     Inc(PBuf,Sizeof(HashBuffer)-Index);
     Compress;
   end
   else
   begin
     Move(PBuf^,HashBuffer[Index],Size);
     Inc(Index,Size);
     Size:= 0;
   end;
 end;
end;

procedure TDCP_md5.Final(var Digest);
begin
 if not fInitialized then
   raise EDCP_hash.Create("Hash not initialized");
 HashBuffer[Index]:= $80;
 if Index>= 56 then
   Compress;
 PDWord(@HashBuffer[56])^:= LenLo;
 PDWord(@HashBuffer[60])^:= LenHi;
 Compress;
 Move(CurrentHash,Digest,Sizeof(CurrentHash));
 Burn;
end;

end.


 
kaZaNoVa ©   (2005-01-18 14:06) [3]

unit DCPconst;

interface

{******************************************************************************}
const
 { Component registration }
 DCPcipherpage     = "DCPciphers";
 DCPhashpage       = "DCPhashes";

 { ID values }
 DCP_rc2           =  1;
 DCP_sha1          =  2;
 DCP_rc5           =  3;
 DCP_rc6           =  4;
 DCP_blowfish      =  5;
 DCP_twofish       =  6;
 DCP_cast128       =  7;
 DCP_gost          =  8;
 DCP_rijndael      =  9;
 DCP_ripemd160     = 10;
 DCP_misty1        = 11;
 DCP_idea          = 12;
 DCP_mars          = 13;
 DCP_haval         = 14;
 DCP_cast256       = 15;
 DCP_md5           = 16;
 DCP_md4           = 17;
 DCP_tiger         = 18;
 DCP_rc4           = 19;
 DCP_ice           = 20;
 DCP_thinice       = 21;
 DCP_ice2          = 22;
 DCP_des           = 23;
 DCP_3des          = 24;
 DCP_tea           = 25;
 DCP_serpent       = 26;
 DCP_ripemd128     = 27;
 DCP_sha256        = 28;
 DCP_sha384        = 29;
 DCP_sha512        = 30;

{******************************************************************************}
{******************************************************************************}
implementation

end.


 
kaZaNoVa ©   (2005-01-18 14:07) [4]

unit DCPcrypt2;

interface
uses
 Classes, Sysutils, DCPconst, DCPbase64;

//{$DEFINE DCP1COMPAT}  { DCPcrypt v1.31 compatiblity mode - see documentation }

{******************************************************************************}
   { A few predefined types to help out }

type
 Pbyte= ^byte;
 Pword= ^word;
 Pdword= ^dword;
 Pint64= ^int64;
 dword= longword;
 Pwordarray= ^Twordarray;
 Twordarray= array[0..19383] of word;
 Pdwordarray= ^Tdwordarray;
 Tdwordarray= array[0..8191] of dword;

{******************************************************************************}
   { The base class from which all hash algorithms are to be derived  }

type
 EDCP_hash= class(Exception);
 TDCP_hash= class(TComponent)
 protected
   fInitialized: boolean;  { Whether or not the algorithm has been initialized }

   procedure DeadInt(Value: integer);   { Knudge to display vars in the object inspector   }
   procedure DeadStr(Value: string);    { Knudge to display vars in the object inspector   }

 public
   property Initialized: boolean
     read fInitialized;

   class function GetId: integer; virtual; abstract;
     { Get the algorithm id }
   class function GetAlgorithm: string; virtual; abstract;
     { Get the algorithm name }
   class function GetHashSize: integer; virtual; abstract;
     { Get the size of the digest produced - in bits }
   class function SelfTest: boolean; virtual; abstract;
     { Tests the implementation with several test vectors }

   procedure Init; virtual; abstract;
     { Initialize the hash algorithm }
   procedure Final(var Digest); virtual; abstract;
     { Create the final digest and clear the stored information.
       The size of the Digest var must be at least equal to the hash size }
   procedure Burn; virtual; abstract;
     { Clear any stored information with out creating the final digest }

   procedure Update(const Buffer; Size: longword); virtual; abstract;
     { Update the hash buffer with Size bytes of data from Buffer }
   procedure UpdateStream(Stream: TStream; Size: longword); overload;
     { Update the hash buffer with Size bytes of data from the stream }
   procedure UpdateStream(Stream: TStream; Size: longword; BufferSize: longword); overload;
     { RM-Modification: overloaded function that allows to select buffer size }
   procedure UpdateStr(const Str: string);
     { Update the hash buffer with the string }

   destructor Destroy; override;

 published
   property Id: integer
     read GetId write DeadInt;
   property Algorithm: string
     read GetAlgorithm write DeadStr;
   property HashSize: integer
     read GetHashSize write DeadInt;
 end;
 TDCP_hashclass= class of TDCP_hash;

{******************************************************************************}
   { The base class from which all encryption components will be derived. }
   { Stream ciphers will be derived directly from this class where as     }
   { Block ciphers will have a further foundation class TDCP_blockcipher. }

type
 EDCP_cipher= class(Exception);
 TDCP_cipher= class(TComponent)
 protected
   fInitialized: boolean;  { Whether or not the key setup has been done yet }

   procedure DeadInt(Value: integer);   { Knudge to display vars in the object inspector   }
   procedure DeadStr(Value: string);    { Knudge to display vars in the object inspector   }

 public
   property Initialized: boolean
     read fInitialized;

   class function GetId: integer; virtual; abstract;
     { Get the algorithm id }
   class function GetAlgorithm: string; virtual; abstract;
     { Get the algorithm name }
   class function GetMaxKeySize: integer; virtual; abstract;
     { Get the maximum key size (in bits) }
   class function SelfTest: boolean; virtual; abstract;
     { Tests the implementation with several test vectors }

   procedure Init(const Key; Size: longword; InitVector: pointer); virtual;
     { Do key setup based on the data in Key, size is in bits }
   procedure InitStr(const Key: string; HashType: TDCP_hashclass);
     { Do key setup based on a hash of the key string }
   procedure Burn; virtual;
     { Clear all stored key information }
   procedure Reset; virtual; abstract;
     { Reset any stored chaining information }
   procedure Encrypt(const Indata; var Outdata; Size: longword); virtual; abstract;
     { Encrypt size bytes of data and place in Outdata }
   procedure Decrypt(const Indata; var Outdata; Size: longword); virtual; abstract;
     { Decrypt size bytes of data and place in Outdata }
   function EncryptStream(InStream, OutStream: TStream; Size: longword): longword;
     { Encrypt size bytes of data from InStream and place in OutStream }
   function DecryptStream(InStream, OutStream: TStream; Size: longword): longword;
     { Decrypt size bytes of data from InStream and place in OutStream }
   function EncryptString(const Str: string): string; virtual;
     { Encrypt a string and return Base64 encoded }
   function DecryptString(const Str: string): string; virtual;
     { Decrypt a Base64 encoded string }

   constructor Create(AOwner: TComponent); override;
   destructor Destroy; override;

 published
   property Id: integer
     read GetId write DeadInt;
   property Algorithm: string
     read GetAlgorithm write DeadStr;
   property MaxKeySize: integer
     read GetMaxKeySize write DeadInt;
 end;
 TDCP_cipherclass= class of TDCP_cipher;

{******************************************************************************}
   { The base class from which all block ciphers are to be derived, this  }
   { extra class takes care of the different block encryption modes.      }

type
 TDCP_ciphermode= (cmCBC, cmCFB8bit, cmCFBblock, cmOFB, cmCTR); // cmCFB8bit is equal to DCPcrypt v1.xx"s CFB mode
 EDCP_blockcipher= class(EDCP_cipher);
 TDCP_blockcipher= class(TDCP_cipher)
 protected


 
kaZaNoVa ©   (2005-01-18 14:07) [5]

   fCipherMode: TDCP_ciphermode;  { The cipher mode the encrypt method uses  }

   procedure InitKey(const Key; Size: longword); virtual; abstract;

 public
   class function GetBlockSize: integer; virtual; abstract;
     { Get the block size of the cipher (in bits) }

   procedure SetIV(const Value); virtual; abstract;
     { Sets the IV to Value and performs a reset }
   procedure GetIV(var Value); virtual; abstract;
     { Returns the current chaining information, not the actual IV }

   procedure Encrypt(const Indata; var Outdata; Size: longword); override;
     { Encrypt size bytes of data and place in Outdata using CipherMode }
   procedure Decrypt(const Indata; var Outdata; Size: longword); override;
     { Decrypt size bytes of data and place in Outdata using CipherMode }
   function EncryptString(const Str: string): string; override;
     { Encrypt a string and return Base64 encoded }
   function DecryptString(const Str: string): string; override;
     { Decrypt a Base64 encoded string }
   procedure EncryptECB(const Indata; var Outdata); virtual; abstract;
     { Encrypt a block of data using the ECB method of encryption }
   procedure DecryptECB(const Indata; var Outdata); virtual; abstract;
     { Decrypt a block of data using the ECB method of decryption }
   procedure EncryptCBC(const Indata; var Outdata; Size: longword); virtual; abstract;
     { Encrypt size bytes of data using the CBC method of encryption }
   procedure DecryptCBC(const Indata; var Outdata; Size: longword); virtual; abstract;
     { Decrypt size bytes of data using the CBC method of decryption }
   procedure EncryptCFB8bit(const Indata; var Outdata; Size: longword); virtual; abstract;
     { Encrypt size bytes of data using the CFB (8 bit) method of encryption }
   procedure DecryptCFB8bit(const Indata; var Outdata; Size: longword); virtual; abstract;
     { Decrypt size bytes of data using the CFB (8 bit) method of decryption }
   procedure EncryptCFBblock(const Indata; var Outdata; Size: longword); virtual; abstract;
     { Encrypt size bytes of data using the CFB (block) method of encryption }
   procedure DecryptCFBblock(const Indata; var Outdata; Size: longword); virtual; abstract;
     { Decrypt size bytes of data using the CFB (block) method of decryption }
   procedure EncryptOFB(const Indata; var Outdata; Size: longword); virtual; abstract;
     { Encrypt size bytes of data using the OFB method of encryption }
   procedure DecryptOFB(const Indata; var Outdata; Size: longword); virtual; abstract;
     { Decrypt size bytes of data using the OFB method of decryption }
   procedure EncryptCTR(const Indata; var Outdata; Size: longword); virtual; abstract;
     { Encrypt size bytes of data using the CTR method of encryption }
   procedure DecryptCTR(const Indata; var Outdata; Size: longword); virtual; abstract;
     { Decrypt size bytes of data using the CTR method of decryption }

   constructor Create(AOwner: TComponent); override;

 published
   property BlockSize: integer
     read GetBlockSize write DeadInt;
   property CipherMode: TDCP_ciphermode
     read fCipherMode write fCipherMode default cmCBC;
 end;
 TDCP_blockcipherclass= class of TDCP_blockcipher;

{******************************************************************************}
   { Helper functions }

procedure XorBlock(var InData1, InData2; Size: longword);

implementation
{$Q-}{$R-}

{** TDCP_hash *****************************************************************}

procedure TDCP_hash.DeadInt(Value: integer);
begin
end;

procedure TDCP_hash.DeadStr(Value: string);
begin
end;

procedure TDCP_hash.UpdateStream(Stream: TStream; Size: longword);
var
 Buffer: array[0..8191] of byte;
 i, read: integer;
begin
 for i:= 1 to (Size div Sizeof(Buffer)) do
 begin
   read:= Stream.Read(Buffer,Sizeof(Buffer));
   Update(Buffer,read);
 end;
 if (Size mod Sizeof(Buffer))<> 0 then
 begin
   read:= Stream.Read(Buffer,Size mod Sizeof(Buffer));
   Update(Buffer,read);
 end;
end;

procedure TDCP_hash.UpdateStream(Stream: TStream; Size: longword; BufferSize: longword);
//RM-Modification: overloaded function that allows to select buffer size
var
 Buffer: Pointer;
 i, read: integer;
begin
 GetMem(Buffer, BufferSize);
try
 for i:= 1 to (Size div BufferSize) do
 begin
   read:= Stream.Read(Buffer^,BufferSize);
   Update(Buffer^,read);
 end;
 if (Size mod BufferSize)<> 0 then
 begin
   read:= Stream.Read(Buffer^, Size mod BufferSize);
   Update(Buffer^,read);
 end;
finally
 FreeMem(Buffer, BufferSize);
end;
end;

procedure TDCP_hash.UpdateStr(const Str: string);
begin
 Update(Str[1],Length(Str));
end;

destructor TDCP_hash.Destroy;
begin
 if fInitialized then
   Burn;
 inherited Destroy;
end;

{** TDCP_cipher ***************************************************************}

procedure TDCP_cipher.DeadInt(Value: integer);
begin
end;

procedure TDCP_cipher.DeadStr(Value: string);
begin
end;

procedure TDCP_cipher.Init(const Key; Size: longword; InitVector: pointer);
begin
 if fInitialized then
   Burn;
 if (Size <= 0) or ((Size and 3)<> 0) or (Size> longword(GetMaxKeySize)) then
   raise EDCP_cipher.Create("Invalid key size")
 else
   fInitialized:= true;
end;

procedure TDCP_cipher.InitStr(const Key: string; HashType: TDCP_hashclass);
var
 Hash: TDCP_hash;
 Digest: pointer;
begin
 if fInitialized then
   Burn;
 try
   GetMem(Digest,HashType.GetHashSize div 8);
   Hash:= HashType.Create(Self);
   Hash.Init;
   Hash.UpdateStr(Key);
   Hash.Final(Digest^);
   Hash.Free;
   if MaxKeySize< HashType.GetHashSize then
     Init(Digest^,MaxKeySize,nil)
   else
     Init(Digest^,HashType.GetHashSize,nil);
   FillChar(Digest^,HashType.GetHashSize div 8,$FF);
   FreeMem(Digest);
 except
   raise EDCP_cipher.Create("Unable to allocate sufficient memory for hash digest");
 end;
end;

procedure TDCP_cipher.Burn;
begin


 
kaZaNoVa ©   (2005-01-18 14:07) [6]

 fInitialized:= false;
end;

function TDCP_cipher.EncryptStream(InStream, OutStream: TStream; Size: longword): longword;
var
 Buffer: array[0..8191] of byte;
 i, Read: longword;
begin
 Result:= 0;
 for i:= 1 to (Size div Sizeof(Buffer)) do
 begin
   Read:= InStream.Read(Buffer,Sizeof(Buffer));
   Inc(Result,Read);
   Encrypt(Buffer,Buffer,Read);
   OutStream.Write(Buffer,Read);
 end;
 if (Size mod Sizeof(Buffer))<> 0 then
 begin
   Read:= InStream.Read(Buffer,Size mod Sizeof(Buffer));
   Inc(Result,Read);
   Encrypt(Buffer,Buffer,Read);
   OutStream.Write(Buffer,Read);
 end;
end;

function TDCP_cipher.DecryptStream(InStream, OutStream: TStream; Size: longword): longword;
var
 Buffer: array[0..8191] of byte;
 i, Read: longword;
begin
 Result:= 0;
 for i:= 1 to (Size div Sizeof(Buffer)) do
 begin
   Read:= InStream.Read(Buffer,Sizeof(Buffer));
   Inc(Result,Read);
   Decrypt(Buffer,Buffer,Read);
   OutStream.Write(Buffer,Read);
 end;
 if (Size mod Sizeof(Buffer))<> 0 then
 begin
   Read:= InStream.Read(Buffer,Size mod Sizeof(Buffer));
   Inc(Result,Read);
   Decrypt(Buffer,Buffer,Read);
   OutStream.Write(Buffer,Read);
 end;
end;

function TDCP_cipher.EncryptString(const Str: string): string;
begin
 SetLength(Result,Length(Str));
 Encrypt(Str[1],Result[1],Length(Str));
 Result:= Base64EncodeStr(Result);
end;

function TDCP_cipher.DecryptString(const Str: string): string;
begin
 Result:= Base64DecodeStr(Str);
 Decrypt(Result[1],Result[1],Length(Result));
end;

constructor TDCP_cipher.Create(AOwner: TComponent);
begin
 inherited Create(AOwner);
 Burn;
end;

destructor TDCP_cipher.Destroy;
begin
 if fInitialized then
   Burn;
 inherited Destroy;
end;

{** TDCP_blockcipher **********************************************************}

procedure TDCP_blockcipher.Encrypt(const Indata; var Outdata; Size: longword);
begin
 case fCipherMode of
   cmCBC: EncryptCBC(Indata,Outdata,Size);
   cmCFB8bit: EncryptCFB8bit(Indata,Outdata,Size);
   cmCFBblock: EncryptCFBblock(Indata,Outdata,Size);
   cmOFB: EncryptOFB(Indata,Outdata,Size);
   cmCTR: EncryptCTR(Indata,Outdata,Size);
 end;
end;

function TDCP_blockcipher.EncryptString(const Str: string): string;
begin
 SetLength(Result,Length(Str));
 EncryptCFB8bit(Str[1],Result[1],Length(Str));
 Result:= Base64EncodeStr(Result);
end;

function TDCP_blockcipher.DecryptString(const Str: string): string;
begin
 Result:= Base64DecodeStr(Str);
 DecryptCFB8bit(Result[1],Result[1],Length(Result));
end;

procedure TDCP_blockcipher.Decrypt(const Indata; var Outdata; Size: longword);
begin
 case fCipherMode of
   cmCBC: DecryptCBC(Indata,Outdata,Size);
   cmCFB8bit: DecryptCFB8bit(Indata,Outdata,Size);
   cmCFBblock: DecryptCFBblock(Indata,Outdata,Size);
   cmOFB: DecryptOFB(Indata,Outdata,Size);
   cmCTR: DecryptCTR(Indata,Outdata,Size);
 end;
end;

constructor TDCP_blockcipher.Create(AOwner: TComponent);
begin
 inherited Create(AOwner);
 fCipherMode:= cmCBC;
end;

{** Helpher functions *********************************************************}
procedure XorBlock(var InData1, InData2; Size: longword);
var
 i: longword;
begin
 for i:= 1 to Size do
   Pbyte(longword(@InData1)+i-1)^:= Pbyte(longword(@InData1)+i-1)^ xor Pbyte(longword(@InData2)+i-1)^;
end;

end.



 
kaZaNoVa ©   (2005-01-18 14:08) [7]

unit DCPbase64;

interface
uses
 Sysutils;

//Added for compiling with FreePascal
type
 PByteArray = ^TByteArray;
 TByteArray = array[0..32767] of Byte;

function Base64EncodeStr(const Value: string): string;
 { Encode a string into Base64 format }
function Base64DecodeStr(const Value: string): string;
 { Decode a Base64 format string }
function Base64Encode(pInput: pointer; pOutput: pointer; Size: longint): longint;
 { Encode a lump of raw data (output is (4/3) times bigger than input) }
function Base64Decode(pInput: pointer; pOutput: pointer; Size: longint): longint;
 { Decode a lump of raw data }

{******************************************************************************}
{******************************************************************************}
implementation
{$Q-}{$R-}

const
 B64: array[0..63] of byte= (65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,
   81,82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,106,107,108,
   109,110,111,112,113,114,115,116,117,118,119,120,121,122,48,49,50,51,52,53,
   54,55,56,57,43,47);

function Base64Encode(pInput: pointer; pOutput: pointer; Size: longint): longint;
var
 i, iptr, optr: integer;
 Input, Output: PByteArray;
begin
 Input:= PByteArray(pInput); Output:= PByteArray(pOutput);
 iptr:= 0; optr:= 0;
 for i:= 1 to (Size div 3) do
 begin
   Output^[optr+0]:= B64[Input^[iptr] shr 2];
   Output^[optr+1]:= B64[((Input^[iptr] and 3) shl 4) + (Input^[iptr+1] shr 4)];
   Output^[optr+2]:= B64[((Input^[iptr+1] and 15) shl 2) + (Input^[iptr+2] shr 6)];
   Output^[optr+3]:= B64[Input^[iptr+2] and 63];
   Inc(optr,4); Inc(iptr,3);
 end;
 case (Size mod 3) of
   1: begin
        Output^[optr+0]:= B64[Input^[iptr] shr 2];
        Output^[optr+1]:= B64[(Input^[iptr] and 3) shl 4];
        Output^[optr+2]:= byte("=");
        Output^[optr+3]:= byte("=");
      end;
   2: begin
        Output^[optr+0]:= B64[Input^[iptr] shr 2];
        Output^[optr+1]:= B64[((Input^[iptr] and 3) shl 4) + (Input^[iptr+1] shr 4)];
        Output^[optr+2]:= B64[(Input^[iptr+1] and 15) shl 2];
        Output^[optr+3]:= byte("=");
      end;
 end;
 Result:= ((Size+2) div 3) * 4;
end;

function Base64EncodeStr(const Value: string): string;
begin
 SetLength(Result,((Length(Value)+2) div 3) * 4);
 Base64Encode(@Value[1],@Result[1],Length(Value));
end;

function Base64Decode(pInput: pointer; pOutput: pointer; Size: longint): longint;
var
 i, j, iptr, optr: integer;
 Temp: array[0..3] of byte;
 Input, Output: PByteArray;
begin
 Input:= PByteArray(pInput); Output:= PByteArray(pOutput);
 iptr:= 0; optr:= 0;
 Result:= 0;
 for i:= 1 to (Size div 4) do
 begin
   for j:= 0 to 3 do
   begin
     case Input^[iptr] of
       65..90 : Temp[j]:= Input^[iptr] - Ord("A");
       97..122: Temp[j]:= Input^[iptr] - Ord("a") + 26;
       48..57 : Temp[j]:= Input^[iptr] - Ord("0") + 52;
       43     : Temp[j]:= 62;
       47     : Temp[j]:= 63;
       61     : Temp[j]:= $FF;
     end;
     Inc(iptr);
   end;
   Output^[optr]:= (Temp[0] shl 2) or (Temp[1] shr 4);
   Result:= optr+1;
   if (Temp[2]<> $FF) and (Temp[3]= $FF) then
   begin
     Output^[optr+1]:= (Temp[1] shl 4) or (Temp[2] shr 2);
     Result:= optr+2;
     Inc(optr)
   end
   else if (Temp[2]<> $FF) then
   begin
     Output^[optr+1]:= (Temp[1] shl 4) or (Temp[2] shr 2);
     Output^[optr+2]:= (Temp[2] shl 6) or  Temp[3];
     Result:= optr+3;
     Inc(optr,2);
   end;
   Inc(optr);
 end;
end;

function Base64DecodeStr(const Value: string): string;
begin
 SetLength(Result,(Length(Value) div 4) * 3);
 SetLength(Result,Base64Decode(@Value[1],@Result[1],Length(Value)));
end;

end.


 
TUser ©   (2005-01-18 14:08) [8]

Здесь был?
http://www.torry.net/pages.php?id=519
http://www.torry.net/pages.php?id=312


 
kaZaNoVa ©   (2005-01-18 14:09) [9]

пример юзания
var
 Form1: TForm1;
 t:textfile;  kkk:boolean;
t1:file of byte;
 k:array[1..8000000] of string;
p2, p,p1,pk,n,kk:int64;  v,l,r:string;
   Digest: array[0..15] of Byte;
   q: array[0..15] of word;
   C,o:Integer;
   Hash: TDCP_md5;
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
Hash := TDCP_md5.Create(nil);
 Hash.Init;
 Hash.UpdateStr(edit1.Text);
 Hash.Final(Digest);
 Edit2.Text:="";
 for C:=Low(Digest) to High(Digest) do begin
  Edit2.Text :=Edit2.Text + IntToHex(Digest[C],2);
 end;
 Hash.Free;
end;



 
kaZaNoVa ©   (2005-01-18 14:10) [10]

ух, кажется всё запостил))))


 
ShimON ©   (2005-01-18 14:11) [11]

uses
Classes, Sysutils, DCPcrypt2, DCPconst;//

Не очень понял... А где DCPcrypt2, и нельзя ли примерчик простой...


 
ShimON ©   (2005-01-18 14:12) [12]

Спасибо :)


 
kaZaNoVa ©   (2005-01-18 14:13) [13]

ShimON ©   (18.01.05 14:11) [11]
всё запостил и пример тоже, пашет у меня 500 000 в секунду - делал перебор MD5

//но код не мой


 
Kerk ©   (2005-01-18 14:16) [14]

Казанова, ты реально достал! :)
Ты ссылку дать не мог? Нахрена столько сюда постить?


 
Amoeba ©   (2005-01-18 14:18) [15]

Между прочим готовый модуль есть здесь:
http://endimus.com/oldcontent/files/index_en.php
http://endimus.com/oldcontent/files/downloads/md5.zip


 
kaZaNoVa ©   (2005-01-18 14:28) [16]

Kerk ©   (18.01.05 14:16) [14]
ЛОЛ
я код Постил)))

а ссылки нет, тока у меня



Страницы: 1 вся ветка

Форум: "Основная";
Текущий архив: 2005.01.30;
Скачать: [xml.tar.bz2];

Наверх




Память: 0.55 MB
Время: 0.037 c
4-1102800675
Piter
2004-12-12 00:31
2005.01.30
Перехват функций


1-1105949734
VID
2005-01-17 11:15
2005.01.30
Как отключить автоматический горизонтальный скроллинг в TreeView


1-1105987607
redlord
2005-01-17 21:46
2005.01.30
TICQClient и регистрация нового пользователя


1-1105957994
glGLU
2005-01-17 13:33
2005.01.30
MessageDlg


1-1106050793
AlexLumpov
2005-01-18 15:19
2005.01.30
Уменьшить rtl70.bpl





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский