EldoS | Feel safer!

Software components for data protection, secure storage and transfer

AES encryption/decryption

Also by EldoS: CallbackFilter
A component to monitor and control disk activity, track file and directory operations (create, read, write, rename etc.), alter file data, encrypt files, create virtual files.
#19305
Posted: 03/06/2012 14:02:40
by mstaszew (Basic support level)
Joined: 03/06/2012
Posts: 3

I am trying to get a simple example together that encrypts a string, base64 encodes the result, and decrypts the encrypted stream. Here is my encryption code.

Code
procedure TForm1.Button1Click(Sender: TObject);
var
  Factory: TElSymmetricCryptoFactory;
  Crypto: TELSymmetricCrypto;
  KeyMaterial: TElSymmetricKeymaterial;
  CryptoProvider: TELCustomCryptoProvider;
  InBuff, OutBuff: TBytes;
  OutSize: Integer;
  Source, Dest: TMemoryStream;
begin
  CryptoProvider := nil;
  KeyMaterial := TElSymmetricKeymaterial.Create(CryptoProvider);
  Factory := TElSymmetricCryptoFactory.Create;
  Source := TMemoryStream.Create;
  Dest := TMemoryStream.Create;
  try
    Factory.CryptoProvider := CryptoProvider;
    Crypto := Factory.CreateInstance(SB_ALGORITHM_CNT_AES256, cmCBC);
    Crypto.KeyMaterial := KeyMaterial;

    { Initialize key and init vector }
    KeyMaterial.Key := BytesOfS tring(AnsiS tring('12345678901234561234567890123456'));
    KeyMaterial.IV := BytesOfS tring(AnsiS tring('1234567890123456123456'));

    { Get array of bytes representation of the unencrypted string }
    InBuff := TEncoding.UTF8.GetBytes(Trim(mSource.Lines.Text));
    Source.WriteBuffer(InBuff, Length(InBuff));
    Source.Position := 0;

    { Set padding }
    Crypto.Padding := cpPKCS5;

    { Calculate the size of the destination buffer }
    OutSize := 0;
    Crypto.Encrypt(Source, Source.Size, nil, OutSize);

    { Set size and decrypt }
    SetLength(OutBuff, OutSize);
    Crypto.Encrypt(Source, Source.Size, OutBuff, OutSize);
    Dest.WriteBuffer(OutBuff, Length(OutBuff));
    Dest.Position := 0;

    { Put base64 encoded encrypted string into memo }
    mEncrypted.Lines.Text := Base64Encode(Dest);
  finally
    Dest.Free;
    Source.Free;
    Factory.Free;
    KeyMaterial.Free;
  end;
end;


Here is my decryption code.

Code
procedure TForm1.Button5Click(Sender: TObject);
var
  Factory: TElSymmetricCryptoFactory;
  Crypto: TELSymmetricCrypto;
  KeyMaterial: TElSymmetricKeymaterial;
  CryptoProvider: TELCustomCryptoProvider;
  OutBuff: TBytes;
  OutSize: Integer;
  Source: TMemoryStream;
begin
  CryptoProvider := nil;
  KeyMaterial := TElSymmetricKeymaterial.Create(CryptoProvider);
  Factory := TElSymmetricCryptoFactory.Create;
  Source := TMemoryStream.Create;
  try
    Factory.CryptoProvider := CryptoProvider;
    Crypto := Factory.CreateInstance(SB_ALGORITHM_CNT_AES256, cmCBC);
    Crypto.KeyMaterial := KeyMaterial;

    { Initialize key and init vector }
    KeyMaterial.Key := BytesOfS tring(AnsiS tring('12345678901234561234567890123456'));
    KeyMaterial.IV := BytesOfS tring(AnsiS tring('1234567890123456123456'));

    { Convert base64 encoded encrypted stream to a binary stream }
    Base64Decode(mEncrypted.Lines.Text, Source);
    Source.Position := 0;

    { Set padding }
    Crypto.Padding := cpPKCS5;

    { Calculate the size of the destination buffer }
    OutSize := 0;
    Crypto.Decrypt(Source, Source.Size, nil, OutSize);

    { Set size and decrypt }
    SetLength(OutBuff, OutSize);
    Crypto.Decrypt(Source, Source.Size, OutBuff, OutSize);

    { Show the unencrypted string finalize }
    mDecrypted.Lines.Text := TEncoding.UTF8.GetS tring(OutBuff);
  finally
    Source.Free;
    Factory.Free;
    KeyMaterial.Free;
  end;
end;


If my input string is 'This is a test' and my key, iv, padding, algorithm, and mode are as set in the source above I get 'aDuLAkg7iwIAAAAAADCLAg==' as my base64 encoded encrypted stream. I'm using Indy by the way to generate the base64 string which I have had no issues with in the past so I do not think this is the issue. When I decrypt the string I get an EElSymmetricCryptoError exception with text 'Invalid symmetric cipher padding.'

What am I doing incorrectly? Any help is appreciated.
#19306
Posted: 03/06/2012 14:10:06
by Vsevolod Ievgiienko (EldoS Corp.)

Thank you for contacting us.

I've created a ticket in Helpdesk for you and attached a sample there. This sample does exactly what you need and it will be included in the next SBB build.

Reply

Statistics

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