Skip to main content

Delphi DEC library (Rijndael) encryption



I am trying to use the DEC 3.0 library ( Delphi Encryption Compedium Part I ) to encrypt data in Delphi 7 and send it to a PHP script through POST, where I am decrypting it with mcrypt (RIJNDAEL_256, ECB mode).





Delphi part:







uses Windows, DECUtil, Cipher, Cipher1;



function EncryptMsgData(MsgData, Key: string): string;

var RCipher: TCipher_Rijndael;

begin

RCipher:= TCipher_Rijndael.Create(KeyStr, nil);

RCipher.Mode:= cmECB;

Result:= RCipher.CodeString(MsgData, paEncode, fmtMIME64);

RCipher.Free;

end;







PHP part:







function decryptMsgContent($msgContent, $sKey) {

return mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $sKey, base64_decode($msgContent), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND));

}







The problem is that the decryption from PHP doesn't work and the output is gibberish, differing from the actual data.





Of course, Delphi Key and PHP $Key is the same 24 characters string.





Now I know DEC 3.0 is old and outdated, and I'm not an expert in encryption and can't tell if the inplementation is actually Rijndael 256. Maybe someone can tell me how this implementation differs from PHP's mcrypt w/ RIJNDAEL_256. Maybe the keysize is different, or the block size, but can't tell this from the code. Here's an excerpt from Cipher1.pas:







const

{don't change this}

Rijndael_Blocks = 4;

Rijndael_Rounds = 14;



class procedure TCipher_Rijndael.GetContext(var ABufSize, AKeySize, AUserSize: Integer);

begin

ABufSize := Rijndael_Blocks * 4;

AKeySize := 32;

AUserSize := (Rijndael_Rounds + 1) * Rijndael_Blocks * SizeOf(Integer) * 2;

end;







Side question:





I know ECB mode isn't recommended and I'll use CBC as soon as I get ECB working. The question is, do I have to transmit the generated IV in Delphi to the PHP script also? Or knowing the key is sufficient, like for ECB?


Comments

  1. You are calling the TCipher.Create(const Password: String; AProtection: TProtection); constructor, which will compute a hash of the password before passing it to the Init method, which performs the standard key schedule of the implemented algorithm. To override this key derivation, use:

    function EncryptMsgData(MsgData, Key: string): string;
    var RCipher: TCipher_Rijndael;
    begin
    RCipher:= TCipher_Rijndael.Create('', nil);
    RCipher.Init(Pointer(Key)^,Length(Key),nil);
    RCipher.Mode:= cmECB;
    Result:= RCipher.CodeString(MsgData, paEncode, fmtMIME64);
    RCipher.Free;


    end;

    ReplyDelete
  2. OK, so to sum this up, there were 3 problems with my code:


    Due to my poor understanding of mcrypt and ciphers in general, MCRYPT_RIJNDAEL_256 refers to 128 bits block and doesn't refer to the keysize. My correct choice should have been MCRYPT_RIJNDAEL_128, which is the AES standard and is also supported by DEC 3.0.
    DEC has it's own default key derivation, so I needed to bypass it so I wouldn't have to implement it in PHP also. In actuality, I am using my own key derivation algorithm that was easy to reproduce in PHP (first 32 characters of sha1(key)).
    DEC doesn't pad plaintext to a multiple of the block size of the cipher, as mcrypt expects, so I had to do it manually.


    Providing working code below:

    Delphi:

    uses Windows, DECUtil, Cipher, Cipher1, CryptoAPI;

    function EncryptMsgData(MsgData, Key: string): string;
    var RCipher: TCipher_Rijndael;
    KeyStr: string;
    begin
    Result:= '';
    try
    // key derivation; just making sure to feed the cipher a 24 chars key
    HashStr(HASH_SHA1, Key, KeyStr);
    KeyStr:= Copy(KeyStr, 1, 24);
    RCipher:= TCipher_Rijndael.Create('', nil);
    RCipher.Init(Pointer(KeyStr)^, Length(KeyStr), nil);
    RCipher.Mode:= cmECB;
    Result:= RCipher.CodeString(MsgData + StringOfChar(#0,16-(Length(MsgData) mod 16)), paEncode, fmtMIME64);
    RCipher.Free;
    except
    end;
    end;


    PHP:

    function decryptMsgContent($msgContent, $sKey) {
    $sKey = substr(sha1(sKey), 0, 24);
    return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $sKey, base64_decode($msgContent), MCRYPT_MODE_ECB, mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB), MCRYPT_RAND)));
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.