1
0
mirror of https://github.com/StephenGenusa/DCPCrypt.git synced 2025-06-22 21:57:42 +02:00

Initial Commit with XE7 Package Files added

DCPCrypt by David Barton updated and tested with Delphi 2009, 2010, XE,
XE2, XE3, XE4, XE5 by Warren Postma. Updated for XE7 by Stephen Genusa.
This commit is contained in:
Stephen Genusa
2015-03-08 18:30:34 -05:00
parent de2a2bded5
commit 5fc435d45f
105 changed files with 21867 additions and 0 deletions

84
Docs/BlockCiphers.html Normal file
View File

@ -0,0 +1,84 @@
<html>
<head>
<title>DCPcrypt v2: Users Guide - Block Ciphers</title>
</head>
<body>
<p align="center"><font size="+2"><b>DCPcrypt Cryptographic Component Library v2</b></font><br>
<font size="+1">Copyright &copy; 1999-2002 David Barton<br>
<a href="http://www.cityinthesky.co.uk/">http://www.cityinthesky.co.uk/</a><br>
<a href="mailto:crypto@cityinthesky.co.uk">crypto@cityinthesky.co.uk</a></font>
<p><font size="+2">Block Ciphers - TDCP_blockcipher</font>
<p>All block ciphers are inherited from the TDCP_blockcipher component via either the TDCP_blockcipher64 and TDCP_blockcipher128 components (the latter implement the block size specific code).
<p>The TDCP_blockcipher component extends the TDCP_cipher component to provide chaining mode functions. Functions available are:
<pre>
property <a href="Ciphers.html#Initialized">Initialized</a>: boolean;
property <a href="Ciphers.html#Id">Id</a>: integer;
property <a href="Ciphers.html#Algorithm">Algorithm</a>: string;
property <a href="Ciphers.html#MaxKeySize">MaxKeySize</a>: integer;
property <a href="#BlockSize">BlockSize</a>: integer;
property <a href="#CipherMode">CipherMode</a>: TDCP_ciphermode;
class function <a href="Ciphers.html#SelfTest">SelfTest</a>: boolean;
procedure <a href="#SetIV">SetIV</a>(const Value);
procedure <a href="#GetIV">GetIV</a>(var Value);
procedure <a href="Ciphers.html#Init">Init</a>(const Key; Size: longword; InitVector: pointer);
procedure <a href="Ciphers.html#InitStr">InitStr</a>(const Key: string; HashType: TDCP_hashclass);
procedure <a href="Ciphers.html#Burn">Burn</a>;
procedure <a href="Ciphers.html#Reset">Reset</a>;
procedure <a href="Ciphers.html#Encrypt">Encrypt</a>(const Indata; var Outdata; Size: longword);
procedure <a href="Ciphers.html#Decrypt">Decrypt</a>(const Indata; var Outdata; Size: longword);
function <a href="Ciphers.html#EncryptStream">EncryptStream</a>(InStream, OutStream: TStream; Size: longword): longword;
function <a href="Ciphers.html#DecryptStream">DecryptStream</a>(InStream, OutStream: TStream; Size: longword): longword;
function <a href="Ciphers.html#EncryptString">EncryptString</a>(const Str: string): string;
function <a href="Ciphers.html#DecryptString">DecryptString</a>(const Str: string): string;
procedure <a href="#EncryptECB">EncryptECB</a>(const Indata; var Outdata);
procedure <a href="#DecryptECB">DecryptECB</a>(const Indata; var Outdata);
procedure <a href="#EncryptCBC">EncryptCBC</a>(const Indata; var Outdata; Size: longword);
procedure <a href="#DecryptCBC">DecryptCBC</a>(const Indata; var Outdata; Size: longword);
procedure <a href="#EncryptCFB8bit">EncryptCFB8bit</a>(const Indata; var Outdata; Size: longword);
procedure <a href="#DecryptCFB8bit">DecryptCFB8bit</a>(const Indata; var Outdata; Size: longword);
procedure <a href="#EncryptCFBblock">EncryptCFBblock</a>(const Indata; var Outdata; Size: longword);
procedure <a href="#DecryptCFBblock">DecryptCFBblock</a>(const Indata; var Outdata; Size: longword);
procedure <a href="#EncryptOFB">EncryptOFB</a>(const Indata; var Outdata; Size: longword);
procedure <a href="#DecryptOFB">DecryptOFB</a>(const Indata; var Outdata; Size: longword);
procedure <a href="#EncryptCTR">EncryptCTR</a>(const Indata; var Outdata; Size: longword);
procedure <a href="#DecryptCTR">DecryptCTR</a>(const Indata; var Outdata; Size: longword);
</pre>
<hr>
<p><font size="+2">Function descriptions</font>
<p><font size="+1"><a name="BlockSize">property BlockSize: integer;</a></font>
<p>This contains the block size of the cipher in BITS.
<p><font size="+1"><a name="CipherMode">property CipherMode: TDCP_ciphermode;</a></font>
<p>This is the current chaining mode used when <a href="Ciphers.html#Encrypt">Encrypt</a> is called. The available modes are:
<ul>
<li>cmCBC - Cipher block chaining.
<li>cmCFB8bit - 8bit cipher feedback.
<li>cmCFBblock - Cipher feedback (using the block size of the algorithm).
<li>cmOFB - Output feedback.
<li>cmCTR - Counter.
</ul>
<p>Each chaining mode has it's own pro's and cons. See any good book on cryptography or the NIST publication SP800-38A for details on each.
<p><font size="+1"><a name="SetIV">procedure SetIV(const Value);</a></font>
<p>Use this procedure to set the current chaining mode information to Value. This variable should be the same size as the block size. When <a href="Ciphers.html#Reset">Reset</a> is called subsequent to this, the chaining information will be set back to Value.
<p><font size="+1"><a name="GetIV">procedure GetIV(var Value);</a></font>
<p>This returns in Value the current chaining mode information, to get the initial chaining mode information you need to call <a href="Ciphers.html#Reset">Reset</a> before calling GetIV. The variable passed in Value must be at least the same size as the block size otherwise you will get a buffer overflow.
<p><font size="+1"><a name="EncryptCBC">procedure EncryptCBC(const Indata; var Outdata; Size: longword);</a></font><br>
<font size="+1"><a name="DecryptCBC">procedure DecryptCBC(const Indata; var Outdata; Size: longword);</a></font><br>
<font size="+1"><a name="EncryptCFB8bit">procedure EncryptCFB8bit(const Indata; var Outdata; Size: longword);</a></font><br>
<font size="+1"><a name="DecryptCFB8bit">procedure DecryptCFB8bit(const Indata; var Outdata; Size: longword);</a></font><br>
<font size="+1"><a name="EncryptCFBblock">procedure EncryptCFBblock(const Indata; var Outdata; Size: longword);</a></font><br>
<font size="+1"><a name="DecryptCFBblock">procedure DecryptCFBblock(const Indata; var Outdata; Size: longword);</a></font><br>
<font size="+1"><a name="EncryptOFB">procedure EncryptOFB(const Indata; var Outdata; Size: longword);</a></font><br>
<font size="+1"><a name="DecryptOFB">procedure DecryptOFB(const Indata; var Outdata; Size: longword);</a></font><br>
<font size="+1"><a name="EncryptCTR">procedure EncryptCTR(const Indata; var Outdata; Size: longword);</a></font><br>
<font size="+1"><a name="DecryptCTR">procedure DecryptCTR(const Indata; var Outdata; Size: longword);</a></font>
<p>These procedures encrypt/decrypt Size bytes of data from Indata and places the result in Outdata. These all employ chaining mode methods of encryption/decryption and so may need to be used inconjunction with <a href="Ciphers.html#Reset">Reset</a>. The CBC method uses short block encryption as specified in Bruce Schneier's "Applied Cryptography" for data blocks that are not multiples of the block size.
<p>&nbsp;
<p><a href="Index.html">Index</a>, <a href="Ciphers.html">Ciphers</a>, <a href="Hashes.html">Hashes</a>
<p>&nbsp;
<p><em>DCPcrypt is copyrighted &copy; 1999-2002 David Barton.<br>
All trademarks are property of their respective owners.</em>
</body>
</html>

273
Docs/Ciphers.html Normal file
View File

@ -0,0 +1,273 @@
<html>
<head>
<title>DCPcrypt v2: Users Guide - Ciphers</title>
</head>
<body>
<p align="center"><font size="+2"><b>DCPcrypt Cryptographic Component Library v2</b></font><br>
<font size="+1">Copyright &copy; 1999-2002 David Barton<br>
<a href="http://www.cityinthesky.co.uk/">http://www.cityinthesky.co.uk/</a><br>
<a href="mailto:crypto@cityinthesky.co.uk">crypto@cityinthesky.co.uk</a></font>
<p><font size="+2">Ciphers - TDCP_cipher</font>
<p>All ciphers are inherited from the TDCP_cipher component either directly for stream ciphers (such as RC4) or via the TDCP_blockcipher component.
<p>The TDCP_cipher component implements key initialisation features and the basic encryption/decryption interface. Functions available are:
<pre>
property <a href="#Initialized">Initialized</a>: boolean;
property <a href="#Id">Id</a>: integer;
property <a href="#Algorithm">Algorithm</a>: string;
property <a href="#MaxKeySize">MaxKeySize</a>: integer;
class function <a href="#SelfTest">SelfTest</a>: boolean;
procedure <a href="#Init">Init</a>(const Key; Size: longword; InitVector: pointer);
procedure <a href="#InitStr">InitStr</a>(const Key: string; HashType: TDCP_hashclass);
procedure <a href="#Burn">Burn</a>;
procedure <a href="#Reset">Reset</a>;
procedure <a href="#Encrypt">Encrypt</a>(const Indata; var Outdata; Size: longword);
procedure <a href="#Decrypt">Decrypt</a>(const Indata; var Outdata; Size: longword);
function <a href="#EncryptStream">EncryptStream</a>(InStream, OutStream: TStream; Size: longword): longword;
function <a href="#DecryptStream">DecryptStream</a>(InStream, OutStream: TStream; Size: longword): longword;
function <a href="#EncryptString">EncryptString</a>(const Str: string): string;
function <a href="#DecryptString">DecryptString</a>(const Str: string): string;
</pre>
<p>Example usage:
<ul>
<li><a href="#Example1">Example 1</a> - String encryption.
<li><a href="#Example2">Example 2</a> - File encryption.
<li><a href="#Example3">Example 3</a> - General encryption.
</ul>
<hr>
<p><font size="+2">Function descriptions</font>
<p><font size="+1"><a name="Initialized">property Initialized: boolean;</a></font>
<p>Once key initialization has been performed this property is set to true, otherwise it is set to false. Calling <a href="#Burn">Burn</a> will immediately set this to false.
<p><font size="+1"><a name="Id">property Id: integer;</a></font>
<p>Every algorithm I implement gets given a unique ID number so that if I use several different algorithms within a program I can determine which one was used. This is a purely arbitrary numbering system.
<p><font size="+1"><a name="Algorithm">property Algorithm: string;</a></font>
<p>This contains the name of the algorithm implemented within the component.
<p><font size="+1"><a name="MaxKeySize">property MaxKeySize: integer;</a></font>
<p>This is the maximum size of key you can pass to the cipher (in bits!).
<p><font size="+1"><a name="SelfTest">class function SelfTest: boolean;</a></font>
<p>In order to test whether the implementations have all been compiled correctly you can call the SelfTest function. This compares the results of several encryption/decryption operations with known results for the algorithms (so called test vectors). If all the tests are passed then true is returned. If ANY of the tests are failed then false is returned. You may want to run this function for all the components when you first install the DCPcrypt package and again if you modify any of the source files, you don't need to run this everytime your program is run. Note: this only performs a selection of tests, it is not exhaustive.
<p><font size="+1"><a name="Init">procedure Init(const Key; Size: longword; InitVector: pointer);</a></font>
<p>This procedure initializes the cipher with the keying material supplied in Key. The Size of the keying material is specified in <b>BITS</b>. The InitVector is a pointer to chaining information (only used for block ciphers). The variable that this points to should be equal to the block size of the algorithm. If <em>nil</em> is specified then (if necessary) an initialization vector is automatically generated from the key. Note: the method for generating automatic IVs is different from DCPcrypt v1.31, if this is a problem uncomment the DCPcrypt v1.31 compatibility mode line in DCPcrypt2.pas.
<p>Init example: use the hash of a string to initialize the cipher
<pre>
<b>procedure</b> TForm1.Button1Click(Sender: TObject);
<b>var</b>
Cipher: TDCP_rc4;
Hash: TDCP_sha1;
Digest: <b>array</b>[0..19] <b>of byte</b>; <em>// SHA-1 produces a 160bit (20byte) output</em>
<b>begin</b>
Hash:= TDCP_sha1.Create(Self);
Hash.Init; <em>// initialize the hash</em>
Hash.UpdateStr(Edit1.Text); <em>// generate a hash of Edit1.Text</em>
Hash.Final(Digest); <em>// save the hash in Digest</em>
Hash.Free;
Cipher:= TDCP_rc4.Create(Self);
Cipher.Init(Digest,Sizeof(Digest)*8,<b>nil</b>); <em>// remember size is in BITS (hence sizeof*8)</em>
...
</pre>
<p><font size="+1"><a name="InitStr">procedure InitStr(const Key: string; HashType: TDCP_hashclass);</a></font>
<p>This procedure initializes the cipher with a hash of the key string using the specified hash type (in a way similar to the example above). To replicate the behaviour from DCPcrypt v2 Beta 1 use Cipher.InitStr(KeyStr,TDCP_sha1).
<p>InitStr example: prompt the user for a passphrase to initialize the cipher
<pre>
<b>procedure</b> TForm1.Button1Click(Sender: TObject);
<b>var</b>
Cipher: TDCP_rc4;
<b>begin</b>
Cipher:= TDCP_rc4.Create(Self);
Cipher.InitStr(InputBox('Passphrase','Enter a passphrase',''),TDCP_sha1); <em>// prompt for a passphrase</em>
...
</pre>
<p><font size="+1"><a name="Burn">procedure Burn;</a></font>
<p>Once you have finished encrypting/decrypting all your data call Burn to erase all keying information. This is automatically called once the cipher is freed, however it is a good habit to call this procedure explicitly.
<p><font size="+1"><a name="Reset">procedure Reset;</a></font>
<p>Stream ciphers (and block ciphers in chaining modes) generally store chaining information that is dependant on the information already encrypted. Consequently decrypting a block of information immediately after encrypting it won't result in the original information because when you called the decrypt procedure the chaining information was different from when you called the encrypt procedure. Hence use Reset to restore the chaining information to it's original state.
<p>Remember that calling <a href="#EncryptString">EncryptString</a>, <a href="#DecryptString">DecryptString</a>, <a href="#EncryptStream">EncryptStream</a> and <a href="#DecryptStream">DecryptStream</a> will also affect the chaining information.
<p>Reset example: encrypting and decrypting
<pre>
<b>function</b> TestCipher: <b>boolean</b>;
<b>const</b>
InData: <b>array</b>[0..9] <b>of byte</b>= ($01,$23,$45,$56,$67,$78,$89,$10,$AB,$FF);
<b>var</b>
Cipher: TDCP_rc4;
Data: <b>array</b>[0..9] <b>of byte</b>;
<b>begin</b>
Cipher:= TDCP_rc4.Create(<b>nil</b>);
Cipher.InitStr('Hello World',TDCP_sha1); <em>// initialize the cipher</em>
Cipher.Encrypt(InData,Data,Sizeof(Data)); <em>// encrypt some known data</em>
Cipher.Decrypt(Data,Data,Sizeof(Data)); <em>// now decrypt it</em>
Cipher.Burn; <em>// clear keying information</em>
Cipher.Free;
Result:= CompareMem(@InData,@Data,Sizeof(Data)); <em>// compare input and output</em>
<b>end</b>;
</pre>
The above will ALWAYS result in false due to the chaining information.
<pre>
<b>function</b> TestCipher: <b>boolean</b>;
<b>const</b>
InData: <b>array</b>[0..9] <b>of byte</b>= ($01,$23,$45,$56,$67,$78,$89,$10,$AB,$FF);
<b>var</b>
Cipher: TDCP_rc4;
Data: <b>array</b>[0..9] <b>of byte</b>;
<b>begin</b>
Cipher:= TDCP_rc4.Create(<b>nil</b>);
Cipher.InitStr('Hello World',TDCP_sha1); <em>// initialize the cipher</em>
Cipher.Encrypt(InData,Data,Sizeof(Data)); <em>// encrypt some known data</em>
Cipher.Reset; <em><b>// reset chaining information</b></em>
Cipher.Decrypt(Data,Data,Sizeof(Data)); <em>// now decrypt it</em>
Cipher.Burn; <em>// clear keying information</em>
Cipher.Free;
Result:= CompareMem(@InData,@Data,Sizeof(Data)); <em>// compare input and output</em>
<b>end</b>;
</pre>
The above <em>should</em> always return true.
<p><font size="+1"><a name="Encrypt">procedure Encrypt(const Indata; var Outdata; Size: longword);</a></font>
<p>Encrypt Size bytes from Indata and place it in Outdata. Block ciphers encrypt the data using the method specified by the <a href="BlockCiphers.html#CipherMode">CipherMode</a> property. Also see the notes on <a href="#Reset">Reset</a>.
<p><font size="+1"><a name="Decrypt">procedure Decrypt(const Indata; var Outdata; Size: longword);</a></font>
<p>Decrypt Size bytes from Indata and place it in Outdata. Block ciphers decrypt the data using the method specified by the <a href="BlockCiphers.html#CipherMode">CipherMode</a> property. Also see the notes on <a href="#Reset">Reset</a>.
<p><font size="+1"><a name="EncryptStream">function EncryptStream(InStream, OutStream: TStream; Size: longword): longword;</a></font>
<p>Encrypt Size bytes from the InStream and place it in the OutStream, returns the number of bytes read from the InStream. Encryption is done by calling the <a href="#Encrypt">Encrypt</a> procedure. Also see the notes on <a href="#Reset">Reset</a>.
<p><font size="+1"><a name="DecryptStream">function DecryptStream(InStream, OutStream: TStream; Size: longword): longword;</a></font>
<p>Decrypt Size bytes from the InStream and place it in the OutStream, returns the number of bytes read from the InStream. Decryption is done by calling the <a href="#Decrypt">Decrypt</a> procedure. Also see the notes on <a href="#Reset">Reset</a>.
<p><font size="+1"><a name="EncryptString">function EncryptString(const Str: string): string;</a></font>
<p>Encrypt the string Str then Base64 encode it and return the result. For stream ciphers the <a href="#Encrypt">Encrypt</a> procedure is called to do the encryption, for block ciphers the <a href="BlockCiphers.html#EncryptCFB8bit">CFB8bit</a> method is always used. Base64 encoding is used to ensure that the output string doesn't contain non-printing characters.
<p><font size="+1"><a name="DecryptString">function DecryptString(const Str: string): string;</a></font>
<p>Base64 decode the string then decrypt it and return the result. For stream ciphers the <a href="#Decrypt">Decrypt</a> procedure is called to do the decryption, for block ciphers the <a href="BlockCiphers.html#DecryptCFB8bit">CFB8bit</a> method is always used.
<hr>
<p><font size="+2"><a name="Example1">Example 1: String encryption</a></font>
<p>This example shows how you can encrypt the contents of a TMemo and leave the contents printable.
<pre>
<b>procedure</b> TForm1.btnEncryptClick(Sender: TObject);
<b>var</b>
i: <b>integer</b>;
Cipher: TDCP_rc4;
KeyStr: string;
<b>begin</b>
KeyStr:= '';
<b>if</b> InputQuery('Passphrase','Enter passphrase',KeyStr) <b>then</b> <em>// get the passphrase</em>
<b>begin</b>
Cipher:= TDCP_rc4.Create(Self);
Cipher.InitStr(KeyStr,TDCP_sha1); <em>// initialize the cipher with a hash of the passphrase</em>
<b>for</b> i:= 0 <b>to</b> Memo1.Lines.Count-1 <b>do</b> <em>// encrypt the contents of the memo</em>
Memo1.Lines[i]:= Cipher.EncryptString(Memo1.Lines[i]);
Cipher.Burn;
Cipher.Free;
<b>end</b>;
<b>end</b>;
<b>procedure</b> TForm1.btnDecryptClick(Sender: TObject);
<b>var</b>
i: <b>integer</b>;
Cipher: TDCP_rc4;
KeyStr: string;
<b>begin</b>
KeyStr:= '';
<b>if</b> InputQuery('Passphrase','Enter passphrase',KeyStr) <b>then</b> <em>// get the passphrase</em>
<b>begin</b>
Cipher:= TDCP_rc4.Create(Self);
Cipher.InitStr(KeyStr,TDCP_sha1); <em>// initialize the cipher with a hash of the passphrase</em>
<b>for</b> i:= 0 <b>to</b> Memo1.Lines.Count-1 <b>do</b> <em>// decrypt the contents of the memo</em>
Memo1.Lines[i]:= Cipher.DecryptString(Memo1.Lines[i]);
Cipher.Burn;
Cipher.Free;
<b>end</b>;
<b>end</b>;
</pre>
<hr>
<p><font size="+2"><a name="Example2">Example 2: File encryption</a></font>
<p>This example shows how you can encrypt the contents of a file, takes the input and output file names from two edit boxes: boxInputFile and boxOutputFile.
<pre>
<b>procedure</b> TForm1.btnEncryptClick(Sender: TObject);
<b>var</b>
Cipher: TDCP_rc4;
KeyStr: string;
Source, Dest: TFileStream;
<b>begin</b>
KeyStr:= '';
<b>if</b> InputQuery('Passphrase','Enter passphrase',KeyStr) <b>then</b> <em>// get the passphrase</em>
<b>begin</b>
<b>try</b>
Source:= TFileStream.Create(boxInputFile.Text,fmOpenRead);
Dest:= TFileStream.Create(boxOutputFile.Text,fmCreate);
Cipher:= TDCP_rc4.Create(Self);
Cipher.InitStr(KeyStr,TDCP_sha1); <em>// initialize the cipher with a hash of the passphrase</em>
Cipher.EncryptStream(Source,Dest,Source.Size); <em>// encrypt the contents of the file</em>
Cipher.Burn;
Cipher.Free;
Dest.Free;
Source.Free;
MessageDlg('File encrypted',mtInformation,[mbOK],0);
<b>except</b>
MessageDlg('File IO error',mtError,[mbOK],0);
<b>end</b>;
<b>end</b>;
<b>end</b>;
<b>procedure</b> TForm1.btnDecryptClick(Sender: TObject);
<b>var</b>
Cipher: TDCP_rc4;
KeyStr: string;
Source, Dest: TFileStream;
<b>begin</b>
KeyStr:= '';
<b>if</b> InputQuery('Passphrase','Enter passphrase',KeyStr) <b>then</b> <em>// get the passphrase</em>
<b>begin</b>
<b>try</b>
Source:= TFileStream.Create(boxInputFile.Text,fmOpenRead);
Dest:= TFileStream.Create(boxOutputFile.Text,fmCreate);
Cipher:= TDCP_rc4.Create(Self);
Cipher.InitStr(KeyStr,TDCP_sha1); <em>// initialize the cipher with a hash of the passphrase</em>
Cipher.DecryptStream(Source,Dest,Source.Size); <em>// decrypt the contents of the file</em>
Cipher.Burn;
Cipher.Free;
Dest.Free;
Source.Free;
MessageDlg('File decrypted',mtInformation,[mbOK],0);
<b>except</b>
MessageDlg('File IO error',mtError,[mbOK],0);
<b>end</b>;
<b>end</b>;
<b>end</b>;
</pre>
<hr>
<p><font size="+2"><a name="Example3">Example 3: General encryption</a></font>
<p>This hypothetical example shows how you might encrypt a packet of information before transmission across a network.
<pre>
<b>type</b>
TSomePacket= <b>record</b>
Date: <b>double</b>;
ToUserID: <b>integer</b>;
FromUserID: <b>integer</b>;
MsgLen: <b>integer</b>;
Msg: string;
<b>end</b>;
<b>procedure</b> EncryptPacket(Cipher: TDCP_cipher; <b>var</b> Packet: TSomePacket);
<em>// encrypt the information packet with the cipher
// if the cipher isn't initialized then prompt for passphrase</em>
<b>begin</b>
<b>if</b> Cipher= <b>nil then</b>
<b>raise</b> Exception.Create('Cipher hasn''t been created!')
<b>else
begin</b>
<b>if not</b> Cipher.Initialized <b>then</b> <em>// check the cipher has been initialized</em>
Cipher.InitStr(InputBox('Passphrase','Enter passphrase',''),TDCP_sha1);
<b>if</b> Cipher <b>is</b> TDCP_blockcipher <b>then</b> <em>// if a block cipher use CFB 8bit as encrypting small packets</em>
TDCP_blockcipher(Cipher).CipherMode:= cmCFB8bit;
<em>// encrypt the record part by part, could do this in one go if it was a packed record</em>
Cipher.Encrypt(Packet.Date,Packet.Date,Sizeof(Packet.Date));
Cipher.Encrypt(Packet.ToUserID,Packet.ToUserID,Sizeof(Packet.ToUserID));
Cipher.Encrypt(Packet.FromUserID,Packet.FromUserID,Sizeof(Packet.FromUserID));
Cipher.Encrypt(Packet.MsgLen,Packet.MsgLen,Sizeof(Packet.MsgLen));
Cipher.Encrypt(Packet.Msg[1],Packet.Msg[1],Length(Packet.Msg)); <em>// slightly different for strings</em>
<em>// don't bother resetting the cipher, instead keep the chaining information</em>
<b>end</b>;
<b>end</b>;
</pre>
<p>&nbsp;
<p><a href="Index.html">Index</a>, <a href="BlockCiphers.html">Block Ciphers</a>, <a href="Hashes.html">Hashes</a>
<p>&nbsp;
<p><em>DCPcrypt is copyrighted &copy; 1999-2002 David Barton.<br>
All trademarks are property of their respective owners.</em>
</body>
</html>

107
Docs/Hashes.html Normal file
View File

@ -0,0 +1,107 @@
<html>
<head>
<title>DCPcrypt v2: Users Guide - Hash Algorithms</title>
</head>
<body>
<p align="center"><font size="+2"><b>DCPcrypt Cryptographic Component Library v2</b></font><br>
<font size="+1">Copyright &copy; 1999-2002 David Barton<br>
<a href="http://www.cityinthesky.co.uk/">http://www.cityinthesky.co.uk/</a><br>
<a href="mailto:crypto@cityinthesky.co.uk">crypto@cityinthesky.co.uk</a></font>
<p><font size="+2">Hash Algorithms - TDCP_hash</font>
<p>All hashes are derived from the TDCP_hash component. It provides a range of functions to allow the hashing of virtually every type of data.
<p>Functions available are:
<pre>
property <a href="#Initialized">Initialized</a>: boolean;
property <a href="#Id">Id: integer</a>;
property <a href="#Algorithm">Algorithm</a>: string;
property <a href="#HashSize">HashSize</a>: integer;
class function <a href="#SelfTest">SelfTest</a>: boolean;
procedure <a href="#Init">Init</a>;
procedure <a href="#Final">Final</a>(var Digest);
procedure <a href="#Burn">Burn</a>;
procedure <a href="#Update">Update</a>(const Buffer; Size: longword);
procedure <a href="#UpdateStream">UpdateStream</a>(Stream: TStream; Size: longword);
procedure <a href="#UpdateStr">UpdateStr</a>(const Str: string);
</pre>
<p>Example usage:
<ul>
<li><a href="#Example1">Example 1</a> - File hashing.
</ul>
<hr>
<p><font size="+1"><a name="Initialized">property Initialized: boolean;</a></font>
<p>This is set to true after <a href="#Init">Init</a> has been called.
<p><font size="+1"><a name="Id">property Id: integer;</a></font>
<p>Every algorithm I implement gets given a unique ID number so that if I use several different algorithms within a program I can determine which one was used. This is a purely arbitrary numbering system.
<p><font size="+1"><a name="Algorithm">property Algorithm: string;</a></font>
<p>This is the name of the algorithm implemented in the component.
<p><font size="+1"><a name="HashSize">property HashSize: integer;</a></font>
<p>This is the size of the output of the hash algorithm in BITS.
<p><font size="+1"><a name="SelfTest">class function SelfTest: boolean;</a></font>
<p>In order to test whether the implementations have all been compiled correctly you can call the SelfTest function. This compares the results of several hash operations with known results for the algorithms (so called test vectors). If all the tests are passed then true is returned. If ANY of the tests are failed then false is returned. You may want to run this function for all the components when you first install the DCPcrypt package and again if you modify any of the source files, you don't need to run this everytime your program is run. Note: this only performs a selection of tests, it is not exhaustive.
<p><font size="+1"><a name="Init">procedure Init;</a></font>
<p>Call this procedure to initialize the hash algorithm, this must be called before using the <a href="#Update">Update</a> procedure.
<p><font size="+1"><a name="Final">procedure Final(var Digest);</a></font>
<p>This procedure returns the final message digest (hash) in Digest. This variable must be the same size as the hash size. This procedure also calls <a href="#Burn">Burn</a> to clear any stored information.
<p><font size="+1"><a name="Burn">procedure Burn;</a></font>
<p>Call this procedure if you want to abort the hashing operation (normally <a href="#Final">Final</a> is used). This clears all information stored within the hash. Before the hash can be used again <a href="#Init">Init</a> must be called.
<p><font size="+1"><a name="Update">procedure Update(const Buffer; Size: longword);</a></font>
<p>This procedure hashes Size bytes of Buffer. To get the hash result call <a href="#Final">Final</a>.
<p>Update example:
<pre>
<b>procedure</b> HashBuffer(<b>const</b> Buffer; Size: <b>longint</b>; <b>var</b> Output);
<b>var</b>
Hash: TDCP_ripemd160;
<b>begin</b>
Hash:= TDCP_ripemd160.Create(<b>nil</b>);
Hash.Init;
Hash.Update(Buffer,Size);
Hash.Final(Output);
Hash.Free;
<b>end</b>;
</pre>
<p><font size="+1"><a name="UpdateStream">procedure UpdateStream(Stream: TStream; Size: longword);</a></font>
<p>This procedure hashes Size bytes from Stream. To get the hash result call <a href="#Final">Final</a>.
<p><font size="+1"><a name="UpdateStr">procedure UpdateStr(const Str: string);</a></font>
<p>This procedure hashes the string Str. To get the hash result call <a href="#Final">Final</a>.
<hr>
<p><font size="+2"><a name="Example1">Example 1 - File hashing</a></font>
<p>This example shows how you can hash the contents of a file
<pre>
<b>procedure</b> TForm1.Button1Click(Sender: TObject);
<b>var</b>
Hash: TDCP_ripemd160;
Digest: <b>array</b>[0..19] <b>of</b> <b>byte</b>; <em>// RipeMD-160 produces a 160bit digest (20bytes)</em>
Source: TFileStream;
i: <b>integer</b>;
s: string;
<b>begin</b>
Source:= <b>nil</b>;
<b>try</b>
Source:= TFileStream.Create(Edit1.Text,fmOpenRead); <em>// open the file specified by Edit1</em>
<b>except</b>
MessageDlg('Unable to open file',mtError,[mbOK],0);
<b>end</b>;
<b>if</b> Source <> <b>nil then</b>
<b>begin</b>
Hash:= TDCP_ripemd160.Create(Self); <em>// create the hash</em>
Hash.Init; <em>// initialize it</em>
Hash.UpdateStream(Source,Source.Size); <em>// hash the stream contents</em>
Hash.Final(Digest); <em>// produce the digest</em>
Source.Free;
s:= '';
<b>for</b> i:= 0 <b>to</b> 19 <b>do</b>
s:= s + IntToHex(Digest[i],2);
Edit2.Text:= s; <em>// display the digest</em>
<b>end</b>;
<b>end</b>;
</pre>
<p>&nbsp;
<p><a href="Index.html">Index</a>, <a href="Ciphers.html">Ciphers</a>, <a href="BlockCiphers.html">Block Ciphers</a>
<p>&nbsp;
<p><em>DCPcrypt is copyrighted &copy; 1999-2002 David Barton.<br>
All trademarks are property of their respective owners.</em>
</body>
</html>

270
Docs/Index.html Normal file
View File

@ -0,0 +1,270 @@
<html>
<head>
<title>DCPcrypt v2: Users Guide - Index</title>
</head>
<body>
<p align="center"><font size="+2"><b>DCPcrypt Cryptographic Component Library v2 Beta 3</b></font><br>
<font size="+1">Copyright &copy; 1999-2002 David Barton<br>
<a href="http://www.cityinthesky.co.uk/">http://www.cityinthesky.co.uk/</a><br>
<a href="mailto:crypto@cityinthesky.co.uk">crypto@cityinthesky.co.uk</a></font>
<p><font size="+2">Introduction</font>
<p>DCPcrypt is a collection of cryptographic components for the Borland Delphi(tm), C++ Builder(tm) and Kylix(tm) programming languages. The supported versions are Delphi 4, 5, 6 and 7, C++ Builder (3?), 4, 5, 6 and Kylix 1 (untested) and 2.
<p>The idea behind DCPcrypt is that it should be possible to "drop in" any algorithm implementation to replace another with minimum or no code changes. To aid in this goal all cryptographic components are descended from one of several base classes, TDCP_cipher for encryption algorithms and TDCP_hash for message digest algorithms.
<p>
<table>
<tr>
<td valign="center"><a href="http://www.opensource.org/docs/definition.php"><img src="osi-certified-120x100.png" border="0"></a></td>
<td valign="center">
<p>DCPcrypt is open source software (released under the <a href="MIT_license.txt">MIT license</a>) and as such there is no charge for inclusion in other software. However, I am currently a student and if you are making money from my software I would really appreciate a donation of some sort, whether financial or a license for the software you develop (<em>or if anyone wants to sponsor a Mathematical Modelling (Masters) student for their final year...</em>). Please note THIS IS NOT COMPULSORY IN ANY WAY. See <a href="http://www.cityinthesky.co.uk/cryptography.html">http://www.cityinthesky.co.uk/cryptography.html</a> for details on donations.
<p>This software is OSI Certified Open Source Software. OSI Certified is a certification mark of the <a href="http://www.opensource.org/">Open Source Initiative</a>.
<p>If you maintain a website then a link to my page at <a href="http://www.cityinthesky.co.uk/">http://www.cityinthesky.co.uk/</a> would be great!
</td>
</tr>
</table>
<p>&nbsp;
<p><font size="+2">What's New</font>
<p>Changes since DCPcrypt v2 Beta 2:
<ul>
<li>Corrected C++ Builder compilation problem.
</ul>
<p>Changes since DCPcrypt v2 Beta 1:
<ul>
<li>Renamed source code files for hashes and ciphers to DCPxxx.pas
<li>Change the format of Cipher.InitStr so that the hash algorithm used to generate the key is explicitly specified. In order to get the same functionality as before, use TDCP_sha1. e.g. Cipher.InitStr('Hello World',TDCP_sha1);
<li>Block ciphers are now inherited from an intermediate component that implements the block size specific chaining mode encryption routines.
<li>Remove the internal component registration, it was more hassle than it was worth. If there is a demand for this to be put back then I might...
<li>Added the full range of operation modes for Haval. By changing the defines at the top of DCPhaval.pas you can specify the number of passes and the output hash size.
<li>Added the Tiger hash algorithm (192bit digest).
<li>Changed the name of the file containing TDCP_ripemd160 for consistency to DCPripemd160 from DCPrmd160.
<li>GOST no longer appears on the component palette pending verifying what the actual standard is (the code is still included however).
<li>Added the RipeMD-128 hash algorithm (128bit digest).
<li>Added the Serpent block cipher (AES finalist).
<li>Added the SHA-256,384,512 hash algorithms (256, 384, 512bit digest respectively).
<li>Added CTR chaining mode to all block ciphers.
</ul>
<p>&nbsp;
<p><font size="+2">Installation</font>
<p>
<table>
<tr>
<td width="120">Delphi</td>
<td>Open the appropriate package, DCPdelphiX.dpk where X is your version of Delphi (either 4, 5 or 6). Then press the install button.</td>
</tr>
<tr>
<td width="120">C++ Builder</td>
<td>Create a new design time package and add all the .pas files from the DCPcrypt2.zip archive including all those in the Ciphers and Hashes subdirectories. Then press the install button.</td>
</tr>
<tr>
<td width="120">Kylix</td>
<td>Open the DCPkylix.dpk package and then press the install button (note: Kylix 1 users may need to create a new package as with C++ Builder as this is a Kylix 2 package).</td>
</tr>
<table>
<p>You may need to add the directory containing DCPcrypt (and the Ciphers and Hashes subdirectories) to your library search path (found under Environment Options).
<p>Once installed you will find two extra pages of components on your component palette, namely DCPciphers and DCPhashes. You can now place these components onto the form of your application to start using the algorithms.
<p>&nbsp;
<p><font size="+2">Usage</font>
<p>Please note that an appreciation of the basic principles of encryption/decryption and key management is needed to ensure the correct usage of the ciphers implemented within this package. A good introduction on this subject is provided by Bruce Schneier's "Applied Cryptography" (ISBN: 0-471-11709-9) also see the NIST publication SP800-38A for information on the block cipher chaining modes.
<ul>
<li><a href="Ciphers.html">Ciphers</a> - the basic building block of DCPcrypt, the TDCP_cipher component.
<li><a href="BlockCiphers.html">Block Ciphers</a> - the base of all block ciphers, the TDCP_blockcipher component.
<li><a href="Hashes.html">Hashes</a> - the base of all hash algorithms, the TDCP_hash component.
</ul>
<p>DCPcrypt v2 contains the following ciphers and hash algorithms:
<p>
<table bgcolor="#FFFFCC" align="center">
<tr>
<td colspan="4" align="center"><font size="+1"><b>Ciphers</b></font></td>
</tr>
<tr>
<td><b>Name</b></td>
<td><b>Patents</b></td>
<td><b>Block Size</b></td>
<td><b>Max Key Size*</b></td>
</tr>
<tr>
<td>Blowfish</td>
<td>None</td>
<td>64 bits</td>
<td>448 bits</td>
</tr>
<tr>
<td>Cast-128</td>
<td>None</td>
<td>64 bits</td>
<td>128 bits</td>
</tr>
<tr>
<td>Cast-256</td>
<td>Patented?</td>
<td>128 bits</td>
<td>256 bits</td>
</tr>
<tr>
<td>DES</td>
<td>None</td>
<td>64 bits**</td>
<td>64 bits</td>
</tr>
<tr>
<td>3DES</td>
<td>None</td>
<td>64 bits</td>
<td>192 bits</td>
</tr>
<tr>
<td>Ice</td>
<td>None?</td>
<td>64 bits</td>
<td>64 bits</td>
</tr>
<tr>
<td>Thin Ice</td>
<td>None?</td>
<td>64 bits</td>
<td>64 bits</td>
</tr>
<tr>
<td>Ice 2</td>
<td>None?</td>
<td>64 bits</td>
<td>128 bits</td>
</tr>
<tr>
<td>IDEA</td>
<td>Free for non-commercial use</td>
<td>64 bits</td>
<td>128 bits</td>
</tr>
<tr>
<td>MARS</td>
<td>Patented?</td>
<td>128 bits</td>
<td>1248 bits</td>
</tr>
<tr>
<td>Misty1</td>
<td>Free for non-commercial use</td>
<td>64 bits</td>
<td>128 bits</td>
</tr>
<tr>
<td>RC2</td>
<td>None</td>
<td>64 bits</td>
<td>1024 bits</td>
</tr>
<tr>
<td>RC4</td>
<td>None</td>
<td>N/A</td>
<td>2048 bits</td>
</tr>
<tr>
<td>RC5</td>
<td>Patented</td>
<td>64 bits</td>
<td>2048 bits</td>
</tr>
<tr>
<td>RC6</td>
<td>Patented</td>
<td>128 bits</td>
<td>2048 bits</td>
</tr>
<tr>
<td>Rijndael (AES)</td>
<td>None</td>
<td>128 bits</td>
<td>256 bits</td>
</tr>
<tr>
<td>Serpent</td>
<td>None</td>
<td>128 bits</td>
<td>256 bits</td>
</tr>
<tr>
<td>TEA</td>
<td>None</td>
<td>64 bits</td>
<td>128 bits</td>
</tr>
<tr>
<td>Twofish</td>
<td>None</td>
<td>128 bits</td>
<td>256 bits</td>
</tr>
</table>
<p>* although the quoted maximum key size may extremely large it doen't mean that the algorithm is secure to the same level.<br>
** a 64bit key is used for DES then every 8th bit is discarded (parity) so the effective size is 56 bits.
<p>
<table bgcolor="#FFFFCC" align="center">
<tr>
<td colspan="3" align="center"><font size="+1"><b>Hash Algorithms</b><font></td>
</tr>
<tr>
<td><b>Name</b></td>
<td><b>Patents</b></td>
<td><b>Digest Size</b></td>
</tr>
<tr>
<td>Haval</td>
<td>None</td>
<td>128, 160, 192, 224, 256 bits*</td>
</tr>
<tr>
<td>MD4</td>
<td>None</td>
<td>128 bits</td>
</tr>
<tr>
<td>MD5</td>
<td>None</td>
<td>128 bits</td>
</tr>
<tr>
<td>RipeMD-128</td>
<td>None</td>
<td>128 bits</td>
</tr>
<tr>
<td>RipeMD-160</td>
<td>None</td>
<td>160 bits</td>
</tr>
<tr>
<td>SHA-1</td>
<td>None</td>
<td>160 bits</td>
</tr>
<tr>
<td>SHA-256</td>
<td>None</td>
<td>256 bits</td>
</tr>
<tr>
<td>SHA-384</td>
<td>None</td>
<td>384 bits</td>
</tr>
<tr>
<td>SHA-512</td>
<td>None</td>
<td>512 bits</td>
</tr>
<tr>
<td>Tiger</td>
<td>None</td>
<td>192 bits</td>
</tr>
</table>
<p>* The different digest sizes of Haval can be accessed by uncommenting the $defines at the start of DCPhaval.pas.
<p>&nbsp;
<p><font size="+2">Contact</font>
<p>I appreciate knowing what DCPcrypt is being used for and also if you have any queries or bug reports please email me at <a href="mailto:crypto@cityinthesky.co.uk">crypto@cityinthesky.co.uk</a>.
<p>&nbsp;
<p><em>DCPcrypt is copyrighted &copy; 1999-2003 David Barton.<br>
All trademarks are property of their respective owners.</em>
</body>
</html>

21
Docs/MIT_license.txt Normal file
View File

@ -0,0 +1,21 @@
The MIT License
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB