哪里可以找到用于加密的des64.dll(200分)

  • 主题发起人 主题发起人 duancy
  • 开始时间 开始时间
D

duancy

Unregistered / Unconfirmed
GUEST, unregistred user!
急!<br>哪里可以找到用于加密的des64.dll
 
这里是DES加密源码:<br>{*******************************************************************************<br>* Unit &nbsp; &nbsp; &nbsp;: TDES &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Encapulates the DES cipher cipher with various cipher modes &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Copyright : This component is copyright TSM Inc. 1999 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; This source code may not be distributed to third parties in &nbsp; &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; or in part without the written permission of TSM Inc. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; All rights reserved. Liability limited to replacement of &nbsp; &nbsp; &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this original source code in the case of loss or damage because &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the use or misuse of this software. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Version &nbsp; : 25.02.98 &nbsp;- 1.0 &nbsp; Original component &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 08.03.98 &nbsp;- 1.01 &nbsp;Additional cipher modes added &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 08.04.98 &nbsp;- 1.02 &nbsp;Fix OFB mode &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 20.07.98 &nbsp;- 1.03 &nbsp;Added base64 encoding to en/decrypt string &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 13.08.98 &nbsp;- 1.03a Changed GetVersion to deliver reg info &nbsp; &nbsp; &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 15.08.98 &nbsp;- 1.10 &nbsp;Delphi4 / CBC MAC support added &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 05.10.98 &nbsp;- 1.11 &nbsp;Added Stream functionality &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 22.01.99 &nbsp;- 1.14 &nbsp;Error in En- &amp; Decrypt file/ stream resolved &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 26.03.99 &nbsp;- 1.14a Init error in "InitialiseString" fixed &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Switches &nbsp;: REGISTERED - Compiles in the registered mode (without nag msg.) &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; COMPONENT &nbsp;- Compiles as a VCL component (otherwise unit) &nbsp; &nbsp; &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; COMMANDLINE- Allows override of mode from command line compiler &nbsp;*<br>*******************************************************************************}<br><br>{********************************************************************************<br>&nbsp; 修改 &nbsp;: &nbsp; &nbsp;由于控件在对能被4K整除文件解密时,会产生内存访问错误,至使导致<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;解密失败,而出现乱码现象,现经过修改,修改用 '// 2001.6.12'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;作标示,并把以前的代码进行了注释。<br>&nbsp; 修改日期: 2001.6.12<br>*******************************************************************************}<br>{********************************************************************************<br>&nbsp; 修改 &nbsp;: &nbsp; &nbsp;支持源数据CRC校验,并可置初值<br>&nbsp; 修改日期: 2001.11.2<br>*******************************************************************************}<br>{********************************************************************************<br>&nbsp; 修改 &nbsp;: &nbsp; &nbsp;支持输入输出流的后部份数据的加密解密<br>&nbsp; 修改日期: 2001.11.3<br>*******************************************************************************}<br><br>{$R-} {$A-} {$Q-}<br>{$ifdef VER125}<br>&nbsp; &nbsp; &nbsp;// fix for CB4<br>&nbsp; &nbsp; &nbsp;{$define VER120}<br>{$endif}<br>{$ifdef VER130}<br>&nbsp; &nbsp; &nbsp;// fix for Delphi5<br>&nbsp; &nbsp; &nbsp;{$define VER120}<br>{$endif}<br><br>{$ifndef COMMANDLINE} &nbsp; &nbsp; &nbsp; &nbsp;// Commanline overrides the follwing presets<br>&nbsp; &nbsp; &nbsp;{$define NOTREGISTERED} //(NOTREGISTERED, FREEWARE, REGISTERED)<br>&nbsp; &nbsp; &nbsp;{$define COMPONENT} &nbsp; &nbsp; //(COMPONENT, OBJECT)<br>{$endif}<br><br>&nbsp; {**************************************************************************<br>&nbsp; ************ This section of code implements the 64 bit limit *************<br>&nbsp; ************ imposed by the Wassennar agreement. The key is &nbsp; *************<br>&nbsp; ************ limited to 64 bits. Should you be in a country &nbsp; *************<br>&nbsp; ************ where the Wassennar agreement is not in force, &nbsp; *************<br>&nbsp; ************ undefine the WASSENAAR_LIMITED variable. &nbsp; &nbsp; &nbsp; &nbsp; *************<br>&nbsp; **************************************************************************}<br>{$define UNLIMITED} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// WASSENAAR_LIMITED, UNLIMITED<br><br>unit des;<br><br>interface<br><br>uses<br>&nbsp; &nbsp; &nbsp;Classes, SysUtils, crc;<br><br>const<br>&nbsp; &nbsp; &nbsp;{general constants}<br>&nbsp; &nbsp; &nbsp;BLOCKSIZE &nbsp; &nbsp; &nbsp; = 8; &nbsp; &nbsp;// DES has an 8 byte block<br>&nbsp; &nbsp; &nbsp;BUFFERSIZE &nbsp; &nbsp; &nbsp;= 4096; // the buffer for file encryption<br><br><br><br>{*******************************************************************************<br>* Type &nbsp; &nbsp; &nbsp;: LongWord (only for D2/D3/CB3) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Defines the Longword type. This is native for D4,D5,CB4 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>*******************************************************************************}<br>{$ifndef VER120}<br>type LongWord = Integer;<br>{$endif}<br><br>{*******************************************************************************<br>* Type &nbsp; &nbsp; &nbsp;: PInteger &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Defines the pointer to the LongWordType &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>*******************************************************************************}<br>type PInteger &nbsp;= ^LongWord;<br><br>{*******************************************************************************<br>* Type &nbsp; &nbsp; &nbsp;: TIntArray &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Defines an array large enough for all purposes &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>type TIntArray = array[0..1023] of LongWord;<br><br>{*******************************************************************************<br>* Type &nbsp; &nbsp; &nbsp;: PIntArray &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Defines the pointer to the IntArray type &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>type PIntArray = ^TIntArray;<br><br>{*******************************************************************************<br>* Type &nbsp; &nbsp; &nbsp;: EKeyError &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Exception raised when key setup incomplete for a given mode &nbsp; &nbsp; &nbsp;*<br>*******************************************************************************}<br>type EKeyError = class(Exception);<br><br>{*******************************************************************************<br>* Type &nbsp; &nbsp; &nbsp;: EFileError &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Exception raised when file manipulation creates an error &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>type EFileError = class(Exception);<br><br>{*******************************************************************************<br>* Type &nbsp; &nbsp; &nbsp;: EInputError &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Exception raised when the input string for decryptstring suspect *<br>*******************************************************************************}<br>type EInputError = class(Exception);<br><br>{*******************************************************************************<br>* Type &nbsp; &nbsp; &nbsp;: TBlock &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Defines the basic element of enc/decryption &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>*******************************************************************************}<br>type TBlock = array[0..(BLOCKSIZE - 1)] of Byte;<br><br>{*******************************************************************************<br>* Type &nbsp; &nbsp; &nbsp;: PBlock &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Pointer to the type TBlock &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>type PBlock &nbsp;= ^TBlock;<br><br>{*******************************************************************************<br>* Type &nbsp; &nbsp; &nbsp;: TDES_ctx &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Defines context variable for TDES &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>*******************************************************************************}<br>type Tdes_ctx = record<br>&nbsp; &nbsp; &nbsp;KeyInit: &nbsp; &nbsp; &nbsp; &nbsp; Boolean; &nbsp;// Shows if the password has been initialised<br>&nbsp; &nbsp; &nbsp;IVInit: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Boolean; &nbsp;// Shows if the IV has been initialised<br>&nbsp; &nbsp; &nbsp;IV: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TBlock;<br>&nbsp; &nbsp; &nbsp;ct: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TBlock;<br><br>&nbsp; &nbsp; &nbsp;FEncKey: &nbsp; &nbsp; &nbsp; &nbsp; array [0..31] of LongWord;<br>&nbsp; &nbsp; &nbsp;FDecKey: &nbsp; &nbsp; &nbsp; &nbsp; array [0..31] of LongWord;<br><br>&nbsp; &nbsp; &nbsp;case Integer of<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0: (ByteBuffer: TBlock);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1: (LongBuffer: array[0..1] of LongInt);<br>end; {Tdes_ctx}<br><br>{*******************************************************************************<br>* Type &nbsp; &nbsp; &nbsp;: TCiphermode &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Defines possible modes for TDES &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>*******************************************************************************}<br>type TCipherMode = (CBC, ECB, CFB, OFB);<br><br>{*******************************************************************************<br>* Type &nbsp; &nbsp; &nbsp;: TStringMode &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Defines possible string modes for the En/Decrypt string &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>*******************************************************************************}<br>type TStringMode = (smEncode, smNormal);<br><br>{*******************************************************************************<br>* Type &nbsp; &nbsp; &nbsp;: TDES &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Defines TDES &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>{$ifdef COMPONENT}<br>type TDES = class(TComponent)<br>{$else}<br>type TDES = class(TObject)<br>{$endif}<br>&nbsp; private<br>&nbsp; &nbsp; &nbsp;ctx: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Tdes_ctx;<br><br>&nbsp; &nbsp; &nbsp;// buffer for larger encryptions<br>&nbsp; &nbsp; &nbsp;FBuffer: &nbsp; &nbsp; &nbsp;array[0..BUFFERSIZE+BLOCKSIZE] of BYTE; {Local Copy of Data}<br>&nbsp; &nbsp; &nbsp;PtrBuffer: &nbsp; &nbsp;PBlock;<br>&nbsp; &nbsp; &nbsp;FCipherMode: &nbsp;TCipherMode;<br>&nbsp; &nbsp; &nbsp;FStringMode: &nbsp;TStringMode;<br>&nbsp; &nbsp; &nbsp;FCrcCaclFlage:Boolean;<br>&nbsp; &nbsp; &nbsp;FCrc32Value: &nbsp;Integer;<br>&nbsp; &nbsp; &nbsp;FEncDecNoPosFlage:Boolean;<br><br>&nbsp; &nbsp; &nbsp;// these routines link to the core block routines of the algorithm<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; DES_Core_Key_Setup(const KeyToSet: TBlock);<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; DES_Core_Block_Encrypt;<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; DES_Core_Block_Decrypt;<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; DES_core_make_key(const Data: array of Byte; Key: PInteger; Reverse: Boolean);<br><br>&nbsp; &nbsp; &nbsp;// Internal encryption primitives<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; EncryptBuffer(const Len: integer);<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; DecryptBuffer(const Len: integer);<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; EncryptBlockMode;<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; DecryptBlockMode;<br><br>&nbsp; &nbsp; &nbsp;// Base64 functions<br>&nbsp; &nbsp; &nbsp;function &nbsp; &nbsp; &nbsp;EncodeString(InputString: string): string;<br>&nbsp; &nbsp; &nbsp;function &nbsp; &nbsp; &nbsp;DecodeString(InputString: string): string;<br><br>&nbsp; &nbsp; &nbsp;// Implemnetation primitives<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; CheckKeys;<br>&nbsp; public<br>&nbsp; &nbsp; &nbsp;// these calls are used to load the key and the IV<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; InitialiseString(const Key: string);<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; InitialiseByte(const Key: array of byte; Keylength: integer);<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; LoadIVString(const IVString: string);<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; LoadIVByte(const IVByte: array of Byte; IVLength: integer);<br><br>&nbsp; &nbsp; &nbsp;// These calls perform the operation using the mode specified in CipherMode<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; EncBlock(const Input: TBlock; var Output: TBlock);<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; DecBlock(const Input: TBlock; var Output: TBlock);<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; EncFile(const InputFileName: string; OutputFileName: string);<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; DecFile(const InputFileName: string; OutputFileName: string);<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; EncStream(const Input: TStream; const Output: TStream);<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; DecStream(const Input: TStream; const Output: TStream);<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; EncString(const Input: string; var Output: string);<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; DecString(const Input: string; var Output: string);<br><br>&nbsp; &nbsp; &nbsp;// this returns the CBC-MAC of the data put through DES<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; CBCMACBlock(var MAC:TBlock);<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; CBCMACString(var MAC: string);<br><br>&nbsp; &nbsp; &nbsp;// Burn clears any sensitive information<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; Burn;<br><br>&nbsp; &nbsp; &nbsp;// returns the version of the component<br>&nbsp; &nbsp; &nbsp;function &nbsp; &nbsp; &nbsp;GetVersion: string;<br><br>&nbsp; {$ifdef COMPONENT}<br>&nbsp; published<br>&nbsp; &nbsp; &nbsp;property &nbsp; &nbsp; &nbsp;CipherMode: &nbsp; TCipherMode read FCipherMode write FCipherMode;<br>&nbsp; &nbsp; &nbsp;property &nbsp; &nbsp; &nbsp;StringMode: &nbsp; TStringMode read FStringMode write FStringMode;<br>&nbsp; {$else}<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; SetCipherMode(const Value: TCipherMode);<br>&nbsp; &nbsp; &nbsp;function &nbsp; &nbsp; &nbsp;GetCipherMode: TCipherMode;<br>&nbsp; &nbsp; &nbsp;procedure &nbsp; &nbsp; SetStringMode(const Value: TStringMode);<br>&nbsp; &nbsp; &nbsp;function &nbsp; &nbsp; &nbsp;GetStringMode: TStringMode;<br>&nbsp; {$endif}<br>&nbsp; &nbsp; &nbsp;property &nbsp; &nbsp; &nbsp;CrcCaclFlage:Boolean read FCrcCaclFlage write FCrcCaclFlage default False;<br>&nbsp; &nbsp; &nbsp;property &nbsp; &nbsp; &nbsp;Crc32Value:Integer read FCrc32Value write FCrc32Value default 0;<br>&nbsp; &nbsp; &nbsp;property &nbsp; &nbsp; &nbsp;EncDecNoPosFlage:Boolean read FEncDecNoPosFlage write FEncDecNoPosFlage default False;<br><br>end;<br><br>{$ifdef COMPONENT}<br>procedure Register;<br>{$endif}<br><br>implementation<br><br>uses Windows, Dialogs;<br><br>const<br>&nbsp; &nbsp; &nbsp;// new implementation<br>{this is set to SwapInt for &lt;= 386 and BSwapInt &gt;= 486 CPU, don't modify}<br>&nbsp; SwapInteger &nbsp; &nbsp; &nbsp; : function(Value: LongWord): LongWord &nbsp;= nil;<br><br><br>&nbsp; &nbsp; &nbsp;RELVER = '1.15'; &nbsp; &nbsp; &nbsp; &nbsp;// Version number<br>&nbsp; &nbsp; &nbsp;LIT_COMPNAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= 'DES';<br>&nbsp; &nbsp; &nbsp;LIT_KEY_NOT_SET &nbsp; &nbsp; &nbsp; &nbsp; = LIT_COMPNAME + ': Key not set';<br>&nbsp; &nbsp; &nbsp;LIT_IV_NOT_SET &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= LIT_COMPNAME + ': IV not set';<br>&nbsp; &nbsp; &nbsp;LIT_KEY_LENGTH &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= LIT_COMPNAME + ': Key must be between 1 and 8 bytes';<br>&nbsp; &nbsp; &nbsp;LIT_INFILE_NOT_FOUND &nbsp; &nbsp;= LIT_COMPNAME + ': Input file not found';<br>&nbsp; &nbsp; &nbsp;LIT_CBC_NOT_SET &nbsp; &nbsp; &nbsp; &nbsp; = LIT_COMPNAME + ': Mode must be CBC for CBCMAC';<br>&nbsp; &nbsp; &nbsp;LIT_OUTFILE_OPEN_ERROR &nbsp;= LIT_COMPNAME + ': Could not open output file';<br>&nbsp; &nbsp; &nbsp;LIT_OUTFILE_WRITE_ERROR = LIT_COMPNAME + ': Error writing output file';<br>&nbsp; &nbsp; &nbsp;LIT_INPUT_LENGTH &nbsp; &nbsp; &nbsp; &nbsp;= LIT_COMPNAME + ': Input not valid - too short';<br>&nbsp; &nbsp; &nbsp;LIT_BASE64CNV &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = LIT_COMPNAME + ': Error converting from Base64 - invalid character';<br><br>&nbsp; &nbsp; &nbsp;DES_Data: array[0..7, 0..63] of LongWord = (<br>&nbsp; &nbsp; ($00200000,$04200002,$04000802,$00000000,$00000800,$04000802,$00200802,$04200800,<br>&nbsp; &nbsp; &nbsp;$04200802,$00200000,$00000000,$04000002,$00000002,$04000000,$04200002,$00000802,<br>&nbsp; &nbsp; &nbsp;$04000800,$00200802,$00200002,$04000800,$04000002,$04200000,$04200800,$00200002,<br>&nbsp; &nbsp; &nbsp;$04200000,$00000800,$00000802,$04200802,$00200800,$00000002,$04000000,$00200800,<br>&nbsp; &nbsp; &nbsp;$04000000,$00200800,$00200000,$04000802,$04000802,$04200002,$04200002,$00000002,<br>&nbsp; &nbsp; &nbsp;$00200002,$04000000,$04000800,$00200000,$04200800,$00000802,$00200802,$04200800,<br>&nbsp; &nbsp; &nbsp;$00000802,$04000002,$04200802,$04200000,$00200800,$00000000,$00000002,$04200802,<br>&nbsp; &nbsp; &nbsp;$00000000,$00200802,$04200000,$00000800,$04000002,$04000800,$00000800,$00200002),<br>&nbsp; &nbsp; ($00000100,$02080100,$02080000,$42000100,$00080000,$00000100,$40000000,$02080000,<br>&nbsp; &nbsp; &nbsp;$40080100,$00080000,$02000100,$40080100,$42000100,$42080000,$00080100,$40000000,<br>&nbsp; &nbsp; &nbsp;$02000000,$40080000,$40080000,$00000000,$40000100,$42080100,$42080100,$02000100,<br>&nbsp; &nbsp; &nbsp;$42080000,$40000100,$00000000,$42000000,$02080100,$02000000,$42000000,$00080100,<br>&nbsp; &nbsp; &nbsp;$00080000,$42000100,$00000100,$02000000,$40000000,$02080000,$42000100,$40080100,<br>&nbsp; &nbsp; &nbsp;$02000100,$40000000,$42080000,$02080100,$40080100,$00000100,$02000000,$42080000,<br>&nbsp; &nbsp; &nbsp;$42080100,$00080100,$42000000,$42080100,$02080000,$00000000,$40080000,$42000000,<br>&nbsp; &nbsp; &nbsp;$00080100,$02000100,$40000100,$00080000,$00000000,$40080000,$02080100,$40000100),<br>&nbsp; &nbsp; ($00000208,$08020200,$00000000,$08020008,$08000200,$00000000,$00020208,$08000200,<br>&nbsp; &nbsp; &nbsp;$00020008,$08000008,$08000008,$00020000,$08020208,$00020008,$08020000,$00000208,<br>&nbsp; &nbsp; &nbsp;$08000000,$00000008,$08020200,$00000200,$00020200,$08020000,$08020008,$00020208,<br>&nbsp; &nbsp; &nbsp;$08000208,$00020200,$00020000,$08000208,$00000008,$08020208,$00000200,$08000000,<br>&nbsp; &nbsp; &nbsp;$08020200,$08000000,$00020008,$00000208,$00020000,$08020200,$08000200,$00000000,<br>&nbsp; &nbsp; &nbsp;$00000200,$00020008,$08020208,$08000200,$08000008,$00000200,$00000000,$08020008,<br>&nbsp; &nbsp; &nbsp;$08000208,$00020000,$08000000,$08020208,$00000008,$00020208,$00020200,$08000008,<br>&nbsp; &nbsp; &nbsp;$08020000,$08000208,$00000208,$08020000,$00020208,$00000008,$08020008,$00020200),<br>&nbsp; &nbsp; ($01010400,$00000000,$00010000,$01010404,$01010004,$00010404,$00000004,$00010000,<br>&nbsp; &nbsp; &nbsp;$00000400,$01010400,$01010404,$00000400,$01000404,$01010004,$01000000,$00000004,<br>&nbsp; &nbsp; &nbsp;$00000404,$01000400,$01000400,$00010400,$00010400,$01010000,$01010000,$01000404,<br>&nbsp; &nbsp; &nbsp;$00010004,$01000004,$01000004,$00010004,$00000000,$00000404,$00010404,$01000000,<br>&nbsp; &nbsp; &nbsp;$00010000,$01010404,$00000004,$01010000,$01010400,$01000000,$01000000,$00000400,<br>&nbsp; &nbsp; &nbsp;$01010004,$00010000,$00010400,$01000004,$00000400,$00000004,$01000404,$00010404,<br>&nbsp; &nbsp; &nbsp;$01010404,$00010004,$01010000,$01000404,$01000004,$00000404,$00010404,$01010400,<br>&nbsp; &nbsp; &nbsp;$00000404,$01000400,$01000400,$00000000,$00010004,$00010400,$00000000,$01010004),<br>&nbsp; &nbsp; ($10001040,$00001000,$00040000,$10041040,$10000000,$10001040,$00000040,$10000000,<br>&nbsp; &nbsp; &nbsp;$00040040,$10040000,$10041040,$00041000,$10041000,$00041040,$00001000,$00000040,<br>&nbsp; &nbsp; &nbsp;$10040000,$10000040,$10001000,$00001040,$00041000,$00040040,$10040040,$10041000,<br>&nbsp; &nbsp; &nbsp;$00001040,$00000000,$00000000,$10040040,$10000040,$10001000,$00041040,$00040000,<br>&nbsp; &nbsp; &nbsp;$00041040,$00040000,$10041000,$00001000,$00000040,$10040040,$00001000,$00041040,<br>&nbsp; &nbsp; &nbsp;$10001000,$00000040,$10000040,$10040000,$10040040,$10000000,$00040000,$10001040,<br>&nbsp; &nbsp; &nbsp;$00000000,$10041040,$00040040,$10000040,$10040000,$10001000,$10001040,$00000000,<br>&nbsp; &nbsp; &nbsp;$10041040,$00041000,$00041000,$00001040,$00001040,$00040040,$10000000,$10041000),<br>&nbsp; &nbsp; ($20000010,$20400000,$00004000,$20404010,$20400000,$00000010,$20404010,$00400000,<br>&nbsp; &nbsp; &nbsp;$20004000,$00404010,$00400000,$20000010,$00400010,$20004000,$20000000,$00004010,<br>&nbsp; &nbsp; &nbsp;$00000000,$00400010,$20004010,$00004000,$00404000,$20004010,$00000010,$20400010,<br>&nbsp; &nbsp; &nbsp;$20400010,$00000000,$00404010,$20404000,$00004010,$00404000,$20404000,$20000000,<br>&nbsp; &nbsp; &nbsp;$20004000,$00000010,$20400010,$00404000,$20404010,$00400000,$00004010,$20000010,<br>&nbsp; &nbsp; &nbsp;$00400000,$20004000,$20000000,$00004010,$20000010,$20404010,$00404000,$20400000,<br>&nbsp; &nbsp; &nbsp;$00404010,$20404000,$00000000,$20400010,$00000010,$00004000,$20400000,$00404010,<br>&nbsp; &nbsp; &nbsp;$00004000,$00400010,$20004010,$00000000,$20404000,$20000000,$00400010,$20004010),<br>&nbsp; &nbsp; ($00802001,$00002081,$00002081,$00000080,$00802080,$00800081,$00800001,$00002001,<br>&nbsp; &nbsp; &nbsp;$00000000,$00802000,$00802000,$00802081,$00000081,$00000000,$00800080,$00800001,<br>&nbsp; &nbsp; &nbsp;$00000001,$00002000,$00800000,$00802001,$00000080,$00800000,$00002001,$00002080,<br>&nbsp; &nbsp; &nbsp;$00800081,$00000001,$00002080,$00800080,$00002000,$00802080,$00802081,$00000081,<br>&nbsp; &nbsp; &nbsp;$00800080,$00800001,$00802000,$00802081,$00000081,$00000000,$00000000,$00802000,<br>&nbsp; &nbsp; &nbsp;$00002080,$00800080,$00800081,$00000001,$00802001,$00002081,$00002081,$00000080,<br>&nbsp; &nbsp; &nbsp;$00802081,$00000081,$00000001,$00002000,$00800001,$00002001,$00802080,$00800081,<br>&nbsp; &nbsp; &nbsp;$00002001,$00002080,$00800000,$00802001,$00000080,$00800000,$00002000,$00802080),<br>&nbsp; &nbsp; ($80108020,$80008000,$00008000,$00108020,$00100000,$00000020,$80100020,$80008020,<br>&nbsp; &nbsp; &nbsp;$80000020,$80108020,$80108000,$80000000,$80008000,$00100000,$00000020,$80100020,<br>&nbsp; &nbsp; &nbsp;$00108000,$00100020,$80008020,$00000000,$80000000,$00008000,$00108020,$80100000,<br>&nbsp; &nbsp; &nbsp;$00100020,$80000020,$00000000,$00108000,$00008020,$80108000,$80100000,$00008020,<br>&nbsp; &nbsp; &nbsp;$00000000,$00108020,$80100020,$00100000,$80008020,$80100000,$80108000,$00008000,<br>&nbsp; &nbsp; &nbsp;$80100000,$80008000,$00000020,$80108020,$00108020,$00000020,$00008000,$80000000,<br>&nbsp; &nbsp; &nbsp;$00008020,$80108000,$00100000,$80000020,$00100020,$80008020,$80000020,$00100020,<br>&nbsp; &nbsp; &nbsp;$00108000,$00000000,$80008000,$00008020,$80000000,$80100020,$80108020,$00108000));<br><br>&nbsp; &nbsp; &nbsp;DES_PC1: array[0..55] of Byte =<br>&nbsp; &nbsp; (56, 48, 40, 32, 24, 16, &nbsp;8, &nbsp;0, 57, 49, 41, 33, 25, 17,<br>&nbsp; &nbsp; &nbsp; 9, &nbsp;1, 58, 50, 42, 34, 26, 18, 10, &nbsp;2, 59, 51, 43, 35,<br>&nbsp; &nbsp; &nbsp;62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21,<br>&nbsp; &nbsp; &nbsp;13, &nbsp;5, 60, 52, 44, 36, 28, 20, 12, &nbsp;4, 27, 19, 11, &nbsp;3);<br><br>&nbsp; &nbsp; &nbsp;DES_PC2: array[0..47] of Byte =<br>&nbsp; &nbsp; (13, 16, 10, 23, &nbsp;0, &nbsp;4, &nbsp;2, 27, 14, &nbsp;5, 20, &nbsp;9,<br>&nbsp; &nbsp; &nbsp;22, 18, 11, &nbsp;3, 25, &nbsp;7, 15, &nbsp;6, 26, 19, 12, &nbsp;1,<br>&nbsp; &nbsp; &nbsp;40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,<br>&nbsp; &nbsp; &nbsp;43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31);<br><br><br>{*******************************************************************************<br>* Table &nbsp; &nbsp; : BinToAsc &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : The encode table used by Base64 encoding &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>&nbsp; &nbsp; &nbsp;BinToAsc : Array [0..63] of Char =<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ('+', '-','0','1','2','3','4','5','6','7',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'t','u','v','w','x','y','z');<br><br>{$ifdef COMPONENT}<br>{*******************************************************************************<br>* Procedure : Register &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Declares the component to the Delhpi IDE (in component mode only)*<br>********************************************************************************<br>* Paramters : 'Crypto' : the name of the Tab under which the component should &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; appear &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'TDES' The name of the component in the tab. Must match the &nbsp; &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; declared type name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure Register;<br>begin<br>&nbsp; &nbsp; &nbsp;RegisterComponents('Crypto', [TDES]);<br>end; {Register}<br>{$endif}<br><br>{*******************************************************************************<br>* Procedure : EncodeString &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Encodes the binary string into a base64 representation to avoid &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; problems with nulls in the encoded string &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : Input - the string to be encoded &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : the encoded string &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>function TDES.EncodeString(InputString: string): string;<br>var<br>&nbsp; &nbsp; &nbsp;Counter: &nbsp; &nbsp; &nbsp;integer;<br>&nbsp; &nbsp; &nbsp;ReturnString: string;<br>&nbsp; &nbsp; &nbsp;b: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Byte;<br>&nbsp; &nbsp; &nbsp;i: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;integer;<br>&nbsp; &nbsp; &nbsp;last: &nbsp; &nbsp; &nbsp; &nbsp; byte;<br>&nbsp; &nbsp; &nbsp;Flush: &nbsp; &nbsp; &nbsp; &nbsp;Boolean;<br>&nbsp; &nbsp; &nbsp;LengthInput: &nbsp;integer;<br>begin<br>&nbsp; &nbsp; &nbsp;Counter := 0;<br>&nbsp; &nbsp; &nbsp;ReturnString := '';<br>&nbsp; &nbsp; &nbsp;Flush := False;<br>&nbsp; &nbsp; &nbsp;last := 0;<br><br>&nbsp; &nbsp; &nbsp;// deal with flushing the partial byte at the end of the string<br>&nbsp; &nbsp; &nbsp;if (Length(InputString) mod 3) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputString := InputString + Chr(0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // flush controls the last byte mod 3<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Flush := True;<br>&nbsp; &nbsp; &nbsp;end; {if}<br><br>&nbsp; &nbsp; &nbsp;LengthInput := Length(InputString);<br>&nbsp; &nbsp; &nbsp;i := 1;<br>&nbsp; &nbsp; &nbsp;while (i &lt;= LengthInput) do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i &lt;= LengthInput then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b := Ord(InputString);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;b := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; {if}<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case Counter of<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReturnString := ReturnString + BinToAsc[(b shr 2)];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last := b;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReturnString := ReturnString + BinToAsc[((last and $3) shl 4) or ((b and $F0) shr 4) ];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last := b;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReturnString := ReturnString + BinToAsc[((last and $F) shl 2) or ((b and $C0) shr 6)];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not (Flush and (i = LengthInput)) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ReturnString := ReturnString + BinToAsc[(b and $3F)];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; {case}<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(Counter);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Counter = 3 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Counter := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(i);<br>&nbsp; &nbsp; &nbsp;end; {while}<br><br>&nbsp; &nbsp; &nbsp;Result := ReturnString;<br>end; {EncodeString}<br><br>{*******************************************************************************<br>* Procedure : DecodeString &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Decodes the binary string into a base64 representation to avoid &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; problems with nulls in the encoded string &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : Input - the string to be decoded &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : the decoded string &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>function TDES.DecodeString(InputString: string): string;<br>&nbsp; &nbsp; &nbsp;function &nbsp;DecodeBase64(b: byte): byte;<br>&nbsp; &nbsp; &nbsp;{*******************************************************************************<br>&nbsp; &nbsp; &nbsp;* Procedure : DecodeBase64 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>&nbsp; &nbsp; &nbsp;********************************************************************************<br>&nbsp; &nbsp; &nbsp;* Purpose &nbsp; : Decodes a byte from the Base64 string &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>&nbsp; &nbsp; &nbsp;********************************************************************************<br>&nbsp; &nbsp; &nbsp;* Paramters : b - the byte to be decoded &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>&nbsp; &nbsp; &nbsp;********************************************************************************<br>&nbsp; &nbsp; &nbsp;* Returns &nbsp; : the decoded byte &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>&nbsp; &nbsp; &nbsp;*******************************************************************************}<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (b &gt;= Ord('0')) and (b &lt;= Ord('9')) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := b - Ord('0') + 2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (b &gt;= Ord('A')) and (b &lt;= Ord('Z')) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := b - Ord('A') + 12;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (b &gt;= Ord('a')) and (b &lt;= Ord('z')) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := b - Ord('a') + 38;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if b = Ord('+') then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if b = Ord('-') then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Default result if the char is not recognised<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise EConvertError.Create(LIT_BASE64CNV);<br>&nbsp; &nbsp; &nbsp;end; {DecodeBase64}<br>var<br>&nbsp; &nbsp; &nbsp;Counter: &nbsp; &nbsp; &nbsp;integer;<br>&nbsp; &nbsp; &nbsp;ReturnString: string;<br>&nbsp; &nbsp; &nbsp;c: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Char;<br>&nbsp; &nbsp; &nbsp;last: &nbsp; &nbsp; &nbsp; &nbsp; byte;<br>&nbsp; &nbsp; &nbsp;this: &nbsp; &nbsp; &nbsp; &nbsp; byte;<br>&nbsp; &nbsp; &nbsp;i: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;integer;<br>begin<br>&nbsp; &nbsp; &nbsp;Counter := 0;<br>&nbsp; &nbsp; &nbsp;ReturnString := '';<br>&nbsp; &nbsp; &nbsp;last := 0;<br><br>&nbsp; &nbsp; &nbsp;for i := 1 to Length(InputString) do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c := InputString;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case Counter of<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last := DecodeBase64(Ord(c)) shl 2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this := DecodeBase64(Ord(c));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReturnString := ReturnString + Chr((last or (this shr 4)) and $ff);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last := this shl 4;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this := DecodeBase64(Ord(c));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReturnString := ReturnString + Chr((last or (this shr 2)) and $ff);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last := this shl 6;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this := DecodeBase64(Ord(c));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ReturnString := ReturnString + Chr((last or this) and $ff);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; last := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; {case}<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(Counter);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Counter = 4 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Counter := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; {if}<br>&nbsp; &nbsp; &nbsp;end; {for}<br><br>&nbsp; &nbsp; &nbsp;Result := ReturnString;<br>end; {DecodeString}<br><br>{*******************************************************************************<br>* Procedure : GetVersion &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Returns the internal version number of the component &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Paramters : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : String - the version number expressed as a string &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>*******************************************************************************}<br>function TDES.GetVersion;<br>begin<br>&nbsp; &nbsp; &nbsp;// return the version string<br>&nbsp; &nbsp; &nbsp;Result := LIT_COMPNAME + ' ' + RELVER;<br><br>&nbsp; &nbsp; &nbsp;{$ifdef REGISTERED}<br>&nbsp; &nbsp; &nbsp;Result := Result + ' Registered';<br>&nbsp; &nbsp; &nbsp;{$endif}<br>&nbsp; &nbsp; &nbsp;{$ifdef NOTREGISTERED}<br>&nbsp; &nbsp; &nbsp;Result := Result + ' Unregistered';<br>&nbsp; &nbsp; &nbsp;{$endif}<br>&nbsp; &nbsp; &nbsp;{$ifdef FREEWARE}<br>&nbsp; &nbsp; &nbsp;Result := Result + ' Freeware';<br>&nbsp; &nbsp; &nbsp;{$endif}<br>end; {GetVersion}<br><br>{$ifndef COMPONENT}<br>{*******************************************************************************<br>* Procedure : SetCipherMode &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Sets the ciphermode when defined as an object &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : 'Value' : The new cipher mode to be set &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Returns &nbsp; : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.SetCipherMode(const Value: TCipherMode);<br>begin<br>&nbsp; &nbsp; &nbsp;FCipherMode := Value;<br>end; {SetCipherMode}<br>{$endif}<br><br>{*******************************************************************************<br>* Procedure : InitialiseString &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Loads the passphrase into the context block &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : 'Key' - the string which holds the key &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.InitialiseString(const Key: string);<br>var<br>&nbsp; &nbsp; &nbsp;KeyArray: TBlock;<br>&nbsp; &nbsp; &nbsp;i: integer;<br>begin<br>&nbsp; &nbsp; &nbsp;// clear the context<br>&nbsp; &nbsp; &nbsp;FillChar(ctx.ct, Sizeof(ctx.ct), #0);<br>&nbsp; &nbsp; &nbsp;FillChar(ctx.ByteBuffer, Sizeof(ctx.ByteBuffer), #0);<br>&nbsp; &nbsp; &nbsp;FillChar(KeyArray, Sizeof(KeyArray), #0);<br><br>&nbsp; &nbsp; &nbsp;{**************************************************************************<br>&nbsp; &nbsp; &nbsp;************ This section of code implements the 64 bit limit *************<br>&nbsp; &nbsp; &nbsp;************ imposed by the Wassennar agreement. The key is &nbsp; *************<br>&nbsp; &nbsp; &nbsp;************ limited to 64 bits. Should you be in a country &nbsp; *************<br>&nbsp; &nbsp; &nbsp;************ where the Wassennar agreement is not in force, &nbsp; *************<br>&nbsp; &nbsp; &nbsp;************ undefine the WASSENAAR_LIMITED variable. &nbsp; &nbsp; &nbsp; &nbsp; *************<br>&nbsp; &nbsp; &nbsp;**************************************************************************}<br><br>&nbsp; &nbsp; &nbsp;{$ifdef WASSENAAR_LIMITED}<br>&nbsp; &nbsp; &nbsp;// turn the key string into a key array<br>&nbsp; &nbsp; &nbsp;for i := 1 to Length(Key) do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KeyArray[(i-1) mod 8] := Ord(Key);<br>&nbsp; &nbsp; &nbsp;end; {for}<br>&nbsp; &nbsp; &nbsp;{$else}<br>&nbsp; &nbsp; &nbsp;// turn the key string into a key array<br>&nbsp; &nbsp; &nbsp;for i := 1 to Length(Key) do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KeyArray[(i-1) mod 8] := Ord(Key);<br>&nbsp; &nbsp; &nbsp;end; {for}<br>&nbsp; &nbsp; &nbsp;{$endif}<br>&nbsp; &nbsp; &nbsp;// and perform the initialisation with the concatenated string<br>&nbsp; &nbsp; &nbsp;DES_Core_Key_Setup(KeyArray);<br><br>&nbsp; &nbsp; &nbsp;// mark the context as initialised<br>&nbsp; &nbsp; &nbsp;ctx.KeyInit := True;<br>end; {InitialiseString}<br><br>{*******************************************************************************<br>* Procedure : InitialiseByte &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Loads the passphrase into the context block &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : 'Key' - array of bytes which holds the key &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'KeyLength' - the number of bytes in the array which are to be &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; read to load the key - need not be the same as the length of the *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Returns &nbsp; : 'OK' if the operation was a success, otherwise an error code &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.InitialiseByte(const Key: array of Byte; KeyLength: integer);<br>var<br>&nbsp; &nbsp; &nbsp;KeyArray: TBlock;<br>&nbsp; &nbsp; &nbsp;i: integer;<br>begin<br>&nbsp; &nbsp; &nbsp;// clear the context<br>&nbsp; &nbsp; &nbsp;FillChar(ctx.ct, Sizeof(ctx.ct), #0);<br>&nbsp; &nbsp; &nbsp;FillChar(ctx.ByteBuffer, Sizeof(ctx.ByteBuffer), #0);<br>&nbsp; &nbsp; &nbsp;FillChar(KeyArray, Sizeof(KeyArray), #0);<br><br>&nbsp; &nbsp; &nbsp;{**************************************************************************<br>&nbsp; &nbsp; &nbsp;************ This section of code implements the 64 bit limit *************<br>&nbsp; &nbsp; &nbsp;************ imposed by the Wassennar agreement. The key is &nbsp; *************<br>&nbsp; &nbsp; &nbsp;************ limited to 64 bits. Should you be in a country &nbsp; *************<br>&nbsp; &nbsp; &nbsp;************ where the Wassennar agreement is not in force, &nbsp; *************<br>&nbsp; &nbsp; &nbsp;************ undefine the WASSENAAR_LIMITED variable. &nbsp; &nbsp; &nbsp; &nbsp; *************<br>&nbsp; &nbsp; &nbsp;**************************************************************************}<br><br>&nbsp; &nbsp; &nbsp;{$ifdef WASSENAAR_LIMITED}<br>&nbsp; &nbsp; &nbsp;// buffer the passed key into the key array to make sure that<br>&nbsp; &nbsp; &nbsp;// it is padded with something defined (just in case)<br>&nbsp; &nbsp; &nbsp;for i := 0 to KeyLength-1 do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KeyArray[i mod 8] := Key;<br>&nbsp; &nbsp; &nbsp;end; {for}<br>&nbsp; &nbsp; &nbsp;{$else}<br>&nbsp; &nbsp; &nbsp;// buffer the passed key into the key array to make sure that<br>&nbsp; &nbsp; &nbsp;// it is padded with something defined (just in case)<br>&nbsp; &nbsp; &nbsp;for i := 0 to KeyLength-1 do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; KeyArray[i mod 8] := Key;<br>&nbsp; &nbsp; &nbsp;end; {for}<br>&nbsp; &nbsp; &nbsp;{$endif}<br><br>&nbsp; &nbsp; &nbsp;// and perform the initialisation with the concatenated string<br>&nbsp; &nbsp; &nbsp;DES_Core_Key_Setup(KeyArray);<br><br>&nbsp; &nbsp; &nbsp;// mark the context as initialised<br>&nbsp; &nbsp; &nbsp;ctx.KeyInit := True;<br>end; {InitialiseByte}<br><br>{*******************************************************************************<br>* Procedure : LoadIVString &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Loads the Initialisation Vector &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Note: this is only necessary for modes other than ECB &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : 'IVString' - the string which holds the IV to be set &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : None - (Null IVs are also valid) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.LoadIVString(Const IVString: string);<br>var<br>&nbsp; &nbsp; &nbsp;i: integer;<br>begin<br>&nbsp; &nbsp; &nbsp;// clear the IV in the context<br>&nbsp; &nbsp; &nbsp;FillChar(ctx.IV, BLOCKSIZE, #0);<br><br>&nbsp; &nbsp; &nbsp;// wrap the IV string into the 16 bytes of the IV block using xor<br>&nbsp; &nbsp; &nbsp;for i := 1 to Length(IVString) do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.IV[(i-1) and (BLOCKSIZE - 1)] := ctx.IV[(i-1) and (BLOCKSIZE - 1)] xor Ord(IVString);<br>&nbsp; &nbsp; &nbsp;end; {for i}<br><br>&nbsp; &nbsp; &nbsp;// mark the IV as being initialised<br>&nbsp; &nbsp; &nbsp;ctx.IVInit := True;<br>end; {LoadIVString}<br><br>{*******************************************************************************<br>* Procedure : LoadIVByte &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Loads the Initialisation Vector &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Note: this is only necessary for modes other than ECB &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : 'IVByte' - the array of bytes which holds the IV to be set &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : None - (Null IVs are also valid) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.LoadIVByte(const IVByte: array of Byte; IVLength: integer);<br>var<br>&nbsp; &nbsp; &nbsp;i: integer;<br>begin<br>&nbsp; &nbsp; &nbsp;// clear the IV in the context<br>&nbsp; &nbsp; &nbsp;FillChar(ctx.ByteBuffer, BLOCKSIZE, #0);<br><br>&nbsp; &nbsp; &nbsp;// wrap the IV string into the 8 bytes of the IV block using xor<br>&nbsp; &nbsp; &nbsp;for i := 1 to IVLength do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.ByteBuffer[(i-1) and (BLOCKSIZE - 1)] := ctx.ByteBuffer[(i-1) and (BLOCKSIZE - 1)] xor IVByte;<br>&nbsp; &nbsp; &nbsp;end; {for i}<br><br>&nbsp; &nbsp; &nbsp;// mark the IV as being initialised<br>&nbsp; &nbsp; &nbsp;ctx.IVInit := True;<br>end; {LoadIVByte}<br><br>{*******************************************************************************<br>* Procedure : EncryptBlock &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Encrypts the contents of the block usint the key (and possibly &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the IV) previously set. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : 'Input' &nbsp;the block to be encrypted &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : 'Output' the encrypted block &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : OK if successful, otherwise error code &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.EncBlock(const Input: TBlock; var Output: TBlock);<br>begin<br>&nbsp; &nbsp; &nbsp;// check that we have a keys and IV<br>&nbsp; &nbsp; &nbsp;CheckKeys;<br><br>&nbsp; &nbsp; &nbsp;// copy the input to the context blockbuffer<br>&nbsp; &nbsp; &nbsp;ctx.ByteBuffer := Input;<br><br>&nbsp; &nbsp; &nbsp;// perform the encryption on the context<br>&nbsp; &nbsp; &nbsp;EncryptBlockMode;<br><br>&nbsp; &nbsp; &nbsp;// copy the context back to the blockbuffer<br>&nbsp; &nbsp; &nbsp;Output := ctx.ByteBuffer;<br>end; {EncryptBlock}<br><br>{*******************************************************************************<br>* Procedure : DecryptBlock &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Decrypts the contents of the block usint the key (and possibly &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the IV) previously set. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : 'Input' &nbsp;the encrypted block to be decrypted &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : 'Output' the decrypted block &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : OK if successful, otherwise error code &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.DecBlock(const Input: TBlock; var Output: TBlock);<br>begin<br>&nbsp; &nbsp; &nbsp;// check that we have a keys and IV<br>&nbsp; &nbsp; &nbsp;CheckKeys;<br><br>&nbsp; &nbsp; &nbsp;// copy the input to the context blockbuffer<br>&nbsp; &nbsp; &nbsp;ctx.ByteBuffer := Input;<br><br>&nbsp; &nbsp; &nbsp;// perform the decryption<br>&nbsp; &nbsp; &nbsp;DecryptBlockMode;<br><br>&nbsp; &nbsp; &nbsp;// copy the context back to the blockbuffer<br>&nbsp; &nbsp; &nbsp;Output := ctx.ByteBuffer;<br>end; {DecryptBlock}<br><br>{*******************************************************************************<br>* Procedure : EncryptBuffer &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Encrypts the contents of the buffer using the key (and possibly &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the IV) previously set. Does not take care of any padding. &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Paramters : 'Len' the number of bytes in the buffer &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Returns &nbsp; : OK if successful, otherwise error code &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.EncryptBuffer(const Len: integer);<br>var<br>&nbsp; &nbsp; &nbsp;Index: integer;<br>begin<br>&nbsp; &nbsp; &nbsp;// check that we have a keys and IV<br>&nbsp; &nbsp; &nbsp;CheckKeys;<br><br>&nbsp; &nbsp; &nbsp;// index is the pointer to the current position in the buffer<br>&nbsp; &nbsp; &nbsp;Index := 0;<br><br>&nbsp; &nbsp; &nbsp;// PtrBuffer points to the address of the buffer<br>&nbsp; &nbsp; &nbsp;PtrBuffer := @FBuffer;<br><br>&nbsp; &nbsp; &nbsp;// for every block in the buffer<br>&nbsp; &nbsp; &nbsp;repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // move one block from the buffer contents into the context<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Move(FBuffer[Index], ctx.ByteBuffer, BLOCKSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // encrypt the context<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EncryptBlockMode;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // move the block back<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Move(ctx.ByteBuffer, PtrBuffer^[Index], BLOCKSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // increment the pointer<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(Index,BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp;until Index = Len;<br>end; {EncryptBuffer}<br><br>{*******************************************************************************<br>* Procedure : DecryptBuffer &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Decrypts the contents of the buffer usint the key (and possibly &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the IV) previously set. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : 'Len' the number of bytes in the buffer &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Returns &nbsp; : OK if successful, otherwise error code &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.DecryptBuffer(const Len: integer);<br>var<br>&nbsp; &nbsp; &nbsp;Index: integer;<br>begin<br>&nbsp; &nbsp; &nbsp;// check that we have a keys and IV<br>&nbsp; &nbsp; &nbsp;CheckKeys;<br><br>&nbsp; &nbsp; &nbsp;// index is the pointer to the current position in the buffer<br>&nbsp; &nbsp; &nbsp;Index := 0;<br><br>&nbsp; &nbsp; &nbsp;// PtrBuffer points to the address of the buffer<br>&nbsp; &nbsp; &nbsp;PtrBuffer := @FBuffer;<br><br>&nbsp; &nbsp; &nbsp;// for every block in the buffer<br>&nbsp; &nbsp; &nbsp;//XuYe 2001.6.12<br>&nbsp; &nbsp; &nbsp;{repeat}<br>&nbsp; &nbsp; &nbsp;while Index &lt; Len do<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // move one block from the buffer contents into the context<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Move(FBuffer[Index], ctx.ByteBuffer, BLOCKSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // decrypt the context<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DecryptBlockMode;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // move the block back<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Move(ctx.ByteBuffer, PtrBuffer^[Index], BLOCKSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // increment the pointer<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(Index,BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;{until Index = Len;}<br>&nbsp;end; {DecryptBuffer}<br><br>{*******************************************************************************<br>* Procedure : EncryptFile &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Encrypts InputFile to OutputFile using the Key (and possibly &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the IV) previously set. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : 'InputFileName' the plaintext file &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'OutputFileName' the ciphertext file &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : OK if successful, otherwise error code &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.EncFile(const InputFileName: string; OutputFileName: string);<br>var<br>&nbsp; &nbsp; &nbsp;InputFile, OutputFile: File;<br>&nbsp; &nbsp; &nbsp;NumWrite, NumRead: integer;<br>&nbsp; &nbsp; &nbsp;Pad: integer;<br>&nbsp; &nbsp; &nbsp;Index: integer;<br>begin<br>&nbsp; &nbsp; &nbsp;// check that we have a keys and IV<br>&nbsp; &nbsp; &nbsp;CheckKeys;<br><br>&nbsp; &nbsp; &nbsp;// open the input file<br>&nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AssignFile(InputFile, InputFileName);<br>&nbsp; &nbsp; &nbsp;except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // we could not open the input file for some reason<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // so exit gracefully with an error code<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise EFileError.Create(LIT_INFILE_NOT_FOUND);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;// reset the input file<br>&nbsp; &nbsp; &nbsp;FileMode := 0;<br>&nbsp; &nbsp; &nbsp;Reset(InputFile, 1);<br><br>&nbsp; &nbsp; &nbsp;// open the output file<br>&nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AssignFile(OutputFile, OutputFileName);<br>&nbsp; &nbsp; &nbsp;except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // we could not open the output file for some reason<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // so exit gracefully with an error code<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise EFileError.Create(LIT_OUTFILE_OPEN_ERROR);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;// reset the output file for writing<br>&nbsp; &nbsp; &nbsp;Rewrite(OutputFile, 1);<br><br>&nbsp; &nbsp; &nbsp;// this is the main loop of EncryptFile. We read (for performance reasons) a block<br>&nbsp; &nbsp; &nbsp;// at a time and encrypt the block. This minimises the accesses to the disk<br>&nbsp; &nbsp; &nbsp;repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Read an input block<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BlockRead(InputFile,FBuffer,BUFFERSIZE, NumRead);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Pad the input to a multiple of 64bits(8BYTES) with Nulls at the end of the file<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if EOF(InputFile) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// pad the last block<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// if we have a zero padding, expand this to a full 8 byte block<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Pad := BLOCKSIZE - (NumRead mod BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Index := Pad;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// add the pad bytes to the buffer<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while Index &lt;&gt; 0 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FBuffer[NumRead] := Pad;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(NumRead);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dec(Index);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end; {while Index}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; {if EOF}<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // encrypt the buffer<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EncryptBuffer(NumRead);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // write the block to the output file<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BlockWrite(OutputFile, FBuffer, NumRead, NumWrite);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if NumRead &lt;&gt; NumWrite, it is probable that the disk is full<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if NumRead &lt;&gt; NumWrite then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// there was an error writing the output file<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// exit with the error code<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;raise EFileError.Create(LIT_OUTFILE_WRITE_ERROR);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; {if NumRead &lt;&gt; NumWrite}<br>&nbsp; &nbsp; &nbsp;until EOF(InputFile) or (NumWrite &lt;&gt; NumRead); {repeat}<br><br>&nbsp; &nbsp; &nbsp;// close the files<br>&nbsp; &nbsp; &nbsp;CloseFile(InputFile);<br>&nbsp; &nbsp; &nbsp;CloseFile(OutputFile);<br>end; {EncryptFile}<br>
 
{*******************************************************************************<br>* Procedure : DecryptFile &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Decrypts InputFile to OutputFile using the Key (and possibly &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the IV) previously set. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : 'InputFileName' the ciphertext file &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'OutputFileName' the plaintext file &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Returns &nbsp; : OK if successful, otherwise error code &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.DecFile(const InputFileName: string; OutputFileName: string);<br>var<br>&nbsp; &nbsp; &nbsp;InputFile, OutputFile: File;<br>&nbsp; &nbsp; &nbsp;NumWrite, NumRead: integer;<br>begin<br>&nbsp; &nbsp; &nbsp;// check that we have a keys and IV<br>&nbsp; &nbsp; &nbsp;CheckKeys;<br><br>&nbsp; &nbsp; &nbsp;// open the input file<br>&nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AssignFile(InputFile, InputFileName);<br>&nbsp; &nbsp; &nbsp;except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // we could not open the input file for some reason<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // so exit gracefully with an error code<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise EFileError.Create(LIT_INFILE_NOT_FOUND);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;// reset the input file<br>&nbsp; &nbsp; &nbsp;FileMode := 0;<br>&nbsp; &nbsp; &nbsp;Reset(InputFile, 1);<br><br>&nbsp; &nbsp; &nbsp;// open the output file<br>&nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AssignFile(OutputFile, OutputFileName);<br>&nbsp; &nbsp; &nbsp;except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // we could not open the output file for some reason<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // so exit gracefully with an error code<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise EFileError.Create(LIT_OUTFILE_OPEN_ERROR);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;// reset the output file<br>&nbsp; &nbsp; &nbsp;Rewrite(OutputFile, 1);<br><br>&nbsp; &nbsp; &nbsp;// this is the main loop of DecryptFile. We read (for performance reasons) a block<br>&nbsp; &nbsp; &nbsp;// at a time and encrypt the block. This minimises the accesses to the disk<br>&nbsp; &nbsp; &nbsp;repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Read an input block<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BlockRead(InputFile,FBuffer,BUFFERSIZE, NumRead);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Call DecryptBuffer to handle the actual decryption<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DecryptBuffer(NumRead);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Put in OutputFile, trimming out the final padding as necessary<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if EOF(InputFile) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// deal with the case that the file is a multiple of BUFFERLENGTH long<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NumRead := NumRead - FBuffer[NumRead-1];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // write the block to the output file<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BlockWrite(OutputFile,FBuffer, NumRead, NumWrite);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if NumRead &lt;&gt; NumWrite, it is probable that the disk is full<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if NumRead &lt;&gt; NumWrite then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// there was an error writing the output file<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// exit with the error code<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;raise EFileError.Create(LIT_OUTFILE_WRITE_ERROR);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; {if NumRead &lt;&gt; NumWrite}<br>&nbsp; &nbsp; &nbsp;until EOF(InputFile) or (NumWrite &lt;&gt; NumRead);<br><br>&nbsp; &nbsp; &nbsp;// close the files<br>&nbsp; &nbsp; &nbsp;CloseFile(InputFile);<br>&nbsp; &nbsp; &nbsp;CloseFile(OutputFile);<br>end; {DecryptFile}<br><br>{*******************************************************************************<br>* Procedure : EncryptStream &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Encrypts the contents of the stream using the key (and possibly &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the IV) previously set. Takes care of padding the stream. &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Paramters : 'Input' &nbsp;the stream to be encrypted &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : 'Output' the encrypted stream &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Returns &nbsp; : OK if successful, otherwise error code &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.EncStream(const Input: TStream; const Output: TStream);<br>var<br>&nbsp; &nbsp; &nbsp;i: integer;<br>&nbsp; &nbsp; &nbsp;j: integer;<br>&nbsp; &nbsp; &nbsp;k: integer;<br>&nbsp; &nbsp; &nbsp;RemainingData: integer;<br>begin<br>&nbsp; &nbsp; &nbsp;// Input.Size contains the length of the stream in bytes and in *not*<br>&nbsp; &nbsp; &nbsp;// updated by reading the stream (i.e. it is not a pointer to the position<br>&nbsp; &nbsp; &nbsp;// in the stream.<br>&nbsp; &nbsp; &nbsp;if FEncDecNoPosFlage then<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RemainingData := Input.Size-Input.Position;<br>&nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RemainingData := Input.Size;<br><br>&nbsp; &nbsp; &nbsp;// reset the stream positions<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Input.Seek(0, soFromBeginning);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Output.Seek(0, soFromBeginning);<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;// check for zero length strings<br>&nbsp; &nbsp; &nbsp;if RemainingData = 0 then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // don't need to do anything<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;// deal with the whole BUFFERSIZE sections first<br>&nbsp; &nbsp; &nbsp;// get the number of BUFFERSIZE sections to process, and process them<br>&nbsp; &nbsp; &nbsp;i := RemainingData div BUFFERSIZE;<br>&nbsp; &nbsp; &nbsp;while i &gt; 0 do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Input.Read(FBuffer, BUFFERSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if FCrcCaclFlage then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FCrc32Value:=crc32(FCrc32Value,@FBuffer[0],BUFFERSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dec(RemainingData, BUFFERSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EncryptBuffer(BUFFERSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Output.Write(FBuffer, BUFFERSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dec(i);<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;// now deal with the final (incomplete) block of length BUFFERSIZE<br>&nbsp; &nbsp; &nbsp;// this must be padded to ensure a sucessful decryption<br><br>&nbsp; &nbsp; &nbsp;// read in the partial block<br>&nbsp; &nbsp; &nbsp;Input.Read(FBuffer, RemainingData);<br>&nbsp; &nbsp; &nbsp;if FCrcCaclFlage then<br>&nbsp; &nbsp; &nbsp; &nbsp;FCrc32Value:=crc32(FCrc32Value,@FBuffer[0],RemainingData);<br>&nbsp; &nbsp; &nbsp;// get the amount of padding and add it<br>&nbsp; &nbsp; &nbsp;i := BLOCKSIZE - (RemainingData mod BLOCKSIZE);<br><br>&nbsp; &nbsp; &nbsp;// and the position to write it to in the buffer<br>&nbsp; &nbsp; &nbsp;j := RemainingData;<br><br>&nbsp; &nbsp; &nbsp;// pad to a full block at the end of the input<br>&nbsp; &nbsp; &nbsp;k := i;<br>&nbsp; &nbsp; &nbsp;while k &gt; 0 do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FBuffer[j] := i;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dec(k);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(j);<br>&nbsp; &nbsp; &nbsp;end; {while k}<br><br>&nbsp; &nbsp; &nbsp;// encrypt the partial block<br>&nbsp; &nbsp; &nbsp;EncryptBuffer(j);<br><br>&nbsp; &nbsp; &nbsp;// write to the output stream<br>&nbsp; &nbsp; &nbsp;Output.Write(FBuffer, j);<br>end; {EncryptStream}<br><br>{*******************************************************************************<br>* Procedure : DecryptStream &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Decrypts the contents of the stream using the key (and possibly &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the IV) previously set. Unpads the stream. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Paramters : 'Input' &nbsp;the stream to be decrypted &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : 'Output' the decrypted stream &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Returns &nbsp; : OK if successful, otherwise error code &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.DecStream(const Input: TStream; const Output: TStream);<br>var<br>&nbsp; &nbsp; &nbsp;i: integer;<br>&nbsp; &nbsp; &nbsp;RemainingData: integer;<br>begin<br>&nbsp; &nbsp; &nbsp;// Input.Size contains the length of the stream in bytes and in *not*<br>&nbsp; &nbsp; &nbsp;// updated by reading the stream (i.e. it is not a pointer to the position<br>&nbsp; &nbsp; &nbsp;// in the stream.<br>&nbsp; &nbsp; &nbsp;if FEncDecNoPosFlage then<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RemainingData := Input.Size-Input.Position;<br>&nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RemainingData := Input.Size;<br><br>&nbsp; &nbsp; &nbsp;// reset the stream positions<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Input.Seek(0, soFromBeginning);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Output.Seek(0, soFromBeginning);<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;// check for zero length strings<br>&nbsp; &nbsp; &nbsp;if RemainingData = 0 then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // don't need to do anything<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;// deal with the whole BUFFERSIZE sections first<br>&nbsp; &nbsp; &nbsp;// get the number of BUFFERSIZE sections to process, and process them<br>&nbsp; &nbsp; &nbsp;i := RemainingData div BUFFERSIZE;<br>&nbsp; &nbsp; &nbsp;while i &gt; 0 do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Input.Read(FBuffer, BUFFERSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dec(RemainingData, BUFFERSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DecryptBuffer(BUFFERSIZE);<br><br>&nbsp; &nbsp; //XuYe 2001.06.12<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not (RemainingData=0) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Output.Write(FBuffer, BUFFERSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if FCrcCaclFlage then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FCrc32Value:=crc32(FCrc32Value,@FBuffer[0],BUFFERSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Output.Write(FBuffer,BUFFERSIZE-FBuffer[RemainingData-1]);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if FCrcCaclFlage then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FCrc32Value:=crc32(FCrc32Value,@FBuffer[0],BUFFERSIZE-FBuffer[RemainingData-1]);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dec(i);<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;// now deal with the final (incomplete) block of length BUFFERSIZE<br>&nbsp; &nbsp; &nbsp;// this must be padded to ensure a sucessful decryption<br><br>&nbsp; &nbsp; &nbsp;// read in the partial block<br>&nbsp; &nbsp; &nbsp;//XuYe 2001.06.12<br>&nbsp; &nbsp; &nbsp;if not (RemainingData=0) then<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Input.Read(FBuffer, RemainingData);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// decrypt the partial block<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DecryptBuffer(RemainingData);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// get the length of the padding<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;i := FBuffer[RemainingData-1];<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// write the data excluding the i bytes of padding<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Output.Write(FBuffer, RemainingData-i);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if FCrcCaclFlage then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FCrc32Value:=crc32(FCrc32Value,@FBuffer[0],RemainingData-i);<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>end; {DecryptStream}<br><br>{*******************************************************************************<br>* Procedure : EncryptString &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Encrypts the contents of the string usint the key (and possibly &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the IV) previously set. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : 'Input' &nbsp;the string to be encrypted &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : 'Output' the encrypted string &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Returns &nbsp; : OK if successful, otherwise error code &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.EncString(const Input: string; var Output: string);<br>var<br>&nbsp; &nbsp; &nbsp;i: longint;<br>&nbsp; &nbsp; &nbsp;j: longint;<br>&nbsp; &nbsp; &nbsp;s: string;<br>begin<br>&nbsp; &nbsp; &nbsp;// check that we have a keys and IV<br>&nbsp; &nbsp; &nbsp;CheckKeys;<br><br>&nbsp; &nbsp; &nbsp;// initialise the output string<br>&nbsp; &nbsp; &nbsp;Output := '';<br><br>&nbsp; &nbsp; &nbsp;// check for zero length strings<br>&nbsp; &nbsp; &nbsp;if Length(Input) = 0 then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // don't need to do anything<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;// load the input into the buffer<br>&nbsp; &nbsp; &nbsp;s := Input;<br><br>&nbsp; &nbsp; &nbsp;// Pad the input string to a multiple of BLOCKSIZE bytes.<br>&nbsp; &nbsp; &nbsp;j := Length(s) + 1;<br>&nbsp; &nbsp; &nbsp;i := BLOCKSIZE - (Length(s) mod BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp;SetLength(s, Length(s)+i);<br>&nbsp; &nbsp; &nbsp;SetLength(Output, Length(s));<br><br>&nbsp; &nbsp; &nbsp;// add the pad bytes to the end of the string<br>&nbsp; &nbsp; &nbsp;while j &lt;= Length(s) do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s[j] := chr(i);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inc(j);<br>&nbsp; &nbsp; &nbsp;end; {while j}<br><br>&nbsp; &nbsp; &nbsp;// initialise the counters<br>&nbsp; &nbsp; &nbsp;j := 1;<br>&nbsp; &nbsp; &nbsp;i := 1;<br><br>&nbsp; &nbsp; &nbsp;// and step through the string<br>&nbsp; &nbsp; &nbsp;while i &lt; length(s) do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // copy the next bytes into the context block buffer<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Move(s, ctx.ByteBuffer, BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(i, BLOCKSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // perform the encryption of the context<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EncryptBlockMode;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // copy the block into the output string<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Move(ctx.ByteBuffer, Output[j], BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(j, BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp;end; {while j}<br><br>&nbsp; &nbsp; &nbsp;// encode the string if required<br>&nbsp; &nbsp; &nbsp;if FStringMode = smEncode then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Output := EncodeString(Output);<br>&nbsp; &nbsp; &nbsp;end;<br>end; {EncryptString}<br><br>{*******************************************************************************<br>* Procedure : DecryptString &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Decrypts the contents of the string usint the key (and possibly &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; the IV) previously set. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : 'Input' &nbsp;the encrypted string to be decrypted &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : 'Output' the decrypted string &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Returns &nbsp; : OK if successful, otherwise error code &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.DecString(const Input: string; var Output: string);<br>var<br>&nbsp; &nbsp; &nbsp;i: longint;<br>&nbsp; &nbsp; &nbsp;j: longint;<br>&nbsp; &nbsp; &nbsp;s: string;<br>&nbsp; &nbsp; &nbsp;InputTemp: string;<br>begin<br>&nbsp; &nbsp; &nbsp;// check that we have a keys and IV<br>&nbsp; &nbsp; &nbsp;CheckKeys;<br><br>&nbsp; &nbsp; &nbsp;// initialise the output string<br>&nbsp; &nbsp; &nbsp;Output := '';<br><br>&nbsp; &nbsp; &nbsp;// check for zero length strings<br>&nbsp; &nbsp; &nbsp;if Length(Input) = 0 then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // don't need to do anything<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;// decode the string if required<br>&nbsp; &nbsp; &nbsp;if FStringMode = smEncode then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputTemp := DecodeString(Input);<br>&nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputTemp := Input;<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;// check that the input is long enough<br>&nbsp; &nbsp; &nbsp;if Length(InputTemp) &lt; BLOCKSIZE then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise EInputError.Create(LIT_INPUT_LENGTH);<br>&nbsp; &nbsp; &nbsp;end; {if Length}<br><br>&nbsp; &nbsp; &nbsp;// initialise the working string<br>&nbsp; &nbsp; &nbsp;s := '';<br><br>&nbsp; &nbsp; &nbsp;// preset the length of the 磜orking string<br>&nbsp; &nbsp; &nbsp;SetLength(s, Length(InputTemp));<br><br>&nbsp; &nbsp; &nbsp;// initialise the counters<br>&nbsp; &nbsp; &nbsp;i := 1;<br>&nbsp; &nbsp; &nbsp;j := 1;<br><br>&nbsp; &nbsp; &nbsp;// and step through the string<br>&nbsp; &nbsp; &nbsp;while i &lt; (Length(InputTemp)) do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // copy the next bytes into the context block buffer<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Move(InputTemp[j], ctx.ByteBuffer, BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(j, BLOCKSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // perform the decryption of the context<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DecryptBlockMode;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // copy the block into the output string<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Move(ctx.ByteBuffer, s, BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(i, BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp;end; {while i}<br><br>&nbsp; &nbsp; &nbsp;// Unpad Plain Text string<br>&nbsp; &nbsp; &nbsp;// Last byte is number of pad bytes<br>&nbsp; &nbsp; &nbsp;i := ord(s[Length(s)]);<br>&nbsp; &nbsp; &nbsp;if (i &gt; 0) and (i &lt;= BLOCKSIZE) then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Output := Copy(s, 1,Length(s) - i);<br>&nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Output := Copy(s, 1,Length(s) - 1);<br>&nbsp; &nbsp; &nbsp;end; {if (i&gt;0) and}<br>end; {DecryptString}<br><br>{*******************************************************************************<br>* Procedure : CBCMACBlock &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Returns the MAC of the data which has passed through the cipher &nbsp;*<br>********************************************************************************<br>* Paramters : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : The MAC as a TBlock &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>*******************************************************************************}<br>procedure TDES.CBCMACBlock(var MAC: TBlock);<br>begin<br>&nbsp; &nbsp; &nbsp;// pre initialise the MAC<br>&nbsp; &nbsp; &nbsp;FillChar(MAC, Sizeof(MAC), #0);<br><br>&nbsp; &nbsp; &nbsp;// check that the formalities are in order<br>&nbsp; &nbsp; &nbsp;if FCipherMode &lt;&gt; CBC then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // we can only reasonably produce a MAC in CBC mode<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise EKeyError.Create(LIT_CBC_NOT_SET);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp;end; {if}<br><br>&nbsp; &nbsp; &nbsp;// check that we have the key and IV<br>&nbsp; &nbsp; &nbsp;CheckKeys;<br><br>&nbsp; &nbsp; &nbsp;MAC := ctx.IV;<br>end; {CBCMACBlock}<br><br>{*******************************************************************************<br>* Procedure : CBCMACstring &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Returns the MAC of the data which has passed through the cipher &nbsp;*<br>********************************************************************************<br>* Paramters : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : The MAC as a string &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>*******************************************************************************}<br>procedure TDES.CBCMACString(var MAC: String);<br>var<br>&nbsp; &nbsp; &nbsp;MACTemp: TBlock;<br>&nbsp; &nbsp; &nbsp;i: integer;<br>begin<br>&nbsp; &nbsp; &nbsp;CBCMACBlock(MACTemp);<br><br>&nbsp; &nbsp; &nbsp;MAC := '';<br><br>&nbsp; &nbsp; &nbsp;for i := 0 to BLOCKSIZE-1 do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MAC := MAC + IntToHex(MACTemp,2);<br>&nbsp; &nbsp; &nbsp;end; {for}<br>end; {CBCMACString}<br><br><br>{*******************************************************************************<br>******************************** utility routines ******************************<br>*******************************************************************************}<br><br><br>{*******************************************************************************<br>* Procedure : Burn &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Clears the context of any sensitive information &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.Burn;<br>begin<br>&nbsp; &nbsp; &nbsp;// reset the whole context<br>&nbsp; &nbsp; &nbsp;FillChar(ctx, SizeOf(ctx), #0);<br>&nbsp; &nbsp; &nbsp;// reset the boolean members<br>&nbsp; &nbsp; &nbsp;ctx.KeyInit := False;<br>&nbsp; &nbsp; &nbsp;ctx.IVInit := False;<br>end; {Burn}<br><br>{*******************************************************************************<br>* Procedure : EncryptBlockMode &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Encrypts the contents of the context buffer, using the &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currently selected encryption mode. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.EncryptBlockMode;<br>var<br>&nbsp; &nbsp; &nbsp;i: integer;<br>begin<br>&nbsp; &nbsp; &nbsp;// perform the operation using the current mode<br>&nbsp; &nbsp; &nbsp;case FCipherMode of<br>&nbsp; &nbsp; &nbsp;CBC:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// xor the input block with the previous IV<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for i := 0 to BLOCKSIZE-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.ByteBuffer := ctx.ByteBuffer xor ctx.IV;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end; {for i}<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// perform the encryption on the block<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DES_Core_Block_Encrypt;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// copy the ciphertext to the IV<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move (ctx.ByteBuffer, ctx.IV, BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; {CBC}<br>&nbsp; &nbsp; &nbsp;ECB:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// ECB Mode has no preprocessing<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// perform the encryption on the block<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DES_Core_Block_Encrypt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; {ECB}<br>&nbsp; &nbsp; &nbsp;CFB:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Encipher the current IV to give the next IV<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move (ctx.ByteBuffer, ctx.ct, BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move (ctx.IV, ctx.ByteBuffer, BLOCKSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// perform the encryption on the block<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DES_Core_Block_Encrypt;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move(ctx.ByteBuffer, ctx.IV, BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move (ctx.ct, ctx.ByteBuffer, BLOCKSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// and xor the plaintext with the IV to get the ciphertext<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for i := 0 to BLOCKSIZE-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.ByteBuffer := ctx.ByteBuffer xor ctx.IV;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end; {for i}<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// feed the ciphertext back into the IV<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move (ctx.ByteBuffer, ctx.IV, BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; {CFB}<br>&nbsp; &nbsp; &nbsp;OFB:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// encrypt the previous IV to get the new IV<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move (ctx.ByteBuffer, ctx.ct, BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move (ctx.IV, ctx.ByteBuffer, BLOCKSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// perform the encryption on the block<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DES_Core_Block_Encrypt;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// move the IV back<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move (ctx.ByteBuffer, ctx.IV, BLOCKSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// the ciphertext is the plaintext xor IV<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for i := 0 to BLOCKSIZE-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.ByteBuffer := ctx.ct xor ctx.IV;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end; {for i}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; {OFB}<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br><br>{*******************************************************************************<br>* Procedure : DecryptBlockMode &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Decrypts the contents of the context buffer, using the &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currently selected encryption mode. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.DecryptBlockMode;<br>var<br>&nbsp; &nbsp; &nbsp;i: integer;<br>begin<br>&nbsp; &nbsp; &nbsp;// perform the operation using the current mode<br>&nbsp; &nbsp; &nbsp;case FCipherMode of<br>&nbsp; &nbsp; &nbsp;CBC:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// preserve the ciphertext in ct<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move(ctx.ByteBuffer, ctx.ct, BLOCKSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// perform the decryption on the block<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DES_Core_Block_Decrypt;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// the plaintext is the ciphertext xor IV<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for i := 0 to BLOCKSIZE-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.ByteBuffer := ctx.ByteBuffer xor ctx.IV;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end; {for i}<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// copy the ciphertext to the IV<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move(ctx.ct, ctx.IV, BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp;ECB:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// no preprocessing required<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// perform the decryption on the block<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DES_Core_Block_Decrypt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp;CFB:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Encipher the current IV to give the next IV<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// note that encipher is used<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move (ctx.ByteBuffer, ctx.ct, BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move (ctx.IV, ctx.ByteBuffer, BLOCKSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// perform the encryption on the IV<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DES_Core_Block_Encrypt;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move(ctx.ByteBuffer, ctx.IV, BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move (ctx.ct, ctx.ByteBuffer, BLOCKSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// and xor the plaintext with the IV to get the ciphertext<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for i := 0 to BLOCKSIZE-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.ByteBuffer := ctx.ByteBuffer xor ctx.IV;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// feed the ciphertext back into the IV<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move (ctx.ct, ctx.IV, BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; {CFB}<br>&nbsp; &nbsp; &nbsp;OFB:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// encrypt the previous IV to get the new IV<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// note that encipher is used<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move (ctx.ByteBuffer, ctx.ct, BLOCKSIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move (ctx.IV, ctx.ByteBuffer, BLOCKSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// perform the encryption on the IV<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DES_Core_Block_Encrypt;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// move the IV back<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Move (ctx.ByteBuffer, ctx.IV, BLOCKSIZE);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// the plaintext is the ciphertext xor IV<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for i := 0 to BLOCKSIZE-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.ByteBuffer := ctx.ct xor ctx.IV;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end; {for i}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; {OFB}<br>&nbsp; &nbsp; &nbsp;end; {case}<br>end; {DecryptBlockMode}<br><br>{*******************************************************************************<br>* Procedure : CheckKeys &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Checks that a valid key has been set before an encryption or &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decryption. If the mode is not ECB, check the IV as well. &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.CheckKeys;<br>begin<br>&nbsp; &nbsp; &nbsp;// check that we have a key<br>&nbsp; &nbsp; &nbsp;if not ctx.KeyInit then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if not, we cannot perform the encryption<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // exit with error code<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise EKeyError.Create(LIT_KEY_NOT_SET);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp;end; {if not ctx.Init}<br><br>&nbsp; &nbsp; &nbsp;// for modes other than ECB we also require an IV<br>&nbsp; &nbsp; &nbsp;// check that this has been set if the mode is not ECB<br>&nbsp; &nbsp; &nbsp;if FCipherMode &lt;&gt; ECB then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // mode is not ECB<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not ctx.IVInit then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// if not, we cannot perform the encryption<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// exit with error code<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;raise EKeyError.Create(LIT_IV_NOT_SET);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end; {if not ctx.IVInit}<br>&nbsp; &nbsp; &nbsp;end; {if FCipherMode}<br>end; {TDES.CheckKeys}<br><br><br>{******************************************************************}<br>{******************************************************************}<br>{****** the following functions encapsulate the DES algorithm *****}<br>{******************************************************************}<br>{******************************************************************}<br><br><br>{*******************************************************************************<br>* Procedure : DES_Func &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : The central engine of the DES algorithm &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : Data - The block to be encrypted &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Key - the key for the encryption &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure DES_Func(Data: PIntArray; Key: PInteger); register;<br>var<br>&nbsp; &nbsp; &nbsp;L: LongWord;<br>&nbsp; &nbsp; &nbsp;R: LongWord;<br>&nbsp; &nbsp; &nbsp;X: LongWord;<br>&nbsp; &nbsp; &nbsp;Y: LongWord;<br>&nbsp; &nbsp; &nbsp;i: LongWord;<br>begin<br>&nbsp; &nbsp; &nbsp;// perform the initial endianness swap<br>&nbsp; &nbsp; &nbsp;L := SwapInteger(Data[0]);<br>&nbsp; &nbsp; &nbsp;R := SwapInteger(Data[1]);<br><br>&nbsp; &nbsp; &nbsp;X := (L shr &nbsp;4 xor R) and $0F0F0F0F;<br>&nbsp; &nbsp; &nbsp;R := R xor X;<br>&nbsp; &nbsp; &nbsp;L := L xor X shl &nbsp;4;<br>&nbsp; &nbsp; &nbsp;X := (L shr 16 xor R) and $0000FFFF;<br>&nbsp; &nbsp; &nbsp;R := R xor X;<br>&nbsp; &nbsp; &nbsp;L := L xor X shl 16;<br>&nbsp; &nbsp; &nbsp;X := (R shr &nbsp;2 xor L) and $33333333;<br>&nbsp; &nbsp; &nbsp;L := L xor X;<br>&nbsp; &nbsp; &nbsp;R := R xor X shl &nbsp;2;<br>&nbsp; &nbsp; &nbsp;X := (R shr &nbsp;8 xor L) and $00FF00FF;<br>&nbsp; &nbsp; &nbsp;L := L xor X;<br>&nbsp; &nbsp; &nbsp;R := R xor X shl &nbsp;8;<br><br>&nbsp; &nbsp; &nbsp;R := R shl 1 or R shr 31;<br>&nbsp; &nbsp; &nbsp;X := (L xor R) and $AAAAAAAA;<br>&nbsp; &nbsp; &nbsp;R := R xor X;<br>&nbsp; &nbsp; &nbsp;L := L xor X;<br>&nbsp; &nbsp; &nbsp;L := L shl 1 or L shr 31;<br><br>&nbsp; &nbsp; &nbsp;for I := 0 to 7 do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; X := (R shl 28 or R shr 4) xor Key^;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(Key);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Y := R xor Key^; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(Key);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; L := L xor (DES_Data[0, X &nbsp; &nbsp; &nbsp; &nbsp;and $3F] or DES_Data[1, X shr &nbsp;8 and $3F] or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DES_Data[2, X shr 16 and $3F] or DES_Data[3, X shr 24 and $3F] or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DES_Data[4, Y &nbsp; &nbsp; &nbsp; &nbsp;and $3F] or DES_Data[5, Y shr &nbsp;8 and $3F] or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DES_Data[6, Y shr 16 and $3F] or DES_Data[7, Y shr 24 and $3F]);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; X := (L shl 28 or L shr 4) xor Key^;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(Key);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Y := L xor Key^;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(Key);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; R := R xor (DES_Data[0, X &nbsp; &nbsp; &nbsp; &nbsp;and $3F] or DES_Data[1, X shr &nbsp;8 and $3F] or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DES_Data[2, X shr 16 and $3F] or DES_Data[3, X shr 24 and $3F] or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DES_Data[4, Y &nbsp; &nbsp; &nbsp; &nbsp;and $3F] or DES_Data[5, Y shr &nbsp;8 and $3F] or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DES_Data[6, Y shr 16 and $3F] or DES_Data[7, Y shr 24 and $3F]);<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;R := R shl 31 or R shr 1;<br>&nbsp; &nbsp; &nbsp;X := (L xor R) and $AAAAAAAA;<br>&nbsp; &nbsp; &nbsp;R := R xor X;<br>&nbsp; &nbsp; &nbsp;L := L xor X;<br>&nbsp; &nbsp; &nbsp;L := L shl 31 or L shr 1;<br><br>&nbsp; &nbsp; &nbsp;X := (L shr &nbsp;8 xor R) and $00FF00FF; R := R xor X; L := L xor X shl &nbsp;8;<br>&nbsp; &nbsp; &nbsp;X := (L shr &nbsp;2 xor R) and $33333333; R := R xor X; L := L xor X shl &nbsp;2;<br>&nbsp; &nbsp; &nbsp;X := (R shr 16 xor L) and $0000FFFF; L := L xor X; R := R xor X shl 16;<br>&nbsp; &nbsp; &nbsp;X := (R shr &nbsp;4 xor L) and $0F0F0F0F; L := L xor X; R := R xor X shl &nbsp;4;<br><br>&nbsp; &nbsp; &nbsp;Data[0] := SwapInteger(R);<br>&nbsp; &nbsp; &nbsp;Data[1] := SwapInteger(L);<br>end; {DES_Func}<br><br>{*******************************************************************************<br>* Procedure : DES_Core_Key_Setup &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : Loads the KeyToSet into the context &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Paramters : 'KeyToSet' - the keyblock to be loaded &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.DES_Core_Key_Setup(const KeyToSet: TBlock);<br>var<br>&nbsp; &nbsp; &nbsp;K: array[0..7] of Byte;<br>begin<br>&nbsp; FillChar(K, SizeOf(K), 0);<br>&nbsp; Move(KeyToSet, K, 8);<br>&nbsp; DES_core_make_key(K, @ctx.FEncKey, False);<br>&nbsp; DES_core_make_key(K, @ctx.FDecKey, True);<br>&nbsp; FillChar(K, SizeOf(K), 0);<br>end; {DES_Core_Key_Setup}<br><br>{*******************************************************************************<br>* Procedure : DES_Core_Block_Encrypt &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : The block currently held in the buffer is enciphered with the &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key held in the context. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Paramters : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.DES_Core_Block_Encrypt;<br>begin<br>&nbsp; &nbsp; &nbsp;DES_Func(@ctx.ByteBuffer, @ctx.FEncKey);<br>end;{TDES.DES_Core_Block_Encrypt}<br><br>{*******************************************************************************<br>* Procedure : DES_Core_Block_Decrypt &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Purpose &nbsp; : The block currently held in the buffer is deciphered with the &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; key held in the context. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Paramters : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Returns &nbsp; : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.DES_Core_Block_Decrypt;<br>begin<br>&nbsp; &nbsp; &nbsp;DES_Func(@ctx.ByteBuffer, @ctx.FDecKey);<br>end;{TDES.DES_Core_Block_Encrypt}<br><br>{*******************************************************************************<br>* Procedure : DES_core_make_key &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Purpose &nbsp; : Makes either an encryption or a decryption key out of the key &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; material &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>********************************************************************************<br>* Paramters : Data - Key material &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Key - the computed key array &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Reverse - False for encryption, True for decryption &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br>********************************************************************************<br>* Returns &nbsp; : None &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br>*******************************************************************************}<br>procedure TDES.DES_core_make_key(const Data: array of Byte; Key: PInteger; Reverse: Boolean);<br>const<br>&nbsp; &nbsp; &nbsp;ROT: array[0..15] of Byte = (1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28);<br>var<br>&nbsp; &nbsp; &nbsp;I: LongWord;<br>&nbsp; &nbsp; &nbsp;J: LongWord;<br>&nbsp; &nbsp; &nbsp;L: LongWord;<br>&nbsp; &nbsp; &nbsp;M: LongWord;<br>&nbsp; &nbsp; &nbsp;N: LongWord;<br>&nbsp; &nbsp; &nbsp;PC_M: array[0..55] of Byte;<br>&nbsp; &nbsp; &nbsp;PC_R: array[0..55] of Byte;<br>&nbsp; &nbsp; &nbsp;K: array[0..31] of LongWord;<br>begin<br>&nbsp; &nbsp; &nbsp;FillChar(K, SizeOf(K), 0);<br>&nbsp; &nbsp; &nbsp;for I := 0 to 55 do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Data[DES_PC1 shr 3] and ($80 shr (DES_PC1 and $07)) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PC_M := 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PC_M := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;for I := 0 to 15 do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Reverse then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;M := (15 - I) shl 1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;M := I shl 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; N := M + 1;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for J := 0 to 27 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;L := J + ROT;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if L &lt; 28 then PC_R[J] := PC_M[L] else PC_R[J] := PC_M[L - 28];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for J := 28 to 55 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;L := J + ROT;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if L &lt; 56 then PC_R[J] := PC_M[L] else PC_R[J] := PC_M[L - 28];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; L := $1000000;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for J := 0 to 23 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;L := L shr 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if PC_R[DES_PC2[J &nbsp; &nbsp; ]] &lt;&gt; 0 then K[M] := K[M] or L;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if PC_R[DES_PC2[J + 24]] &lt;&gt; 0 then K[N] := K[N] or L;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp; &nbsp;for I := 0 to 15 do<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; M := I shl 1; N := M + 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Key^ := K[M] and $00FC0000 shl &nbsp;6 or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; K[M] and $00000FC0 shl 10 or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; K[N] and $00FC0000 shr 10 or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; K[N] and $00000FC0 shr &nbsp;6;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(Key);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Key^ := K[M] and $0003F000 shl 12 or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; K[M] and $0000003F shl 16 or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; K[N] and $0003F000 shr &nbsp;4 or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; K[N] and $0000003F;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Inc(Key);<br>&nbsp; &nbsp; &nbsp;end;<br>end;<br><br>function GetCPUType: Integer; assembler;<br>asm<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PUSH &nbsp; EBX<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PUSH &nbsp; ECX<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PUSH &nbsp; EDX<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MOV &nbsp; &nbsp;EBX,ESP<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AND &nbsp; &nbsp;ESP,0FFFFFFFCh<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PUSHFD<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PUSHFD<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;POP &nbsp; &nbsp;EAX<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MOV &nbsp; &nbsp;ECX,EAX<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XOR &nbsp; &nbsp;EAX,40000h<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PUSH &nbsp; EAX<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;POPFD<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PUSHFD<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;POP &nbsp; &nbsp;EAX<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XOR &nbsp; &nbsp;EAX,ECX<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MOV &nbsp; &nbsp;EAX,3<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JE &nbsp; &nbsp; @Exit<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PUSHFD<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;POP &nbsp; &nbsp;EAX<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MOV &nbsp; &nbsp;ECX,EAX<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XOR &nbsp; &nbsp;EAX,200000h<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PUSH &nbsp; EAX<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;POPFD<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PUSHFD<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;POP &nbsp; &nbsp;EAX<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;XOR &nbsp; &nbsp;EAX,ECX<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MOV &nbsp; &nbsp;EAX,4<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JE &nbsp; &nbsp; @Exit<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PUSH &nbsp; EBX<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MOV &nbsp; &nbsp;EAX,1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DB &nbsp; &nbsp; 0Fh,0A2h &nbsp; &nbsp; &nbsp;//CPUID<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MOV &nbsp; &nbsp;AL,AH<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AND &nbsp; &nbsp;EAX,0Fh<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;POP &nbsp; &nbsp;EBX<br>@Exit: &nbsp; POPFD<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MOV &nbsp; &nbsp;ESP,EBX<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;POP &nbsp; &nbsp;EDX<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;POP &nbsp; &nbsp;ECX<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;POP &nbsp; &nbsp;EBX<br>end;<br><br>function SwapInt(Value: LongWord): LongWord; assembler; register;<br>asm<br>&nbsp; &nbsp; &nbsp; &nbsp;XCHG &nbsp;AH,AL<br>&nbsp; &nbsp; &nbsp; &nbsp;ROL &nbsp; EAX,16<br>&nbsp; &nbsp; &nbsp; &nbsp;XCHG &nbsp;AH,AL<br>end;<br><br>function BSwapInt(Value: LongWord): LongWord; assembler; register;<br>asm<br>&nbsp; &nbsp; &nbsp; &nbsp;BSWAP &nbsp;EAX<br>end;<br><br>initialization<br>&nbsp; if GetCPUType &gt; 3 then<br>&nbsp; begin<br>&nbsp; &nbsp; SwapInteger := BSwapInt;<br>&nbsp; end<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; SwapInteger := SwapInt;<br>&nbsp; end;<br>end.<br>//本人使用中经过修改
 
delphiland非常感谢你!!!<br>能编译成控件或DLL文件吗?<br>在线等待!
 
其实这样我觉得最好,直接调用函数就好了。<br>要变成DLL也很简单,New一个Libary,再将代码COPY过去<br>再将函数Expert就好了。
 
直接加入到我的Project中?<br>我测试一下?
 
to delphiland:<br>我加入到我的Project中不能编译<br>[Fatal Error] tdes.pas(72): File not found: 'crc.dcu'<br>我去掉use中的crc也不行.<br>[Error] des.pas(1127): Undeclared identifier: 'crc32'<br>能将你的des.pas发给我吗?<br>我是delphi5.0谢谢!<br>
 
Email:?<br>{*******************************************************}<br>{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>{ &nbsp; &nbsp; &nbsp; 32位CRC循环冗余校验码实现单元 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>{ &nbsp; &nbsp; &nbsp; CRC For Windows &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>{ &nbsp; &nbsp; &nbsp; Copyright (R) 2001 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br>{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br>{*******************************************************}<br><br>{<br>&nbsp; &nbsp;说明:<br>&nbsp; &nbsp; &nbsp; &nbsp;使用的多项式($EDB88200):<br>&nbsp; &nbsp; &nbsp; &nbsp;x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x^1+x^0<br>&nbsp; &nbsp; &nbsp; &nbsp;这是由Ethernet/ADCCP等协议推荐的多项式.<br>}<br><br>Unit Crc;<br><br>interface<br><br>uses windows, SysUtils;<br><br>function crc32 (crc : Cardinal; buf : PByte; len : Integer): Cardinal; //实现计算<br><br>procedure make_crc_table; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//动态创建CRC表<br><br>var DynamicMakeCRCTableFlage:BOOL=False; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //是否动态产生CRC表<br><br>implementation<br><br>//CRC表预设静态表,可以加快校验速度<br>var<br>&nbsp; crc_table : array[0..255] of Cardinal = (<br>&nbsp; $00000000, $77073096, $ee0e612c, $990951ba, $076dc419,<br>&nbsp; $706af48f, $e963a535, $9e6495a3, $0edb8832, $79dcb8a4,<br>&nbsp; $e0d5e91e, $97d2d988, $09b64c2b, $7eb17cbd, $e7b82d07,<br>&nbsp; $90bf1d91, $1db71064, $6ab020f2, $f3b97148, $84be41de,<br>&nbsp; $1adad47d, $6ddde4eb, $f4d4b551, $83d385c7, $136c9856,<br>&nbsp; $646ba8c0, $fd62f97a, $8a65c9ec, $14015c4f, $63066cd9,<br>&nbsp; $fa0f3d63, $8d080df5, $3b6e20c8, $4c69105e, $d56041e4,<br>&nbsp; $a2677172, $3c03e4d1, $4b04d447, $d20d85fd, $a50ab56b,<br>&nbsp; $35b5a8fa, $42b2986c, $dbbbc9d6, $acbcf940, $32d86ce3,<br>&nbsp; $45df5c75, $dcd60dcf, $abd13d59, $26d930ac, $51de003a,<br>&nbsp; $c8d75180, $bfd06116, $21b4f4b5, $56b3c423, $cfba9599,<br>&nbsp; $b8bda50f, $2802b89e, $5f058808, $c60cd9b2, $b10be924,<br>&nbsp; $2f6f7c87, $58684c11, $c1611dab, $b6662d3d, $76dc4190,<br>&nbsp; $01db7106, $98d220bc, $efd5102a, $71b18589, $06b6b51f,<br>&nbsp; $9fbfe4a5, $e8b8d433, $7807c9a2, $0f00f934, $9609a88e,<br>&nbsp; $e10e9818, $7f6a0dbb, $086d3d2d, $91646c97, $e6635c01,<br>&nbsp; $6b6b51f4, $1c6c6162, $856530d8, $f262004e, $6c0695ed,<br>&nbsp; $1b01a57b, $8208f4c1, $f50fc457, $65b0d9c6, $12b7e950,<br>&nbsp; $8bbeb8ea, $fcb9887c, $62dd1ddf, $15da2d49, $8cd37cf3,<br>&nbsp; $fbd44c65, $4db26158, $3ab551ce, $a3bc0074, $d4bb30e2,<br>&nbsp; $4adfa541, $3dd895d7, $a4d1c46d, $d3d6f4fb, $4369e96a,<br>&nbsp; $346ed9fc, $ad678846, $da60b8d0, $44042d73, $33031de5,<br>&nbsp; $aa0a4c5f, $dd0d7cc9, $5005713c, $270241aa, $be0b1010,<br>&nbsp; $c90c2086, $5768b525, $206f85b3, $b966d409, $ce61e49f,<br>&nbsp; $5edef90e, $29d9c998, $b0d09822, $c7d7a8b4, $59b33d17,<br>&nbsp; $2eb40d81, $b7bd5c3b, $c0ba6cad, $edb88320, $9abfb3b6,<br>&nbsp; $03b6e20c, $74b1d29a, $ead54739, $9dd277af, $04db2615,<br>&nbsp; $73dc1683, $e3630b12, $94643b84, $0d6d6a3e, $7a6a5aa8,<br>&nbsp; $e40ecf0b, $9309ff9d, $0a00ae27, $7d079eb1, $f00f9344,<br>&nbsp; $8708a3d2, $1e01f268, $6906c2fe, $f762575d, $806567cb,<br>&nbsp; $196c3671, $6e6b06e7, $fed41b76, $89d32be0, $10da7a5a,<br>&nbsp; $67dd4acc, $f9b9df6f, $8ebeeff9, $17b7be43, $60b08ed5,<br>&nbsp; $d6d6a3e8, $a1d1937e, $38d8c2c4, $4fdff252, $d1bb67f1,<br>&nbsp; $a6bc5767, $3fb506dd, $48b2364b, $d80d2bda, $af0a1b4c,<br>&nbsp; $36034af6, $41047a60, $df60efc3, $a867df55, $316e8eef,<br>&nbsp; $4669be79, $cb61b38c, $bc66831a, $256fd2a0, $5268e236,<br>&nbsp; $cc0c7795, $bb0b4703, $220216b9, $5505262f, $c5ba3bbe,<br>&nbsp; $b2bd0b28, $2bb45a92, $5cb36a04, $c2d7ffa7, $b5d0cf31,<br>&nbsp; $2cd99e8b, $5bdeae1d, $9b64c2b0, $ec63f226, $756aa39c,<br>&nbsp; $026d930a, $9c0906a9, $eb0e363f, $72076785, $05005713,<br>&nbsp; $95bf4a82, $e2b87a14, $7bb12bae, $0cb61b38, $92d28e9b,<br>&nbsp; $e5d5be0d, $7cdcefb7, $0bdbdf21, $86d3d2d4, $f1d4e242,<br>&nbsp; $68ddb3f8, $1fda836e, $81be16cd, $f6b9265b, $6fb077e1,<br>&nbsp; $18b74777, $88085ae6, $ff0f6a70, $66063bca, $11010b5c,<br>&nbsp; $8f659eff, $f862ae69, $616bffd3, $166ccf45, $a00ae278,<br>&nbsp; $d70dd2ee, $4e048354, $3903b3c2, $a7672661, $d06016f7,<br>&nbsp; $4969474d, $3e6e77db, $aed16a4a, $d9d65adc, $40df0b66,<br>&nbsp; $37d83bf0, $a9bcae53, $debb9ec5, $47b2cf7f, $30b5ffe9,<br>&nbsp; $bdbdf21c, $cabac28a, $53b39330, $24b4a3a6, $bad03605,<br>&nbsp; $cdd70693, $54de5729, $23d967bf, $b3667a2e, $c4614ab8,<br>&nbsp; $5d681b02, $2a6f2b94, $b40bbe37, $c30c8ea1, $5a05df1b,<br>&nbsp; $2d02ef8d);<br><br>procedure make_crc_table;<br>var<br>&nbsp; c,n,k,poly : Cardinal;<br>const<br>&nbsp; p: array [0..13] of Byte = (0,1,2,4,5,7,8,10,11,12,16,22,23,26);<br>&nbsp; begin<br>&nbsp; &nbsp; poly := 0;<br>&nbsp; &nbsp; for n := 0 to (sizeof(p) div sizeof(Byte))-1 do<br>&nbsp; &nbsp; &nbsp; poly := poly or (1 shl (31 - p[n]));<br><br>&nbsp; &nbsp; for n := 0 to 255 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; c := n;<br>&nbsp; &nbsp; &nbsp; for k := 0 to 7 do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if (c and 1) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c := poly xor (c shr 1)<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c := (c shr 1);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; crc_table[n] := c;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br><br>function crc32 (crc : Cardinal; buf : PByte; len : Integer): Cardinal;<br>&nbsp; begin<br>&nbsp; &nbsp; if (buf = nil) then<br>&nbsp; &nbsp; &nbsp; crc32 := 0<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if DynamicMakeCRCTableFlage then<br>&nbsp; &nbsp; &nbsp; &nbsp; make_crc_table;<br><br>&nbsp; &nbsp; &nbsp; crc := crc xor $ffffffff;<br>&nbsp; &nbsp; &nbsp; while (len &gt;= 8) do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; {一次处理8个再循环来加快处理速度}<br>&nbsp; &nbsp; &nbsp; &nbsp; crc := crc_table[(crc xor Cardinal(buf^)) and $ff] xor (crc shr 8);<br>&nbsp; &nbsp; &nbsp; &nbsp; inc(buf);<br>&nbsp; &nbsp; &nbsp; &nbsp; crc := crc_table[(crc xor Cardinal(buf^)) and $ff] xor (crc shr 8);<br>&nbsp; &nbsp; &nbsp; &nbsp; inc(buf);<br>&nbsp; &nbsp; &nbsp; &nbsp; crc := crc_table[(crc xor Cardinal(buf^)) and $ff] xor (crc shr 8);<br>&nbsp; &nbsp; &nbsp; &nbsp; inc(buf);<br>&nbsp; &nbsp; &nbsp; &nbsp; crc := crc_table[(crc xor Cardinal(buf^)) and $ff] xor (crc shr 8);<br>&nbsp; &nbsp; &nbsp; &nbsp; inc(buf);<br>&nbsp; &nbsp; &nbsp; &nbsp; crc := crc_table[(crc xor Cardinal(buf^)) and $ff] xor (crc shr 8);<br>&nbsp; &nbsp; &nbsp; &nbsp; inc(buf);<br>&nbsp; &nbsp; &nbsp; &nbsp; crc := crc_table[(crc xor Cardinal(buf^)) and $ff] xor (crc shr 8);<br>&nbsp; &nbsp; &nbsp; &nbsp; inc(buf);<br>&nbsp; &nbsp; &nbsp; &nbsp; crc := crc_table[(crc xor Cardinal(buf^)) and $ff] xor (crc shr 8);<br>&nbsp; &nbsp; &nbsp; &nbsp; inc(buf);<br>&nbsp; &nbsp; &nbsp; &nbsp; crc := crc_table[(crc xor Cardinal(buf^)) and $ff] xor (crc shr 8);<br>&nbsp; &nbsp; &nbsp; &nbsp; inc(buf);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; Dec(len, 8);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; if (len &lt;&gt; 0) then<br>&nbsp; &nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; {剩余的不足8个字节,则单个处理}<br>&nbsp; &nbsp; &nbsp; &nbsp; crc := crc_table[(crc xor Cardinal(buf^)) and $ff] xor (crc shr 8);<br>&nbsp; &nbsp; &nbsp; &nbsp; inc(buf);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; Dec(len);<br>&nbsp; &nbsp; &nbsp; until (len = 0);<br>&nbsp; &nbsp; &nbsp; crc32 := crc xor $ffffffff;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end.
 
还是加入到Project中比较好,能不用dll就不用
 
已经可以编译了(加了crc.pas)非常感谢!<br>我今晚再测试测试,不用发EMAIL了。以后多联系duancy@163.net我在广州<br>qq:10130645
 
后退
顶部