Re: 3DES and how to change mode and set IV.

GMX <[email protected]>
Newsgroups gmane.comp.encryption.cryptlib
Message-ID <[email protected]>
The Delphi Interface for cryptlib contains all functions needed
for this job. But the "Delphi-like" interface DelphiCryptlib.pas
with object oriented features for cryptlib does not contain
methods for the low level features of cryptlib.

But it is very simple to expand the objects and methods and
implement any "missing" feature. I give you a sample for the
low level encoding/decoding as suggested by Peter below.

The Delphi interface module DelphiCryptlib.pas is available
from my Delphi-cryptlib pages http://cryptlib.sogot.de

Wolfgang Gothier
--

Source samples:
------------------------------------------------------------------
program SampleForFernandoPerez;

{$APPTYPE CONSOLE}

uses
   SysUtils,
   DelphiCryptlib,
   cryptlib,
   LowLevelDelphiCryptlib in 'LowLevelDelphiCryptlib.pas';

var
   cryptContext: TCryptContext;
   key : AnsiString = 'My Special Key 1';
   iv : array[0..7] of byte = (8,7,6,5,4,3,2,1);
   data: array[1..16] of byte = (0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5);

begin
   try
     cryptContext := TCryptContext.Create(CRYPT_ALGO_3DES);
     with cryptContext do begin
       SetAttribute(CRYPT_CTXINFO_MODE, ord(CRYPT_MODE_CBC));
       SetAttributeString(CRYPT_CTXINFO_KEY, key);
       SetAttributeBinary(CRYPT_CTXINFO_IV, @iv, 8);
       Encrypt(@data, SizeOf(data));
     end;
     cryptContext.Destroy;

     { the decryption follows as sample }
     cryptContext := TCryptContext.Create(CRYPT_ALGO_3DES);
     cryptContext.SetAttribute(CRYPT_CTXINFO_MODE, ord(CRYPT_MODE_CBC));
     cryptContext.SetAttributeString(CRYPT_CTXINFO_KEY, key);
     cryptContext.SetAttributeBinary(CRYPT_CTXINFO_IV, @iv, 8);
     cryptContext.Decrypt(@data, SizeOf(data));
     cryptContext.Destroy;

   except
     on E:Exception do
       Writeln(E.Classname, ': ', E.Message);
   end;
end.

------------------------------------------------------------------
------------------------------------------------------------------

unit LowLevelDelphiCryptlib;

interface

uses
   DelphiCryptlib,
   cryptlib;

type
   TCryptContext = class(TCryptObject)
     constructor Create(algo: CRYPT_ALGO_TYPE);
     procedure Encrypt(data: Pointer; length: Integer);
     procedure Decrypt(data: Pointer; length: Integer);
   end;

implementation

constructor TCryptContext.Create(algo: CRYPT_ALGO_TYPE);
var
   err: Integer;
begin
   err := cryptCreateContext(CryptHandle, CRYPT_UNUSED, algo);
   if err < 0 then
     raise ECryptError.Create(err, 'cryptCreateContext');
end;

procedure TCryptContext.Encrypt(data: Pointer; length: Integer);
var
   err: Integer;
begin
   err := cryptEncrypt( CryptHandle, data, length );
   if err < 0 then
     raise ECryptError.Create(err, 'cryptEncrypt');
end;

procedure TCryptContext.Decrypt(data: Pointer; length: Integer);
var
   err: Integer;
begin
   err := cryptDecrypt( CryptHandle, data, length );
   if err < 0 then
     raise ECryptError.Create(err, 'cryptDecrypt');
end;

end.
------------------------------------------------------------------


-------- Original Message  --------
Subject: Re: [Cryptlib] 3DES and how to change mode and set IV.
From: Fernando Perez <[email protected]>
To: Peter Gutmann <[email protected]>
Date: Wed Sep 02 2009 16:46:17 GMT+0200

> Hello Peter, sorry my delay I have been testing and trying without 
> success... :-(
> 
> Perhaps the big problem is that we are doing all of this on Delphi 
> (object Pascal language) and we have not could to get this to work.
> 
> Thanks anyway...
> 
> Greetings..
> 
> 
> 
> 
> 
> Peter Gutmann escribió:
>> Fernando Perez <[email protected]> writes:
>>
>>  
>>> I need to crypt/decrypt with cryptlib, but I need to support all mode 
>>> formats
>>> (cbc, ecb, ....) and IV's.   I have read documentation, search into 
>>> but I
>>> have no luck to get info on how to change cbc-mode or set the IV.  I 
>>> need a
>>> step by step functions to execute (an esquematic view)...
>>>     
>>
>> To set the IV, see "Working with Initialisation Vectors" in the 
>> manual, or
>> "Conventional Encryption" for a complete code example.  Here's a 
>> sample, for
>> CBC mode:
>>
>>   CRYPT_CONTEXT cryptContext;
>>
>>   cryptCreateContext( &cryptContext, CRYPT_UNUSED, CRYPT_ALGO_3DES );
>>   cryptSetAttribute( cryptContext, CRYPT_CTXINFO_MODE, CRYPT_MODE_CBC );
>>   cryptSetAttributeString( cryptContext, CRYPT_CTXINFO_KEY, key, 16 );
>>   cryptSetAttributeString( cryptContext, CRYPT_CTXINFO_IV, iv, 8 );
>>   cryptEncrypt( cryptContext, data, length );
>>   cryptDestroyContext( cryptContext );
>>
>> Peter.


_______________________________________________
Cryptlib mailing list
[email protected] via Mail: [email protected]
Archive: ftp://ftp.franken.de/pub/crypt/cryptlib/archives/
http://news.gmane.org/gmane.comp.encryption.cryptlib
Posts from non-subscribed addresses are blocked to prevent spam, please
subscribe in order to post messages.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.