Sign in Join
Wiki copytemporary

DCPcrypt: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11### About
22
33DCPcrypt is a collection of cryptographic components for the Lazarus.
44
55The 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.
66
77Since v2.0.4 it compiles with FPC 2.4.1 and has been tested under 32-bit & 64-bit Linux and 32-bit Windows.
88
99- Status: Production/Stable
1010- Latest version: 2.0.4.1
1111
1212Notice that DCPcrypt does not have an (un)official maintainer anymore.
1313
1414Screenshots:
1515
1616![](/wiki-images/b/be/Dcp1.png)
1717
1818![](/wiki-images/7/74/Dcp2.png)
1919
2020### Author(s)
2121
2222- David Barton, [home page](http://www.cityinthesky.co.uk)
2323
2424Contributors:
2525
2626- Port to Lazarus by [barko](http://wiki.freepascal.org/User:Barko) - 2006
2727- [Graeme Geldenhuys](http://geldenhuys.co.uk) - 2009
2828
2929### License
3030
3131The MIT License
3232
3333Permission 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:
3434
3535The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
3636
3737THE 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.
3838
3939### Download
4040
4141Since March 2010, DCPCrypt source code is hosted as part of the [Lazarus-CCR project](http://sourceforge.net/projects/lazarus-ccr/) on SourceForge.net.
4242
4343DCPCrypt is also hosted in a [Git](http://git-scm.com) repository. To download the latest source code from the repository you need to run the following command.
4444
45-`git clone `[`git://lazarus-ccr.git.sourceforge.net/gitroot/lazarus-ccr/dcpcrypt`](git://lazarus-ccr.git.sourceforge.net/gitroot/lazarus-ccr/dcpcrypt)
45+```text
46+git clone git://lazarus-ccr.git.sourceforge.net/gitroot/lazarus-ccr/dcpcrypt
47+```
4648
47-`git clone http://git.code.sf.net/p/lazarus-ccr/dcpcrypt lazarus-ccr-dcpcrypt`
49+```text
50+git clone http://git.code.sf.net/p/lazarus-ccr/dcpcrypt lazarus-ccr-dcpcrypt
51+```
4852
4953After that you can stay updated by simply running
5054
51-`git pull`
55+```text
56+git pull
57+```
5258
5359### Installation
5460
5561Since the v2.0.4 release, DCPCrypt is split into two packages:
5662
5763dcpcrypt.lpk
5864This is a run-time only package and only contains the core non-gui code. You can use the package for console applications or other non-LCL projects (eg: fpGUI or MSEgui based projects).
5965
6066dcpcrypt_laz.lpk
6167This is the design time package to register the various components with the Lazarus IDE component palette.
6268
6369To use DCPCrypt with Lazarus IDE for the first time:
6470
6571- In 'Package \> Open Package File', open dcpcrypt.lpk and click **Compile**
6672- If you want to register the DCPCrypt components with the Lazarus IDE component palette, open the dcpcrypt_laz.lpk and click **Compile** and then **Install**. Lazarus IDE will now rebuild itself.
6773- Restart Lazarus IDE and you are ready to use DCPCrypt.
6874
6975### Usage
7076
7177Please 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.
7278
7379- Ciphers - the basic building block of DCPcrypt, the TDCP_cipher component.
7480- Block Ciphers - the base of all block ciphers, the TDCP_blockcipher component.
7581- Hashes - the base of all hash algorithms, the TDCP_hash component.
7682
7783See included documentation.
7884
7985#### Encrypting using plain key
8086
8187Normally, for encryption, dpcpcrypt expects keys that are hashed when they are supplied through *.InitStr*.
8288
8389You can pass a plain string key by using *[.Init](http://is.muni.cz/th/51468/fi_b/priloha/DCPcrypt/Docs/Ciphers.html#Init)*, e.g. like this:
8490
8591``` pascal
8692var s:string;
8793begin
8894 s:='1234567890ABCDEF1234567890ABCDEF';
8995 DCP_3des1.Init(s[1],length(s)*8,nil);
9096```
9197
9298Source: [forum thread](http://lazarus.freepascal.org/index.php/topic,18108.msg101683.html#msg101683).
9399
94100#### Lazarus example
95101
96102``` pascal
97103unit Unit1;
98104
99105{$mode objfpc}{$H+}
100106
101107interface
102108
103109uses
104110 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
105111 DCPrc4, DCPsha1;
106112
107113type
108114
109115 { TForm1 }
110116
111117 TForm1 = class(TForm)
112118 Button1: TButton;
113119 Button2: TButton;
114120 DCP_rc4_1: TDCP_rc4;
115121 DCP_sha1_1: TDCP_sha1;
116122 Edit1: TEdit;
117123 Edit2: TEdit;
118124 Label1: TLabel;
119125 Label2: TLabel;
120126 procedure Button1Click(Sender: TObject);
121127 procedure Button2Click(Sender: TObject);
122128 procedure FormCreate(Sender: TObject);
123129 private
124130 { private declarations }
125131 public
126132 { public declarations }
127133 end;
128134
129135var
130136 Form1: TForm1;
131137 KeyStr: string;
132138
133139implementation
134140
135141{$R *.lfm}
136142
137143{ TForm1 }
138144
139145procedure TForm1.Button1Click(Sender: TObject);
140146var
141147 Cipher: TDCP_rc4;
142148 Source, Dest: TFileStream;
143149 begin
144150 //if InputQuery('Passphrase','Enter passphrase',KeyStr) then // get the passphrase
145151 begin
146152 try
147153 Source:= TFileStream.Create(Edit1.Text,fmOpenRead); // Edit1.Text, PATH SOURCE OF FILE
148154 Dest:= TFileStream.Create(Edit2.Text,fmCreate); // Edit2.Text, PATH DESTINATION OF FILE
149155 Cipher:= TDCP_rc4.Create(Self);
150156 Cipher.InitStr(KeyStr,TDCP_sha1); // initialize the cipher with a hash of the passphrase
151157 Cipher.EncryptStream(Source,Dest,Source.Size); // encrypt the contents of the file
152158 Cipher.Burn;
153159 Cipher.Free;
154160 Dest.Free;
155161 Source.Free;
156162 MessageDlg('File encrypted',mtInformation,[mbOK],0);
157163 except
158164 MessageDlg('File IO error',mtError,[mbOK],0);
159165 end;
160166 end;
161167 end;
162168
163169procedure TForm1.Button2Click(Sender: TObject);
164170var
165171 Cipher: TDCP_rc4;
166172 //KeyStr: string;
167173 Source, Dest: TFileStream;
168174 begin
169175 //if InputQuery('Passphrase','Enter passphrase',KeyStr) then // get the passphrase
170176 begin
171177 try
172178 Source:= TFileStream.Create(Edit1.Text,fmOpenRead); // Edit1.Text, PATH SOURCE OF FILE
173179 Dest:= TFileStream.Create(Edit2.Text,fmCreate); // Edit2.Text, PATH DESTINATION OF FILE
174180 Cipher:= TDCP_rc4.Create(Self);
175181 Cipher.InitStr(KeyStr,TDCP_sha1); // initialize the cipher with a hash of the passphrase
176182 Cipher.DecryptStream(Source,Dest,Source.Size); // decrypt the contents of the file
177183 Cipher.Burn;
178184 Cipher.Free;
179185 Dest.Free;
180186 Source.Free;
181187 MessageDlg('File decrypted',mtInformation,[mbOK],0);
182188 except
183189 MessageDlg('File IO error',mtError,[mbOK],0);
184190 end;
185191 end;
186192 end;
187193
188194procedure TForm1.FormCreate(Sender: TObject);
189195begin
190196 KeyStr := 'casa777'; // my password
191197end;
192198
193199end.
194200```
195201
196202![](/wiki-images/a/ad/Dcrypt_form.jpg)
197203
198204### Bugs
199205
200206If you are using DCPrijndael, be advised that key sizes that are not in the AES standard are known to produce key schedules that are **not** inter-operable with other Rijndael implementations. Few implementations provide the non-standard key (or block) sizes. [Brian Gladman](https://web.archive.org/web/20090201173123/http://fp.gladman.plus.com/cryptography_technology/rijndael/) and [Jeffrey Clement](https://web.archive.org/web/20110826031605/http://jclement.ca/software/pyrijndael/) provide implementations (C and Python) that are known to be incompatible with DCPrijndael for non-standard key sizes. Test program provided below with comments showing the expect results:
201207
202208``` pascal
203209program ctst;
204210{$mode objfpc}{$H+}
205211uses
206212 {$IFDEF UNIX}{$IFDEF UseCThreads}
207213 cthreads,
208214 {$ENDIF}{$ENDIF}
209215 Sysutils, DCPrijndael;
210216const
211217 InData: array[0..15] of byte =
212218 ($32,$43,$f6,$a8,$88,$5a,$30,$8d,$31,$31,$98,$a2,$e0,$37,$07,$34);
213219 Key1: array[0..19] of byte =
214220 ($2b,$7e,$15,$16,$28,$ae,$d2,$a6,$ab,$f7,$15,$88,$09,$cf,$4f,$3c,$76,$2e,$71,$60);
215221 Key2: array[0..31] of byte =
216222 ($2b,$7e,$15,$16,$28,$ae,$d2,$a6,$ab,$f7,$15,$88,$09,$cf,$4f,$3c,$76,$2e,$71,$60,
217223 $f3,$8b,$4d,$a5,$6a,$78,$4d,$90,$45,$19,$0c,$fe);
218224 OutData1: array[0..15] of byte =
219225 ($23,$1d,$84,$46,$39,$b3,$1b,$41,$22,$11,$cf,$e9,$37,$12,$b8,$80);
220226 OutData2: array[0..15] of byte =
221227 ($1a,$6e,$6c,$2c,$66,$2e,$7d,$a6,$50,$1f,$fb,$62,$bc,$9e,$93,$f3);
222228var
223229 Cipher: TDCP_rijndael;
224230 Block: array[0..15] of byte;
225231begin
226232// lengths: block = 16 bytes, key = 20 bytes
227233// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160
228234// input = 3243f6a8885a308d313198a2e0370734
229235// encrypt = 231d844639b31b412211cfe93712b880
230236// decrypt = 3243f6a8885a308d313198a2e0370734
231237 Cipher := TDCP_rijndael.Create(nil);
232238 Cipher.Init(Key1, Sizeof(Key1) * 8, nil);
233239 Cipher.EncryptECB(InData, Block);
234240 writeln('128-bit block & 160-bit key results:');
235241 writeln(boolean(CompareMem(@Block, @OutData1, 16)));
236242 Cipher.DecryptECB(Block, Block);
237243 writeln(boolean(CompareMem(@Block, @InData, 16)));
238244 Cipher.Free;
239245// lengths: block = 16 bytes, key = 32 bytes
240246// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d9045190cfe
241247// input = 3243f6a8885a308d313198a2e0370734
242248// encrypt = 1a6e6c2c662e7da6501ffb62bc9e93f3
243249// decrypt = 3243f6a8885a308d313198a2e0370734
244250 Cipher := TDCP_rijndael.Create(nil);
245251 Cipher.Init(Key2, Sizeof(Key2) * 8, nil);
246252 Cipher.EncryptECB(InData, Block);
247253 writeln('128-bit block & 256-bit key results:');
248254 writeln(boolean(CompareMem(@Block, @OutData2, 16)));
249255 Cipher.DecryptECB(Block, Block);
250256 writeln(boolean(CompareMem(@Block, @InData, 16)));
251257 Cipher.Free;
252258end.
253259```
254260
255261The output that freepascal 2.2.0 produces is:
256262
257-` 128-bit block & 160-bit key results:`\
258-` FALSE`\
259-` TRUE`\
260-` 128-bit block & 256-bit key results:`\
261-` TRUE`\
262-` TRUE`
263+```text
264+ 128-bit block & 160-bit key results:
265+ FALSE
266+ TRUE
267+ 128-bit block & 256-bit key results:
268+ TRUE
269+ TRUE
270+```
263271
264272What this meas is that using 160-bit keys you can only decrypt the output with DCPrijndael, but it won't work with other implementations. On the other hand, 256-bit keys are interoperable. The 160-bit case is important because SHA-1 outputs 160-bit hashes, which makes it a convenient way to generate a key from a password, so it's used by some (hardware) systems.
265273
266274### External links
267275
268276- [HMAC/PBKDF1/PBKDF2](http://keit.co/p/dcpcrypt-hmac-rfc2104/) implementation based on DCPcrypt.
269277
270278---
271279*From the Free Pascal wiki, page "DCPcrypt", as it stood on 2022-01-13 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
272280Category: Components
273281Category: Lazarus-CCR

Back to history