EldoS | Feel safer!

Software components for data protection, secure storage and transfer

symmetrical encryption

Also by EldoS: MsgConnect
Cross-platform protocol-independent communication framework for building peer-to-peer and client-server applications and middleware components.
#10482
Posted: 06/27/2009 03:23:28
by Daniel K (Standard support level)
Joined: 06/25/2009
Posts: 5

Hi,

This week I received the OpenPGPBlackbox VCL-components (with sourcecode).

Is it possible to use AES/Twofish and perform a (basic) symmetrical encryption of a string or a file? And can we set the mode to CTR and initialize the IV ourselves?

Hope to read you soon,
Daniel
#10484
Posted: 06/27/2009 03:45:33
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

Hi. Answer to all questions is positive. You should take a look at TElSymmetricCrypto/TElSymmetricCryptoFactory classes.
#10508
Posted: 07/01/2009 05:22:02
by Daniel K (Standard support level)
Joined: 06/25/2009
Posts: 5

Thanks for the answer.

For a symmetrical encryption/decryption, there seem to be 3 possibilities:

1) TElMessageEncryptor/TElMessageDecryptor
2) TElSymmetricCrypto
3) PGPWriter with password-based encryption

Is there some sample sourcecode available for the 3 methods?

Hope to read you soon,
Daniel
#10509
Posted: 07/01/2009 05:31:11
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

In your example you are calling Encrypt() twice.

And, you should note, that ElMessageEncryptor uses CMS (Cryptography Messages Syntax) for message encryption, which means that it will add some additional payload to resulting file, according to CMS standart (RFC 3852).
The same about TElPGPWriter (this uses OpenPGP specification, RFC 4880).

And only TElSymmetricCrypto performs raw encryption and decryption without additional payload (except data padding, when using CBC/ECB cipher modes).
#10510
Posted: 07/01/2009 05:33:28
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

Sample code is available in our demos, you should find them in installation directory (.\Samples\Delphi).
#10511
Posted: 07/01/2009 05:52:12
by Daniel K (Standard support level)
Joined: 06/25/2009
Posts: 5

Thanks for the fast replies. I noticed the gobbledegook was the result of a double encryption. I corrected this, but am still doing something terribly wrong
Code
procedure TForm1.btnSymmEncClick(Sender: TObject); {---------------------------}
var enc : TElMessageEncryptor;
    dec : TElMessageDecryptor;
    myKey : TKey; //type TKey = array [1..16] of byte;
    msIN, msOUT :  TMemoryStream;
begin
  //1) Open stream for PlainText, open Stream for CipherText
  msIN := TMemoryStream.Create() ;
  msIN.LoadFromFile(ExtractFilePath(Application.ExeName) +  '\PT.txt');
  memo1.Lines.Add('PT.TXT : ' + IntToStr(msIN.Size)+ ' bytes'); //128 bytes
  msOUT := TMemoryStream.Create();
  msOUT.Position := 0;

  //2) Create a 16 byte key (using TElRandom)
  myKey := CreateKey('password');

  //3) Encrypt
  enc := SBMessages.ElMessageEncryptor.Create(nil);
  enc.Algorithm := SB_ALGORITHM_CNT_AES128;
  enc.Encrypt(msIN,msOUT,@myKey,SizeOf(myKey));
  memo1.Lines.Add('CT.TXT : ' + IntToStr(msOUT.Size) + ' bytes'); //223 bytes

  //4) Clear content and save result to disk
  msIN.Clear; //Clear content
  msOUT.SaveToFile(ExtractFilePath(Application.ExeName) +  '\CT.txt');

  //5)) Decrypt
  dec := SBMessages.ElMessageDecryptor.Create(nil);
  dec.Decrypt(msOUT,msIN,@myKey,SizeOf(myKey),SizeOf(msOUT));
  memo1.Lines.Add('PT".TXT : ' + IntToStr(msIN.Size)+ ' bytes'); //0 bytes

  //6) Save result to disk
  msIN.SaveToFile(ExtractFilePath(Application.ExeName) +  '\PT2.txt');

  //clear memory
  FreeAndNil(enc);
  FreeAndNil(dec);
  FreeAndNil(msIN);
  FreeAndNil(msOUT);
end; {*************************************************************************}


Could you please point out what I'm doing wrong? Thanks!
#10512
Posted: 07/01/2009 06:00:29
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

I guess you should write msOUT.Size instead of SizeOf(msOUT) in .Decrypt call.
#10513
Posted: 07/01/2009 06:43:09
by Daniel K (Standard support level)
Joined: 06/25/2009
Posts: 5

Mykola,

I finally found what I was doing wrong. It is a combination of msOUT.Size and placing the position of the pointer to the beginning of the stream before starting the decryption. Below is the example code which might be of interest for other users as well.

Code
procedure TForm1.btnSymmEncClick(Sender: TObject); {---------------------------}
var enc : TElMessageEncryptor;
    dec : TElMessageDecryptor;
    myKey : TKey; //type TKey = array [1..16] of byte;
    msIN, msOUT :  TMemoryStream;
begin
  //1) Open stream for PlainText, open Stream for CipherText
  msIN := TMemoryStream.Create() ;
  msIN.LoadFromFile(ExtractFilePath(Application.ExeName) +  '\PT.txt');
  memo1.Lines.Add('PT.TXT : ' + IntToStr(msIN.Size)+ ' bytes'); //128 bytes
  msOUT := TMemoryStream.Create();
  msOUT.Position := 0;

  //2) Create a 16 byte key (using TElRandom)
  myKey := CreateKey('password');

  //3) Encrypt
  enc := SBMessages.ElMessageEncryptor.Create(nil);
  enc.Algorithm := SB_ALGORITHM_CNT_AES128;
  enc.Encrypt(msIN,msOUT,@myKey,SizeOf(myKey));
  memo1.Lines.Add('CT.TXT : ' + IntToStr(msOUT.Size) + ' bytes'); //223 bytes

  //4) Clear content and save result to disk
  msIN.Clear;  //Clear content
  msOUT.SaveToFile(ExtractFilePath(Application.ExeName) +  '\CT.txt');

  //5)) Decrypt
  msOUT.Position := 0; //IMPORTANT : reset msOUT index
  dec := SBMessages.ElMessageDecryptor.Create(nil);
  //dec.Decrypt(msOUT,msIN,@myKey,SizeOf(myKey), SizeOf(msOUT)); THIS FAILS
  dec.Decrypt(msOUT,msIN,@myKey,SizeOf(myKey), msOUT.Size);
  //dec.Decrypt(msOUT,msIN,@myKey,16,0);  //hardcoded works fine as well
  memo1.Lines.Add('PT".TXT : ' + IntToStr(msIN.Size)+ ' bytes'); //128 bytes

  //6) Save result to disk
  msIN.SaveToFile(ExtractFilePath(Application.ExeName) +  '\PT2.txt');

  //7) clear memory
  FreeAndNil(enc);
  FreeAndNil(dec);
  FreeAndNil(msIN);
  FreeAndNil(msOUT);
end; {*************************************************************************}


Cheers,
Daniel
Also by EldoS: CallbackProcess
A component to control process creation and termination in Windows and .NET applications.

Reply

Statistics

Topic viewed 1988 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!