Sign in Join
Wiki copytemporary

DCPcrypt: what changed

Revision 1 to 2. 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
16-<figure>
17-<img src="Dcp1.png" title="Dcp1.png" />
18-<figcaption>Dcp1.png</figcaption>
19-</figure>
16+![](/wiki-images/b/be/Dcp1.png)
2017
21-<figure>
22-<img src="Dcp2.png" title="Dcp2.png" />
23-<figcaption>Dcp2.png</figcaption>
24-</figure>
18+![](/wiki-images/7/74/Dcp2.png)
2519
2620### Author(s)
2721
2822- David Barton, [home page](http://www.cityinthesky.co.uk)
2923
3024Contributors:
3125
3226- Port to Lazarus by [barko](http://wiki.freepascal.org/User:Barko) - 2006
3327- [Graeme Geldenhuys](http://geldenhuys.co.uk) - 2009
3428
3529### License
3630
3731The MIT License
3832
3933Permission 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:
4034
4135The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
4236
4337THE 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.
4438
4539### Download
4640
4741Since March 2010, DCPCrypt source code is hosted as part of the [Lazarus-CCR project](http://sourceforge.net/projects/lazarus-ccr/) on SourceForge.net.
4842
4943DCPCrypt 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.
5044
5145`git clone `[`git://lazarus-ccr.git.sourceforge.net/gitroot/lazarus-ccr/dcpcrypt`](git://lazarus-ccr.git.sourceforge.net/gitroot/lazarus-ccr/dcpcrypt)
5246
5347`git clone http://git.code.sf.net/p/lazarus-ccr/dcpcrypt lazarus-ccr-dcpcrypt`
5448
5549After that you can stay updated by simply running
5650
5751`git pull`
5852
5953### Installation
6054
6155Since the v2.0.4 release, DCPCrypt is split into two packages:
6256
6357dcpcrypt.lpk
6458This 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).
6559
6660dcpcrypt_laz.lpk
6761This is the design time package to register the various components with the Lazarus IDE component palette.
6862
6963To use DCPCrypt with Lazarus IDE for the first time:
7064
7165- In 'Package \> Open Package File', open dcpcrypt.lpk and click **Compile**
7266- 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.
7367- Restart Lazarus IDE and you are ready to use DCPCrypt.
7468
7569### Usage
7670
7771Please 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.
7872
7973- Ciphers - the basic building block of DCPcrypt, the TDCP_cipher component.
8074- Block Ciphers - the base of all block ciphers, the TDCP_blockcipher component.
8175- Hashes - the base of all hash algorithms, the TDCP_hash component.
8276
8377See included documentation.
8478
8579#### Encrypting using plain key
8680
8781Normally, for encryption, dpcpcrypt expects keys that are hashed when they are supplied through *.InitStr*.
8882
8983You 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:
9084
9185``` pascal
9286var s:string;
9387begin
9488 s:='1234567890ABCDEF1234567890ABCDEF';
9589 DCP_3des1.Init(s[1],length(s)*8,nil);
9690```
9791
9892Source: [forum thread](http://lazarus.freepascal.org/index.php/topic,18108.msg101683.html#msg101683).
9993
10094#### Lazarus example
10195
10296``` pascal
10397unit Unit1;
10498
10599{$mode objfpc}{$H+}
106100
107101interface
108102
109103uses
110104 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
111105 DCPrc4, DCPsha1;
112106
113107type
114108
115109 { TForm1 }
116110
117111 TForm1 = class(TForm)
118112 Button1: TButton;
119113 Button2: TButton;
120114 DCP_rc4_1: TDCP_rc4;
121115 DCP_sha1_1: TDCP_sha1;
122116 Edit1: TEdit;
123117 Edit2: TEdit;
124118 Label1: TLabel;
125119 Label2: TLabel;
126120 procedure Button1Click(Sender: TObject);
127121 procedure Button2Click(Sender: TObject);
128122 procedure FormCreate(Sender: TObject);
129123 private
130124 { private declarations }
131125 public
132126 { public declarations }
133127 end;
134128
135129var
136130 Form1: TForm1;
137131 KeyStr: string;
138132
139133implementation
140134
141135{$R *.lfm}
142136
143137{ TForm1 }
144138
145139procedure TForm1.Button1Click(Sender: TObject);
146140var
147141 Cipher: TDCP_rc4;
148142 Source, Dest: TFileStream;
149143 begin
150144 //if InputQuery('Passphrase','Enter passphrase',KeyStr) then // get the passphrase
151145 begin
152146 try
153147 Source:= TFileStream.Create(Edit1.Text,fmOpenRead); // Edit1.Text, PATH SOURCE OF FILE
154148 Dest:= TFileStream.Create(Edit2.Text,fmCreate); // Edit2.Text, PATH DESTINATION OF FILE
155149 Cipher:= TDCP_rc4.Create(Self);
156150 Cipher.InitStr(KeyStr,TDCP_sha1); // initialize the cipher with a hash of the passphrase
157151 Cipher.EncryptStream(Source,Dest,Source.Size); // encrypt the contents of the file
158152 Cipher.Burn;
159153 Cipher.Free;
160154 Dest.Free;
161155 Source.Free;
162156 MessageDlg('File encrypted',mtInformation,[mbOK],0);
163157 except
164158 MessageDlg('File IO error',mtError,[mbOK],0);
165159 end;
166160 end;
167161 end;
168162
169163procedure TForm1.Button2Click(Sender: TObject);
170164var
171165 Cipher: TDCP_rc4;
172166 //KeyStr: string;
173167 Source, Dest: TFileStream;
174168 begin
175169 //if InputQuery('Passphrase','Enter passphrase',KeyStr) then // get the passphrase
176170 begin
177171 try
178172 Source:= TFileStream.Create(Edit1.Text,fmOpenRead); // Edit1.Text, PATH SOURCE OF FILE
179173 Dest:= TFileStream.Create(Edit2.Text,fmCreate); // Edit2.Text, PATH DESTINATION OF FILE
180174 Cipher:= TDCP_rc4.Create(Self);
181175 Cipher.InitStr(KeyStr,TDCP_sha1); // initialize the cipher with a hash of the passphrase
182176 Cipher.DecryptStream(Source,Dest,Source.Size); // decrypt the contents of the file
183177 Cipher.Burn;
184178 Cipher.Free;
185179 Dest.Free;
186180 Source.Free;
187181 MessageDlg('File decrypted',mtInformation,[mbOK],0);
188182 except
189183 MessageDlg('File IO error',mtError,[mbOK],0);
190184 end;
191185 end;
192186 end;
193187
194188procedure TForm1.FormCreate(Sender: TObject);
195189begin
196190 KeyStr := 'casa777'; // my password
197191end;
198192
199193end.
200194```
201195
202-<figure>
203-<img src="dcrypt_form.jpg" title="dcrypt_form.jpg" />
204-<figcaption>dcrypt_form.jpg</figcaption>
205-</figure>
196+![](/wiki-images/a/ad/Dcrypt_form.jpg)
206197
207198### Bugs
208199
209200If 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:
210201
211202``` pascal
212203program ctst;
213204{$mode objfpc}{$H+}
214205uses
215206 {$IFDEF UNIX}{$IFDEF UseCThreads}
216207 cthreads,
217208 {$ENDIF}{$ENDIF}
218209 Sysutils, DCPrijndael;
219210const
220211 InData: array[0..15] of byte =
221212 ($32,$43,$f6,$a8,$88,$5a,$30,$8d,$31,$31,$98,$a2,$e0,$37,$07,$34);
222213 Key1: array[0..19] of byte =
223214 ($2b,$7e,$15,$16,$28,$ae,$d2,$a6,$ab,$f7,$15,$88,$09,$cf,$4f,$3c,$76,$2e,$71,$60);
224215 Key2: array[0..31] of byte =
225216 ($2b,$7e,$15,$16,$28,$ae,$d2,$a6,$ab,$f7,$15,$88,$09,$cf,$4f,$3c,$76,$2e,$71,$60,
226217 $f3,$8b,$4d,$a5,$6a,$78,$4d,$90,$45,$19,$0c,$fe);
227218 OutData1: array[0..15] of byte =
228219 ($23,$1d,$84,$46,$39,$b3,$1b,$41,$22,$11,$cf,$e9,$37,$12,$b8,$80);
229220 OutData2: array[0..15] of byte =
230221 ($1a,$6e,$6c,$2c,$66,$2e,$7d,$a6,$50,$1f,$fb,$62,$bc,$9e,$93,$f3);
231222var
232223 Cipher: TDCP_rijndael;
233224 Block: array[0..15] of byte;
234225begin
235226// lengths: block = 16 bytes, key = 20 bytes
236227// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160
237228// input = 3243f6a8885a308d313198a2e0370734
238229// encrypt = 231d844639b31b412211cfe93712b880
239230// decrypt = 3243f6a8885a308d313198a2e0370734
240231 Cipher := TDCP_rijndael.Create(nil);
241232 Cipher.Init(Key1, Sizeof(Key1) * 8, nil);
242233 Cipher.EncryptECB(InData, Block);
243234 writeln('128-bit block & 160-bit key results:');
244235 writeln(boolean(CompareMem(@Block, @OutData1, 16)));
245236 Cipher.DecryptECB(Block, Block);
246237 writeln(boolean(CompareMem(@Block, @InData, 16)));
247238 Cipher.Free;
248239// lengths: block = 16 bytes, key = 32 bytes
249240// key = 2b7e151628aed2a6abf7158809cf4f3c762e7160f38b4da56a784d9045190cfe
250241// input = 3243f6a8885a308d313198a2e0370734
251242// encrypt = 1a6e6c2c662e7da6501ffb62bc9e93f3
252243// decrypt = 3243f6a8885a308d313198a2e0370734
253244 Cipher := TDCP_rijndael.Create(nil);
254245 Cipher.Init(Key2, Sizeof(Key2) * 8, nil);
255246 Cipher.EncryptECB(InData, Block);
256247 writeln('128-bit block & 256-bit key results:');
257248 writeln(boolean(CompareMem(@Block, @OutData2, 16)));
258249 Cipher.DecryptECB(Block, Block);
259250 writeln(boolean(CompareMem(@Block, @InData, 16)));
260251 Cipher.Free;
261252end.
262253```
263254
264255The output that freepascal 2.2.0 produces is:
265256
266257` 128-bit block & 160-bit key results:`\
267258` FALSE`\
268259` TRUE`\
269260` 128-bit block & 256-bit key results:`\
270261` TRUE`\
271262` TRUE`
272263
273264What 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.
274265
275266### External links
276267
277268- [HMAC/PBKDF1/PBKDF2](http://keit.co/p/dcpcrypt-hmac-rfc2104/) implementation based on DCPcrypt.
278269
279270---
280271*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).*
281272Category: Components
282273Category: Lazarus-CCR

Back to history