Debian Codegraph @

components/kascrypt/Docs/Ciphers.html

raw
1 <html>
2 <head>
3 <title>DCPcrypt v2: Users Guide - Ciphers</title>
4 </head>
5 <body>
6 <p align="center"><font size="+2"><b>DCPcrypt Cryptographic Component Library v2</b></font><br>
7 <font size="+1">Copyright &copy; 1999-2002 David Barton<br>
8 <a href="http://www.cityinthesky.co.uk/">http://www.cityinthesky.co.uk/</a><br>
9 <a href="mailto:[email protected]">[email protected]</a></font>
10 <p><font size="+2">Ciphers - TDCP_cipher</font>
11 <p>All ciphers are inherited from the TDCP_cipher component either directly for stream ciphers (such as RC4) or via the TDCP_blockcipher component.
12 <p>The TDCP_cipher component implements key initialisation features and the basic encryption/decryption interface. Functions available are:
13 <pre>
14 property <a href="#Initialized">Initialized</a>: boolean;
15 property <a href="#Id">Id</a>: integer;
16 property <a href="#Algorithm">Algorithm</a>: string;
17 property <a href="#MaxKeySize">MaxKeySize</a>: integer;
18
19 class function <a href="#SelfTest">SelfTest</a>: boolean;
20
21 procedure <a href="#Init">Init</a>(const Key; Size: longword; InitVector: pointer);
22 procedure <a href="#InitStr">InitStr</a>(const Key: string; HashType: TDCP_hashclass);
23 procedure <a href="#Burn">Burn</a>;
24 procedure <a href="#Reset">Reset</a>;
25 procedure <a href="#Encrypt">Encrypt</a>(const Indata; var Outdata; Size: longword);
26 procedure <a href="#Decrypt">Decrypt</a>(const Indata; var Outdata; Size: longword);
27 function <a href="#EncryptStream">EncryptStream</a>(InStream, OutStream: TStream; Size: longword): longword;
28 function <a href="#DecryptStream">DecryptStream</a>(InStream, OutStream: TStream; Size: longword): longword;
29 function <a href="#EncryptString">EncryptString</a>(const Str: string): string;
30 function <a href="#DecryptString">DecryptString</a>(const Str: string): string;
31 </pre>
32 <p>Example usage:
33 <ul>
34 <li><a href="#Example1">Example 1</a> - String encryption.
35 <li><a href="#Example2">Example 2</a> - File encryption.
36 <li><a href="#Example3">Example 3</a> - General encryption.
37 </ul>
38 <hr>
39 <p><font size="+2">Function descriptions</font>
40 <p><font size="+1"><a name="Initialized">property Initialized: boolean;</a></font>
41 <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.
42 <p><font size="+1"><a name="Id">property Id: integer;</a></font>
43 <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.
44 <p><font size="+1"><a name="Algorithm">property Algorithm: string;</a></font>
45 <p>This contains the name of the algorithm implemented within the component.
46 <p><font size="+1"><a name="MaxKeySize">property MaxKeySize: integer;</a></font>
47 <p>This is the maximum size of key you can pass to the cipher (in bits!).
48 <p><font size="+1"><a name="SelfTest">class function SelfTest: boolean;</a></font>
49 <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.
50 <p><font size="+1"><a name="Init">procedure Init(const Key; Size: longword; InitVector: pointer);</a></font>
51 <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.
52 <p>Init example: use the hash of a string to initialize the cipher
53 <pre>
54 <b>procedure</b> TForm1.Button1Click(Sender: TObject);
55 <b>var</b>
56 Cipher: TDCP_rc4;
57 Hash: TDCP_sha1;
58 Digest: <b>array</b>[0..19] <b>of byte</b>; <em>// SHA-1 produces a 160bit (20byte) output</em>
59 <b>begin</b>
60 Hash:= TDCP_sha1.Create(Self);
61 Hash.Init; <em>// initialize the hash</em>
62 Hash.UpdateStr(Edit1.Text); <em>// generate a hash of Edit1.Text</em>
63 Hash.Final(Digest); <em>// save the hash in Digest</em>
64 Hash.Free;
65 Cipher:= TDCP_rc4.Create(Self);
66 Cipher.Init(Digest,Sizeof(Digest)*8,<b>nil</b>); <em>// remember size is in BITS (hence sizeof*8)</em>
67 ...
68 </pre>
69 <p><font size="+1"><a name="InitStr">procedure InitStr(const Key: string; HashType: TDCP_hashclass);</a></font>
70 <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).
71 <p>InitStr example: prompt the user for a passphrase to initialize the cipher
72 <pre>
73 <b>procedure</b> TForm1.Button1Click(Sender: TObject);
74 <b>var</b>
75 Cipher: TDCP_rc4;
76 <b>begin</b>
77 Cipher:= TDCP_rc4.Create(Self);
78 Cipher.InitStr(InputBox('Passphrase','Enter a passphrase',''),TDCP_sha1); <em>// prompt for a passphrase</em>
79 ...
80 </pre>
81 <p><font size="+1"><a name="Burn">procedure Burn;</a></font>
82 <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.
83 <p><font size="+1"><a name="Reset">procedure Reset;</a></font>
84 <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.
85 <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.
86 <p>Reset example: encrypting and decrypting
87 <pre>
88 <b>function</b> TestCipher: <b>boolean</b>;
89 <b>const</b>
90 InData: <b>array</b>[0..9] <b>of byte</b>= ($01,$23,$45,$56,$67,$78,$89,$10,$AB,$FF);
91 <b>var</b>
92 Cipher: TDCP_rc4;
93 Data: <b>array</b>[0..9] <b>of byte</b>;
94 <b>begin</b>
95 Cipher:= TDCP_rc4.Create(<b>nil</b>);
96 Cipher.InitStr('Hello World',TDCP_sha1); <em>// initialize the cipher</em>
97 Cipher.Encrypt(InData,Data,Sizeof(Data)); <em>// encrypt some known data</em>
98 Cipher.Decrypt(Data,Data,Sizeof(Data)); <em>// now decrypt it</em>
99 Cipher.Burn; <em>// clear keying information</em>
100 Cipher.Free;
101 Result:= CompareMem(@InData,@Data,Sizeof(Data)); <em>// compare input and output</em>
102 <b>end</b>;
103 </pre>
104 The above will ALWAYS result in false due to the chaining information.
105 <pre>
106 <b>function</b> TestCipher: <b>boolean</b>;
107 <b>const</b>
108 InData: <b>array</b>[0..9] <b>of byte</b>= ($01,$23,$45,$56,$67,$78,$89,$10,$AB,$FF);
109 <b>var</b>
110 Cipher: TDCP_rc4;
111 Data: <b>array</b>[0..9] <b>of byte</b>;
112 <b>begin</b>
113 Cipher:= TDCP_rc4.Create(<b>nil</b>);
114 Cipher.InitStr('Hello World',TDCP_sha1); <em>// initialize the cipher</em>
115 Cipher.Encrypt(InData,Data,Sizeof(Data)); <em>// encrypt some known data</em>
116 Cipher.Reset; <em><b>// reset chaining information</b></em>
117 Cipher.Decrypt(Data,Data,Sizeof(Data)); <em>// now decrypt it</em>
118 Cipher.Burn; <em>// clear keying information</em>
119 Cipher.Free;
120 Result:= CompareMem(@InData,@Data,Sizeof(Data)); <em>// compare input and output</em>
121 <b>end</b>;
122 </pre>
123 The above <em>should</em> always return true.
124 <p><font size="+1"><a name="Encrypt">procedure Encrypt(const Indata; var Outdata; Size: longword);</a></font>
125 <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>.
126 <p><font size="+1"><a name="Decrypt">procedure Decrypt(const Indata; var Outdata; Size: longword);</a></font>
127 <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>.
128 <p><font size="+1"><a name="EncryptStream">function EncryptStream(InStream, OutStream: TStream; Size: longword): longword;</a></font>
129 <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>.
130 <p><font size="+1"><a name="DecryptStream">function DecryptStream(InStream, OutStream: TStream; Size: longword): longword;</a></font>
131 <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>.
132 <p><font size="+1"><a name="EncryptString">function EncryptString(const Str: string): string;</a></font>
133 <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.
134 <p><font size="+1"><a name="DecryptString">function DecryptString(const Str: string): string;</a></font>
135 <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.
136 <hr>
137 <p><font size="+2"><a name="Example1">Example 1: String encryption</a></font>
138 <p>This example shows how you can encrypt the contents of a TMemo and leave the contents printable.
139 <pre>
140 <b>procedure</b> TForm1.btnEncryptClick(Sender: TObject);
141 <b>var</b>
142 i: <b>integer</b>;
143 Cipher: TDCP_rc4;
144 KeyStr: string;
145 <b>begin</b>
146 KeyStr:= '';
147 <b>if</b> InputQuery('Passphrase','Enter passphrase',KeyStr) <b>then</b> <em>// get the passphrase</em>
148 <b>begin</b>
149 Cipher:= TDCP_rc4.Create(Self);
150 Cipher.InitStr(KeyStr,TDCP_sha1); <em>// initialize the cipher with a hash of the passphrase</em>
151 <b>for</b> i:= 0 <b>to</b> Memo1.Lines.Count-1 <b>do</b> <em>// encrypt the contents of the memo</em>
152 Memo1.Lines[i]:= Cipher.EncryptString(Memo1.Lines[i]);
153 Cipher.Burn;
154 Cipher.Free;
155 <b>end</b>;
156 <b>end</b>;
157
158 <b>procedure</b> TForm1.btnDecryptClick(Sender: TObject);
159 <b>var</b>
160 i: <b>integer</b>;
161 Cipher: TDCP_rc4;
162 KeyStr: string;
163 <b>begin</b>
164 KeyStr:= '';
165 <b>if</b> InputQuery('Passphrase','Enter passphrase',KeyStr) <b>then</b> <em>// get the passphrase</em>
166 <b>begin</b>
167 Cipher:= TDCP_rc4.Create(Self);
168 Cipher.InitStr(KeyStr,TDCP_sha1); <em>// initialize the cipher with a hash of the passphrase</em>
169 <b>for</b> i:= 0 <b>to</b> Memo1.Lines.Count-1 <b>do</b> <em>// decrypt the contents of the memo</em>
170 Memo1.Lines[i]:= Cipher.DecryptString(Memo1.Lines[i]);
171 Cipher.Burn;
172 Cipher.Free;
173 <b>end</b>;
174 <b>end</b>;
175 </pre>
176 <hr>
177 <p><font size="+2"><a name="Example2">Example 2: File encryption</a></font>
178 <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.
179 <pre>
180 <b>procedure</b> TForm1.btnEncryptClick(Sender: TObject);
181 <b>var</b>
182 Cipher: TDCP_rc4;
183 KeyStr: string;
184 Source, Dest: TFileStream;
185 <b>begin</b>
186 KeyStr:= '';
187 <b>if</b> InputQuery('Passphrase','Enter passphrase',KeyStr) <b>then</b> <em>// get the passphrase</em>
188 <b>begin</b>
189 <b>try</b>
190 Source:= TFileStream.Create(boxInputFile.Text,fmOpenRead);
191 Dest:= TFileStream.Create(boxOutputFile.Text,fmCreate);
192 Cipher:= TDCP_rc4.Create(Self);
193 Cipher.InitStr(KeyStr,TDCP_sha1); <em>// initialize the cipher with a hash of the passphrase</em>
194 Cipher.EncryptStream(Source,Dest,Source.Size); <em>// encrypt the contents of the file</em>
195 Cipher.Burn;
196 Cipher.Free;
197 Dest.Free;
198 Source.Free;
199 MessageDlg('File encrypted',mtInformation,[mbOK],0);
200 <b>except</b>
201 MessageDlg('File IO error',mtError,[mbOK],0);
202 <b>end</b>;
203 <b>end</b>;
204 <b>end</b>;
205
206 <b>procedure</b> TForm1.btnDecryptClick(Sender: TObject);
207 <b>var</b>
208 Cipher: TDCP_rc4;
209 KeyStr: string;
210 Source, Dest: TFileStream;
211 <b>begin</b>
212 KeyStr:= '';
213 <b>if</b> InputQuery('Passphrase','Enter passphrase',KeyStr) <b>then</b> <em>// get the passphrase</em>
214 <b>begin</b>
215 <b>try</b>
216 Source:= TFileStream.Create(boxInputFile.Text,fmOpenRead);
217 Dest:= TFileStream.Create(boxOutputFile.Text,fmCreate);
218 Cipher:= TDCP_rc4.Create(Self);
219 Cipher.InitStr(KeyStr,TDCP_sha1); <em>// initialize the cipher with a hash of the passphrase</em>
220 Cipher.DecryptStream(Source,Dest,Source.Size); <em>// decrypt the contents of the file</em>
221 Cipher.Burn;
222 Cipher.Free;
223 Dest.Free;
224 Source.Free;
225 MessageDlg('File decrypted',mtInformation,[mbOK],0);
226 <b>except</b>
227 MessageDlg('File IO error',mtError,[mbOK],0);
228 <b>end</b>;
229 <b>end</b>;
230 <b>end</b>;
231 </pre>
232 <hr>
233 <p><font size="+2"><a name="Example3">Example 3: General encryption</a></font>
234 <p>This hypothetical example shows how you might encrypt a packet of information before transmission across a network.
235 <pre>
236 <b>type</b>
237 TSomePacket= <b>record</b>
238 Date: <b>double</b>;
239 ToUserID: <b>integer</b>;
240 FromUserID: <b>integer</b>;
241 MsgLen: <b>integer</b>;
242 Msg: string;
243 <b>end</b>;
244
245 <b>procedure</b> EncryptPacket(Cipher: TDCP_cipher; <b>var</b> Packet: TSomePacket);
246 <em>// encrypt the information packet with the cipher
247 // if the cipher isn't initialized then prompt for passphrase</em>
248 <b>begin</b>
249 <b>if</b> Cipher= <b>nil then</b>
250 <b>raise</b> Exception.Create('Cipher hasn''t been created!')
251 <b>else
252 begin</b>
253 <b>if not</b> Cipher.Initialized <b>then</b> <em>// check the cipher has been initialized</em>
254 Cipher.InitStr(InputBox('Passphrase','Enter passphrase',''),TDCP_sha1);
255 <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>
256 TDCP_blockcipher(Cipher).CipherMode:= cmCFB8bit;
257 <em>// encrypt the record part by part, could do this in one go if it was a packed record</em>
258 Cipher.Encrypt(Packet.Date,Packet.Date,Sizeof(Packet.Date));
259 Cipher.Encrypt(Packet.ToUserID,Packet.ToUserID,Sizeof(Packet.ToUserID));
260 Cipher.Encrypt(Packet.FromUserID,Packet.FromUserID,Sizeof(Packet.FromUserID));
261 Cipher.Encrypt(Packet.MsgLen,Packet.MsgLen,Sizeof(Packet.MsgLen));
262 Cipher.Encrypt(Packet.Msg[1],Packet.Msg[1],Length(Packet.Msg)); <em>// slightly different for strings</em>
263 <em>// don't bother resetting the cipher, instead keep the chaining information</em>
264 <b>end</b>;
265 <b>end</b>;
266 </pre>
267 <p>&nbsp;
268 <p><a href="Index.html">Index</a>, <a href="BlockCiphers.html">Block Ciphers</a>, <a href="Hashes.html">Hashes</a>
269 <p>&nbsp;
270 <p><em>DCPcrypt is copyrighted &copy; 1999-2002 David Barton.<br>
271 All trademarks are property of their respective owners.</em>
272 </body>
273 </html>