EldoS | Feel safer!

Software components for data protection, secure storage and transfer

3des encryption/decryption example

Also by EldoS: Callback File System
Create virtual file systems and disks, expose and manage remote data as if they were files on the local disk.
#21943
Posted: 10/11/2012 08:36:30
by medax (Standard support level)
Joined: 08/28/2012
Posts: 36

Good day,
I not found good example for 3des encryption/decryption.
I found only this example: https://www.eldos.com/forum/read.php?FID=7&TID=2919&MID=16064&sphrase_id=407029#message16064 , but I donĀ“t know how what value set to property "TElSymmetricKeyMaterial.IV".

I have data and key string and please simple example for encryption/decryption.

Thank you, Martin
#21944
Posted: 10/11/2012 08:42:44
by Vsevolod Ievgiienko (EldoS Corp.)

Thank you for contacting us.

The last build contains a sample that is located in \EldoS\SecureBlackbox.VCL\Samples\Delphi\PKIBlackbox\SymmetricCrypto folder. It shows how to perform AES encryption, but can be simply changed to use 3DES. The changes depend on your key string. Is it a password or a raw key? What encryption mode do you need (CBC, CFB, ECB etc.).
#21945
Posted: 10/11/2012 08:44:33
by Eugene Mayevski (EldoS Corp.)

The sample there works as you have noticed, and you just need to setup Initialization Vector


Sincerely yours
Eugene Mayevski
#21947
Posted: 10/11/2012 09:42:38
by medax (Standard support level)
Joined: 08/28/2012
Posts: 36

Quote
The last build contains a sample that is located in \EldoS\SecureBlackbox.VCL\Samples\Delphi\PKIBlackbox\SymmetricCrypto folder.

I have installed SecureBlackbox Professional VCL, source code, version 9.1.216 and I not found subfolder "SymmetricCrypto". Please, can you send me examples from folder "SymmetricCrypto"?

Thank you, Martin.
#21948
Posted: 10/11/2012 09:47:41
by medax (Standard support level)
Joined: 08/28/2012
Posts: 36

Oh, I try download latest version 10.0...
Thank you, Martin.
#21955
Posted: 10/11/2012 15:57:31
by medax (Standard support level)
Joined: 08/28/2012
Posts: 36

Quote
Is it a password or a raw key?

It is password.

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

I try example from folder "SymmetricCrypto".


I make changes in example:

1. in procedure "bbEncryptClick" in first row I changed from "SB_ALGORITHM_CNT_AES256" to "SB_ALGORITHM_CNT_3DES"

2. in procedure "bbDecryptClick" in first row I changed from "SB_ALGORITHM_CNT_AES256" to "SB_ALGORITHM_CNT_3DES"

3. I changed procedure PasswordToKeyMaterial:
Code
function PasswordToKeyMaterial(const Pass: string): TElSymmetricKeyMaterial;
var
  IV: ByteArray;
begin
  SetLength(IV, 8);
  FillChar(IV[0], 8, 0);

  Result := TElSymmetricKeyMaterial.Create;
  Result.Key := SBUtils.BytesOfString(Pass);
  Result.IV := IV;
end;


I works. The changes are correct?

Thank you, Martin.
#21956
Posted: 10/11/2012 16:07:28
by Mykola Olshevsky (Basic support level)
Joined: 07/07/2005
Posts: 450

Everything looks ok, except that you should derive key from password (by hashing, for example) instead of directly passing a password to .Key property.
For 3DES key should be 192 bits (24 bytes) long.
#21957
Posted: 10/12/2012 01:49:01
by Eugene Mayevski (EldoS Corp.)

In SecureBlackbox 10 we've added key derivation functions to TElSymmetricKeyMaterial class.


Sincerely yours
Eugene Mayevski
#21958
Posted: 10/12/2012 03:36:11
by medax (Standard support level)
Joined: 08/28/2012
Posts: 36

Quote
Everything looks ok, except that you should derive key from password (by hashing, for example) instead of directly passing a password to .Key property.
For 3DES key should be 192 bits (24 bytes) long.


How do I use hash to be 192 bit?

Code
function PasswordToKeyMaterial(const Pass: string): TElSymmetricKeyMaterial;
var
  Hash : TElHashFunction;
  PassBytes : ByteArray;
  Digest : BufferType;
  IV : ByteArray;
begin
  SetLength(IV, 8);
  FillChar(IV[0], 8, 0);

  Hash := TElHashFunction.Create( ? );
  try
    PassBytes := SBUtils.BytesOfString(Pass);
    Hash.Update(@PassBytes[0], Length(PassBytes));
    Digest := Hash.Finish;
  finally
    FreeAndNil(Hash);
  end;

  Result := TElSymmetricKeyMaterial.Create;
  Result.Key := SBUtils.BytesOfString(Digest); // set 192-bit key
  Result.IV := IV; // set 64-bit initialization vector
end;


Thank you, Martin
#21959
Posted: 10/12/2012 03:41:56
by Vsevolod Ievgiienko (EldoS Corp.)

You can calculate SHA-256 hash (SB_ALGORITHM_DGST_SHA256) and cut it to 192 bits.
Also by EldoS: Rethync
The cross-platform framework that simplifies synchronizing data between mobile and desktop applications and servers and cloud storages

Reply

Statistics

Topic viewed 4372 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!