#1 2018-06-16 17:54:57

ertank
Member
Registered: 2016-03-16
Posts: 163

Help needed with TAESECB

Hello,

There is a Aes-ECB crypt information with sample input values and result. I want to have same result using mORMot, but I could not achieve that. Code I tried is below:

const
  AKey: string  = '783490FD6A6C90F07236A8ED402794F8732C96FB711FA0F46C349AC4792493E8';
var
  Key: TSHA256Digest;
  Aes: TAESECB;
  Utf8String: RawByteString;
begin
  Key := SHA256Digest(Pointer(@AKey), Length(AKey));

  Aes := TAESECB.Create(Key, 256);
  try
    Utf8String := StringToUTF8('000102030405060708090A0B0C0D0E0F');
    try
      Utf8String := BinToBase64(Aes.EncryptPKCS7(Utf8String, False));
    except
      Exit();
    end;
    ShowMessage(string(Utf8String));
  finally
    Aes.Free();
  end;
end;

Result I get is: 'oZSifB86ZMrA0l5sYpUyCj85t2klIRSDqcwEnyx+wHVugxvzP3U9+BtDD+eqx+pu'
Result I expected is: 'E6861877DB7B021E8B755F927243ED7B'

Online crypt link for same data: http://extranet.cryptomathic.com/aescal … 927243ED7B

I so far only used TAESCFB with mORMot with the help of this forum. I do not have experience about cryptography. I could not fix my code.

I appreciate any help.

Offline

#2 2018-06-28 12:08:17

jaclas
Member
Registered: 2014-09-12
Posts: 215

Re: Help needed with TAESECB

1. EncryptPKCS7() generate random IV (initialization vector), the result will be different each time for the same input data
try this method:

    /// perform the AES cypher in the corresponding mode
    // - when used in block chaining mode, you should have set the IV property
    procedure Encrypt(BufIn, BufOut: pointer; Count: cardinal); virtual; abstract;

but you must known and pass IV

2. You use encoding to Base64, but you expected result in hex?

Offline

#3 2018-07-01 22:21:32

ertank
Member
Registered: 2016-03-16
Posts: 163

Re: Help needed with TAESECB

Hello,

jaclas wrote:

but you must known and pass IV

I do not know much about encryption. Sample link I provided above says ECB mode does not require any IV. I do not know how to use ECB mode in mORMot.

jaclas wrote:

2. You use encoding to Base64, but you expected result in hex?

You are right. It seems result should be converted to hex from bytes. However, my initial problem above still remains.

Thanks.

Offline

#4 2018-07-02 20:31:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,238
Website

Re: Help needed with TAESECB

Yes, ECB doesn't need any IV, but it is also an unsafe mode, which should not be used.

Offline

Board footer

Powered by FluxBB