?? myldbcipher.pas
字號:
unit MYLDBCipher;
interface
{$I MYLDBVer.Inc}
{$I VER.INC}
uses SysUtils, Classes,MYLDBDecUtil;
const
Rijndael_Cipher = 1;
RIPEMD_128_Hash = 0;
RIPEMD_256_Hash = 1;
Default_Cipher_Method = Rijndael_Cipher;
Default_Hash_Method = RIPEMD_128_Hash;
const {ErrorCode's for ECipherException}
errGeneric = 0; {generic Error}
errInvalidKey = 1; {Decode Key is not correct}
errInvalidKeySize = 2; {Size of the Key is too large}
errNotInitialized = 3; {Methods Init() or InitKey() were not called}
errInvalidMACMode = 4; {CalcMAC can't use cmECB, cmOFB}
errCantCalc = 5;
type
ECipherException = class(Exception)
public
ErrorCode: Integer;
end;
type
{all Cipher Classes in this Unit, a good Selection}
TCipherMode = (cmCTS, cmCBC, cmCFB, cmOFB, cmECB, cmCTSMAC, cmCBCMAC, cmCFBMAC);
{ the Cipher Modes:
cmCTS Cipher Text Stealing, a Variant from cmCBC, but relaxes
the restriction that the DataSize must be a mulitply from BufSize,
this is the Defaultmode, fast and Bytewise
cmCBC Cipher Block Chaining
cmCFB K-bit Cipher Feedback, here is K = 8 -> 1 Byte
cmOFB K-bit Output Feedback, here is K = 8 -> 1 Byte
cmECB * Electronic Codebook, DataSize must be a multiply from BufSize
cmCTSMAC Build a Message Authentication Code in cmCTS Mode
cmCBCMAC Build a CBC-MAC
cmCFBMAC Build a CFB-MAC
}
TCipherClass = class of TCipher;
//-------- from hash ------------
// hash
{the Base-Class of all Hashs}
THash = class(TProtection)
protected
class function TestVector: Pointer; virtual; {must override}
procedure CodeInit(Action: TPAction); override; {TProtection Methods, You can use a Hash to En/Decode}
procedure CodeDone(Action: TPAction); override; {TProtection Methods}
procedure CodeBuf(var Buffer; const BufferSize: Integer; Action: TPAction); override; {TProtection Methods}
procedure Protect(IsInit: Boolean); {calls any installed Protection}
public
destructor Destroy; override;
procedure Init; virtual;
procedure Calc(const Data; DataSize: Integer); virtual; {must override}
procedure Done; virtual;
function DigestKey: Pointer; virtual; {must override}
function DigestStr(Format: Integer): String;
class function DigestKeySize: Integer; virtual; {must override}
{$IFDEF VER_D4H} // new features from D4
class function CalcBuffer(const Buffer; BufferSize: Integer; Protection: TProtection = nil; Format: Integer = fmtDEFAULT): String; overload;
class function CalcStream(const Stream: TStream; StreamSize: Integer; Protection: TProtection = nil; Format: Integer = fmtDEFAULT): String; overload;
class function CalcString(const Data: String; Protection: TProtection = nil; Format: Integer = fmtDEFAULT): String; overload;
class function CalcFile(const FileName: String; Protection: TProtection = nil; Format: Integer = fmtDEFAULT): String; overload;
{$ELSE}
class function CalcBuffer(const Buffer; BufferSize: Integer; Protection: TProtection; Format: Integer): String;
class function CalcStream(const Stream: TStream; StreamSize: Integer; Protection: TProtection; Format: Integer): String;
class function CalcString(const Data: String; Protection: TProtection; Format: Integer): String;
class function CalcFile(const FileName: String; Protection: TProtection; Format: Integer): String;
{$ENDIF}
{test the correct working}
class function SelfTest: Boolean;
end;
{
// HMAC's - Hash Message Authentication Code's
TMAC = class(TProtection) // generic MAC, encrypt Password with any AProtection and XOR's with
protected // the Initstate from the Hash (DigestKey)
FPassword: String; // final Digest is encrypted with AProtecion
procedure Init(Hash: THash); virtual; // Only used in all THash Descends
procedure Done(Hash: THash); virtual; // Only used in all THash Descends
public
constructor Create(const Password: String; AProtection: TProtection);
destructor Destroy; override;
end;
}
//-------- from hash end ------------
THashClass = class of THash;
TCipher = class(TProtection)
private
FMode: TCipherMode;
FHash: THash;
FHashClass: THashClass;
FKeySize: Integer;
FBufSize: Integer;
FUserSize: Integer;
FBuffer: Pointer;
FVector: Pointer;
FFeedback: Pointer;
FUser: Pointer;
FFlags: Integer;
function GetHash: THash;
procedure SetHashClass(Value: THashClass);
protected
function GetFlag(Index: Integer): Boolean;
procedure SetFlag(Index: Integer; Value: Boolean); virtual;
{used in method Init()}
procedure InitBegin(var Size: Integer);
procedure InitEnd(IVector: Pointer); virtual;
{must override}
class procedure GetContext(var ABufSize, AKeySize, AUserSize: Integer); virtual;
class function TestVector: Pointer; virtual;
{override TProtection Methods}
procedure CodeInit(Action: TPAction); override;
procedure CodeDone(Action: TPAction); override;
procedure CodeBuf(var Buffer; const BufferSize: Integer; Action: TPAction); override;
{the encode function, must override}
procedure Encode(Data: Pointer); virtual;
{the decode function, must override}
procedure Decode(Data: Pointer); virtual;
{the individual Userdata and Buffer}
property User: Pointer read FUser;
property Buffer: Pointer read FBuffer;
property UserSize: Integer read FUserSize;
public
constructor Create(const Password: String; AProtection: TProtection);
destructor Destroy; override;
class function MaxKeySize: Integer;
{performs a Test of correct work}
class function SelfTest: Boolean;
{initialization form the Cipher}
procedure Init(const Key; Size: Integer; IVector: Pointer); virtual;
procedure InitKey(const Key: String; IVector: Pointer);
{reset the Feedbackregister with the actual IVector}
procedure Done; virtual;
{protect the security Data's, Feedback, Buffer, Vector etc.}
procedure Protect; virtual;
procedure EncodeBuffer(const Source; var Dest; DataSize: Integer);
procedure DecodeBuffer(const Source; var Dest; DataSize: Integer);
{the Cipher Mode = cmXXX}
property Mode: TCipherMode read FMode write FMode;
{the Current Hash-Object, to build a Digest from InitKey()}
property Hash: THash read GetHash;
{the Class of the Hash-Object}
property HashClass: THashClass read FHashClass write SetHashClass;
{the maximal KeySize and BufSize (Size of Feedback, Buffer and Vector}
property KeySize: Integer read FKeySize;
property BufSize: Integer read FBufSize;
{Init() was called}
property Initialized: Boolean index 1 read GetFlag write SetFlag;
{the actual IVector, BufSize Bytes long}
property Vector: Pointer read FVector;
{the Feedback register, BufSize Bytes long}
property Feedback: Pointer read FFeedback;
{the Key is set from InitKey() and the Hash.DigestKey^ include the encrypted Hash-Key}
property HasHashKey: Boolean index 0 read GetFlag;
end;
// now the Cipher's
function DefaultCipherClass: TCipherClass;
procedure SetDefaultCipherClass(CipherClass: TCipherClass);
procedure RaiseCipherException(const ErrorCode: Integer; const Msg: String);
function RegisterCipher(const ACipher: TCipherClass; const AName, ADescription: String): Boolean;
function UnregisterCipher(const ACipher: TCipherClass): Boolean;
function CipherList: TStrings;
procedure CipherNames(List: TStrings);
function GetCipherClass(const Name: String): TCipherClass;
function GetCipherName(CipherClass: TCipherClass): String;
const
CheckCipherKeySize: Boolean = False;
{set to True raises Exception when Size of the Key is too large, (Method Init())
otherwise will truncate the Key, default mode is False}
//------------ from cipher1 -----------
type
TCipher_Rijndael = class(TCipher)
private
FRounds: Integer;
protected
class procedure GetContext(var ABufSize, AKeySize, AUserSize: Integer); override;
class function TestVector: Pointer; override;
procedure Encode(Data: Pointer); override;
procedure Decode(Data: Pointer); override;
public
procedure Init(const Key; Size: Integer; IVector: Pointer); override;
end;
TCipher_Blowfish = class(TCipher)
private
{$IFDEF UseASM}
{$IFNDEF 486GE} // no Support for <= CPU 386
procedure Encode386(Data: Pointer);
procedure Decode386(Data: Pointer);
{$ENDIF}
{$ENDIF}
protected
class procedure GetContext(var ABufSize, AKeySize, AUserSize: Integer); override;
class function TestVector: Pointer; override;
procedure Encode(Data: Pointer); override;
procedure Decode(Data: Pointer); override;
public
procedure Init(const Key; Size: Integer; IVector: Pointer); override;
end;
TCipher_1DES = class(TCipher)
protected
class procedure GetContext(var ABufSize, AKeySize, AUserSize: Integer); override;
class function TestVector: Pointer; override;
procedure Encode(Data: Pointer); override;
procedure Decode(Data: Pointer); override;
procedure MakeKey(const Data : array of byte; Key_1: pointer; Reverse: Boolean);
// procedure MakeKey(const Data: array of Byte; Key: PInteger; Reverse: Boolean);
public
procedure Init(const Key; Size: Integer; IVector: Pointer); override;
end;
TCipher_3DES = class(TCipher_1DES)
protected
class procedure GetContext(var ABufSize, AKeySize, AUserSize: Integer); override;
class function TestVector: Pointer; override;
procedure Encode(Data: Pointer); override;
procedure Decode(Data: Pointer); override;
public
procedure Init(const Key; Size: Integer; IVector: Pointer); override;
end;
TCipher_3TDES = class(TCipher_3DES)
protected
class procedure GetContext(var ABufSize, AKeySize, AUserSize: Integer); override;
class function TestVector: Pointer; override;
procedure Encode(Data: Pointer); override;
procedure Decode(Data: Pointer); override;
end;
TCipher_Twofish = class(TCipher)
protected
class procedure GetContext(var ABufSize, AKeySize, AUserSize: Integer); override;
class function TestVector: Pointer; override;
procedure Encode(Data: Pointer); override;
procedure Decode(Data: Pointer); override;
public
procedure Init(const Key; Size: Integer; IVector: Pointer); override;
end;
TCipher_Square = class(TCipher)
protected
class procedure GetContext(var ABufSize, AKeySize, AUserSize: Integer); override;
class function TestVector: Pointer; override;
procedure Decode(Data: Pointer); override;
public
procedure Init(const Key; Size: Integer; IVector: Pointer); override;
end;
//------------ from cipher1 end -----------
//------------ from hash -----------
THash_MD4 = class(THash)
private
FCount: LongWord;
FBuffer: array[0..63] of Byte;
FDigest: array[0..9] of LongWord;
protected
class function TestVector: Pointer; override;
procedure Transform(Buffer: PIntArray); virtual;
public
class function DigestKeySize: Integer; override;
procedure Init; override;
procedure Done; override;
procedure Calc(const Data; DataSize: Integer); override;
function DigestKey: Pointer; override;
end;
THash_RipeMD128 = class(THash_MD4) {RACE Integrity Primitives Evaluation Message Digest}
protected
class function TestVector: Pointer; override;
procedure Transform(Buffer: PIntArray); override;
end;
THash_RipeMD256 = class(THash_MD4)
protected
class function TestVector: Pointer; override;
procedure Transform(Buffer: PIntArray); override;
public
{DigestKey-Size 256 bit}
class function DigestKeySize: Integer; override;
procedure Init; override;
end;
// check sum
TChecksum = class(THash);
// general
function DefaultHashClass: THashClass;
procedure SetDefaultHashClass(HashClass: THashClass);
function RegisterHash(const AHash: THashClass; const AName, ADescription: String): Boolean;
function UnregisterHash(const AHash: THashClass): Boolean;
function HashList: TStrings;
procedure HashNames(List: TStrings);
function GetHashClass(const Name: String): THashClass;
function GetHashName(HashClass: THashClass): String;
//------------ from hash end -----------
implementation
uses MYLDBDecConst2, Windows;
{$I cipher1.inc}
const
FDefaultHashClass: THashClass = THash_RipeMD256;
FDefaultCipherClass : TCipherClass = TCipher_Rijndael;
FCipherList : TStringList = nil;
FHashList: TStringList = nil;
function DefaultCipherClass: TCipherClass;
begin
Result := FDefaultCipherClass;
end;
procedure SetDefaultCipherClass(CipherClass: TCipherClass);
begin
if CipherClass = nil then FDefaultCipherClass := TCipher_Rijndael
else FDefaultCipherClass := CipherClass;
end;
procedure RaiseCipherException(const ErrorCode: Integer; const Msg: String);
var
E: ECipherException;
begin
E := ECipherException.Create(Msg);
E.ErrorCode := ErrorCode;
raise E;
end;
function RegisterCipher(const ACipher: TCipherClass; const AName, ADescription: String): Boolean;
var
I: Integer;
S: String;
begin
Result := False;
if ACipher = nil then Exit;
S := Trim(AName);
if S = '' then
begin
S := ACipher.ClassName;
if S[1] = 'T' then Delete(S, 1, 1);
I := Pos('_', S);
if I > 0 then Delete(S, 1, I);
end;
S := S + '=' + ADescription;
I := CipherList.IndexOfObject(Pointer(ACipher));
if I < 0 then CipherList.AddObject(S, Pointer(ACipher))
else CipherList[I] := S;
Result := True;
end;
function UnregisterCipher(const ACipher: TCipherClass): Boolean;
var
I: Integer;
begin
Result := False;
repeat
I := CipherList.IndexOfObject(Pointer(ACipher));
if I < 0 then Break;
Result := True;
CipherList.Delete(I);
until False;
end;
function CipherList: TStrings;
begin
if not IsObject(FCipherList, TStringList) then FCipherList := TStringList.Create;
Result := FCipherList;
end;
procedure CipherNames(List: TStrings);
var
I: Integer;
begin
if not IsObject(List, TStrings) then Exit;
for I := 0 to CipherList.Count-1 do
List.AddObject(FCipherList.Names[I], FCipherList.Objects[I]);
end;
function GetCipherClass(const Name: String): TCipherClass;
var
I: Integer;
N: String;
begin
Result := nil;
N := Name;
I := Pos('_', N);
if I > 0 then Delete(N, 1, I);
for I := 0 to CipherList.Count-1 do
if AnsiCompareText(N, GetShortClassName(TClass(FCipherList.Objects[I]))) = 0 then
begin
Result := TCipherClass(FCipherList.Objects[I]);
Exit;
end;
I := FCipherList.IndexOfName(N);
if I >= 0 then Result := TCipherClass(FCipherList.Objects[I]);
end;
function GetCipherName(CipherClass: TCipherClass): String;
var
I: Integer;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -