EldoS | Feel safer!

Software components for data protection, secure storage and transfer

error in AES encryption

Also by EldoS: RawDisk
Access locked and protected files in Windows, read and write disks and partitions and more.
#831
Posted: 07/24/2006 04:29:47
by Gloria Ho (Standard support level)
Joined: 07/20/2006
Posts: 12

I tried to do AES encryption with TElAESSymmetricCrypto but get an error:
---------------------------
Project Project1.exe raised exception class EDivByZero with message 'Division by zero'.
---------------------------

Code
procedure AES_Encryption;
var
  AES: TElAESSymmetricCrypto;
  key: TElSymmetricKeyMaterial;
  INStream: TFileStream;
  OUTStream: TFileStream;
begin
  INStream := TFileStream.Create('In.txt', fmOpenRead);
  OUTStream := TFileStream.Create('out.txt', fmCreate);
  key := TElSymmetricKeyMaterial.Create;
  Aes := TElAESSymmetricCrypto.Create(cmECB);
  try
    key.Generate(128);
    aes.KeyMaterial := Key;
    aes.Encrypt(INStream, OUTStream);  [COLOR=red]// <-- error raised here [/COLOR]
  finally
    FreeAndNil(INStream);
    FreeAndNil(OUTStream);
    aes.Free;
    Key.Free;
  end;
end;


Besides, can you show me the options for TElSymmetricKeyMaterial.Algorithm?
Thanks a lot! ;) [COLOR=red]
#833
Posted: 07/24/2006 04:45:23
by Eugene Mayevski (EldoS Corp.)

You have not specified the IV for encryption. Of course, some sanity checks must be added to avoid the problem.


Sincerely yours
Eugene Mayevski
#834
Posted: 07/24/2006 04:56:50
by Gloria Ho (Standard support level)
Joined: 07/20/2006
Posts: 12

I don't understand why IV is necessary here as I specify ECB encryption modeto be used.
#835
Posted: 07/24/2006 05:21:18
by Eugene Mayevski (EldoS Corp.)

Found it. You need to use another constructor, then block size is initialized properly. BTW the right method to create encryption class is use the factory (TElSymmetricCryptoFactory).


Sincerely yours
Eugene Mayevski
#836
Posted: 07/24/2006 05:35:13
by Eugene Mayevski (EldoS Corp.)

Also you need to set padding:

aes.Padding := cpPKCS5;


Sincerely yours
Eugene Mayevski
#837
Posted: 07/24/2006 06:21:28
by Gloria Ho (Standard support level)
Joined: 07/20/2006
Posts: 12

Quote
Found it. You need to use another constructor, then block size is initialized properly. BTW the right method to create encryption class is use the factory (TElSymmetricCryptoFactory).


You mean AES should be as TElSymmetricCryptoFactory but not TElAESSymmetricCrypto?

#838
Posted: 07/24/2006 06:26:12
by Gloria Ho (Standard support level)
Joined: 07/20/2006
Posts: 12

Or any suitable sample I should have a look?
#839
Posted: 07/24/2006 06:50:45
by Eugene Mayevski (EldoS Corp.)

Here's your code that works:

Code

var
  AES: TElAESSymmetricCrypto;
  key: TElSymmetricKeyMaterial;
  INStream: TFileStream;
  OUTStream: TFileStream;
begin
  INStream := TFileStream.Create('c:\temp\In.txt', fmOpenRead);
  OUTStream := TFileStream.Create('c:\temp\out.txt', fmCreate);
  key := TElSymmetricKeyMaterial.Create;
  Aes := TElAESSymmetricCrypto.Create(SB_ALGORITHM_CNT_AES128, cmECB);
  try
    key.Generate(128);
    aes.Padding := cpPKCS5;
    aes.KeyMaterial := Key;
    aes.Encrypt(INStream, OUTStream);
  finally
    FreeAndNil(INStream);
    FreeAndNil(OUTStream);
    aes.Free;
    Key.Free;
  end;
end;


Sincerely yours
Eugene Mayevski
Also by EldoS: MsgConnect
Cross-platform protocol-independent communication framework for building peer-to-peer and client-server applications and middleware components.

Reply

Statistics

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