- 15
- 0
- 约2.17万字
- 约 34页
- 2021-10-30 发布于浙江
- 举报
Blowfish加密算法(Delphi源码)
Blowfish
{$define UNLIMITED} // WASSENAAR_LIMITED, UNLIMITED
unit Blowfish;
interface
uses
Classes, SysUtils;
const
{general constants}
BLOCKSIZE = 8; // Blowfish has an 8 byte block
BUFFERSIZE = 4096; // the buffer for file encryption
type SingleBytes = Record
byte3: Byte; {LSB}
byte2: BYTE;
byte1: BYTE;
byte0: BYTE; {MSB}
end;{SingleBytes}
type EKeyError = class(Exception);
type EFileError = class(Exception);
type EInputError = class(Exception);
type DoublWord = record
case Integer Of
0: (LWord: Longint);
1: (w: singleBytes);
2: (fByte: Array[0..3] of Byte);
end;{DoublWord}
type PDoublWord = ^DoublWord;
type TBlock = array[0..(BLOCKSIZE - 1)] of Byte; type PBlock = ^TBlock;
PArray = array[0..17] of Longint; SArray = array[0..255] of Longint;
type TCipherMode = (ECB, CBC, CFB, OFB); type TStringMode = (smEncode, smNormal); type Tblf_Core_ctx = record
P : PArray; //P数组
S1: SArray; //第1个S盒
S2: SArray; //第2个S盒
S3: SArray; //第3个S盒
S4: SArray; //第4个S盒
case Integer of // Encryption/decryption block buffer
0: (ByteBuffer: TBlock);
1: (LongBuffer: array[0..1] of LongInt);
end; {Tblf_ctx}
type Tblf_ctx = record
KeyInit: Boolean; // Shows if the password has been initialised
IVInit: Boolean; // Shows if the IV has been initialised
IV: TBlock; // The Initialisation Vector
ct: TBlock; // temporary buffer used in CBC, CFB and OFB modes
end; {Tblf_ctx}
type TBlowCore = class(TObject)
ctx: Tblf_Core_ctx;
FPtrL: PDoublWord; //指向低32位数据
FPtrR: PDoublWord; //指向高32位数据
public
procedure Blowfish_Core_Block_Encrypt;
procedure Blowfish_Core_Block_Decrypt;
end; {TBLowCore}
type TBlowfish = class(TObject)
private
FBlowCore: TBlowCore;
ctx: Tblf_ctx;
FBuffer: array[0..BUFFERSIZE+BLOCKSIZE] of BYTE; {Local Copy of Data}
PtrBuffer: PBlock;
FCipherMode: TCipherMode;
FStringMode: TStringMode;
procedure Blowfish_Core_Key_Setup(KeyArray: array of Byte; const KeyLength:
integer);
procedure EncryptBlockMode;
proced
原创力文档

文档评论(0)