All Delphi

Everything You Want To Know From VCL, Tips To Snippet All Everything About Delphi And CodeGear Related

Encryption, cypher and hashes(1)

24th June 2008

Hello, www.alldelphi.com readers, today i want to talk about Encryption, Cipher and Hashes after googling for some times i found some article that interesting enough that i want to share to you all alldelphi.com readers so here it go.

In this article we are going to cover how to install DCPcrypt Cryptographic Component Library (open source), how to call methods for encrypting and decrypting strings, and finally we will take a look at the advanced topic of encrypting files. Our examples relate to applications written in Delphi5 or similar. For our examples we assume you already have a basic understanding of Delphi but only a limited understanding of encryption components and techniques.

Encryption is becoming ever more popular in today’s applications. Developers must have a good understanding of the methods used to secure their applications including any sensitive data. As most developers will tell you, Delphi straight out of the box provides the developer with very little encryption components or classes. Instead developers tend to opt for 3rd party components which are tailored to the exact needs. In this article we are going to use an excellent Open Source library called ‘DCPcrypt Cryptographic Component Library‘.

The library contains various Ciphers and Hashes, which are further sub divided into the more common names of BlowFish, cat128, DES, SHA and many more. In this article we are going to look closely at TDCP_cast256 (cast256 cipher) which has a block size of 128 bits and a max key size of 256 bits, and also DCP_sha1 (sha1 hash) which has a digest size of 160 bits. Don’t worry too much about these technical details at this stage. These two classes provide the ideal encryption architecture to use with any Delphi application. One other point before we continue, the encryption library can also be used in both C++ Builder and Kylix.

Right, lets take a look at how to install the library and get started with some simple encryption. The library comes with several packages which are ‘ready to go’. Its easy to install the package, you just need to double click the package which meets your version of Delphi, in our example: DCPdelphi5.dpk. Double clicking the package opens Delphi and the standard install package screen appears. Compile the package if you so wish but clicking install directly will do the trick. The next time you open Delphi you will notice two new tabs; DCPciphers and DCPhashes. The TDCP_cast256 class is found under the DCPciphers tab and DCP_sha1 under the DCPhashes tab.

Now we have installed the library lets get to work and start encrypting some strings. Firstly, before we can use any of the encryption methods we must set a reference in our Delphi class to the encryption classes we want to use:

Uses
  DCPCAST256, DCPSHA1

Now we have a reference to the classes lets take a look at a sample:

function TForm1.EncryptThis(aString : string) : string;
var
   Cipher: TDCP_cast256;
   KeyStr: string;
begin
   KeyStr:= 'AcceleratedKeyString';
   Cipher:= TDCP_cast256.Create(Self);
   Cipher.InitStr(KeyStr,TDCP_sha1);
   result := Cipher.EncryptString(aString);
   Cipher.Burn;
   Cipher.Free;
end;

That probably seems a bit daunting but its incredibly simple. The idea of the function is to provide a compact single call to encrypt any string we want. The string we want to encrypt is passed as the only parameter to our EncryptThis function. The result returned will be the encrypted string, simple!

This library is covered by the Open Source license. See the readme.txt file for details
Download DCPcrypt Cryptographic Component library HERE

To Page 2

Bookmark and Share
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati

6 Responses to “Encryption, cypher and hashes(1)”

  1. Phil Read Says:

    Hi, I’ve installed the ‘DCPcrypt Cryptographic Component Library’ in Builder C++ 2009, the components look fine.

    But I’m haivng trouble converting the really simple delphi example to C++ just on one line:

    Cipher.InitStr(KeyStr,TDCP_sha1);

    It says Cipher.InitStr expects a TMetaClass* where ‘TDCP_sha1′ us shown in the above line, I’vew tried everything and I can’t work out what to pass in there for c++ any ideas?

    Cheers! ;)

    P.S – Simple delphi example below:

    procedure TForm1.btnDecryptClick(Sender: TObject);
    var
    i: integer;
    Cipher: TDCP_rc4;
    KeyStr: string;
    begin
    KeyStr:= ”;
    if InputQuery(’Passphrase’,'Enter passphrase’,KeyStr) then // get the passphrase
    begin
    Cipher:= TDCP_rc4.Create(Self);
    Cipher.InitStr(KeyStr,TDCP_sha1); // initialize the cipher with a hash of the passphrase
    for i:= 0 to Memo1.Lines.Count-1 do // decrypt the contents of the memo
    Memo1.Lines[i]:= Cipher.DecryptString(Memo1.Lines[i]);
    Cipher.Burn;
    Cipher.Free;
    end;
    end;

  2. admin Says:

    hi Phil,

    did the unit library is already included properly at the uses ?
    as when you do the “chiper.initstr(KeyStr,TDCP_sha1)”, you use TDCP_Sha1 component and for that you
    need to include the DCPsha1 unit on the library .

  3. Phil Read Says:

    Hi,

    Here’s the code snippet, I have the unit :

    #include “dcpsha1.hpp”

    .
    .
    .

    int i;
    TDCP_rc4 *Cipher;
    String KeyStr;

    Cipher->InitStr(KeyStr,TDCP_sha1); // this causes ERROR: Improper use of typedef ‘TDCP_sha1′

  4. admin Says:

    this is how i do it in delphi

    unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls, -> DCPcrypt2, DCPrc4, DCPsha1 <– I Add This then all work;

    type
    TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    private
    { Private declarations }
    public
    { Public declarations }
    end;

    var
    Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.Button1Click(Sender: TObject);
    var
    i: integer;
    Cipher: TDCP_rc4;
    KeyStr: string;
    begin
    KeyStr:= ”;
    if InputQuery(’Passphrase’,'Enter passphrase’,KeyStr) then // get the passphrase
    begin
    Cipher:= TDCP_rc4.Create(Self);
    Cipher.InitStr(KeyStr,TDCP_sha1); // initialize the cipher with a hash of the passphrase
    for i:= 0 to Memo1.Lines.Count-1 do // decrypt the contents of the memo
    Memo1.Lines[i]:= Cipher.DecryptString(Memo1.Lines[i]);
    Cipher.Burn;
    Cipher.Free;
    end;
    end;

  5. Phil Read Says:

    Thanks for your help… Yes I have already reproduced all that in C++ Builder, the problem is the line:

    Cipher.InitStr(KeyStr,TDCP_sha1);

    Which in C++ Builder would be:

    Cipher->InitStr(KeyStr,TDCP_sha1);

    But it kicks up a fuss about: Improper use of typedef ‘TDCP_sha1′ other people seem to of had this problem, but I tried their solution and it still had problems: http://www.c-plusplus.de/forum/viewtopic-var-t-is-119771.html

    Cheers ;)

  6. Phil Read Says:

    Finally got it working in C++ Builder 2009 using this:

    TDCP_rijndael *Cipher = new TDCP_rijndael (this);

    String KeyStr = KeyEdit->Text;

    String encrypted = EncryptedEdit->Text;
    String decrypted = “”;

    Cipher->InitStr (KeyStr, __classid (TDCP_sha1));

    decrypted = Cipher->DecryptString(encrypted);
    DecryptedEdit->Text = decrypted;

    Cipher->Burn();
    Cipher->Free();

    Now of course my decrpyt doesn’t work… Basically I have encrypted a string using mySQL’s AES_ENCRYPT() and am now trying to decrpyt back to plain text using the above code. I have the correct key etc, anyone have any idea? Should I be passing the encrypted message in a non-text format, like binary or something?

    Thanks muchly!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>