Recent

Author Topic: Is there lib for rijndael256  (Read 5320 times)

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: Is there lib for rijndael256
« Reply #15 on: March 22, 2019, 01:18:33 pm »
Nothing in API PDF about padding, but it use CBC mode,
like in this somple php code

Code: [Select]
        $iv = hash("SHA256", $apikey, true);
        $key = md5($secret);
        $output = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $string, MCRYPT_MODE_CBC, $iv);
        return rtrim(strtr(base64_encode($output), '+/', '-_'), '=');

Just download CryptoLib4Pascal and it's dependencies and compile the attached demo.
demo produces same result as your vectors.

Zaher

  • Hero Member
  • *****
  • Posts: 680
    • parmaja.org
Re: Is there lib for rijndael256
« Reply #16 on: March 22, 2019, 04:09:54 pm »
Yes, thank you so much,

It is work fine, I will post it here for easy find it by searchers

https://github.com/Xor-el/CryptoLib4Pascal/

Code: [Select]
function CustomRijndael256Encrypt(const AInput, AKey: AnsiString; AIV: TBytes): AnsiString;
var
  engine: IRijndaelEngine;
  blockCipher: ICbcBlockCipher;
  cipher: IBufferedCipher;
  KeyParametersWithIV: IParametersWithIV;
  InputBytes, KeyBytes, EncryptionResult: TBytes;
begin
  InputBytes := TEncoding.UTF8.GetBytes(UnicodeString(AInput));

  KeyBytes := TEncoding.UTF8.GetBytes(UnicodeString(AKey));
  //IVBytes := TBase16.Decode(AIV);

  engine := TRijndaelEngine.Create(256);
  blockCipher := TCbcBlockCipher.Create(engine); // CBC
  cipher := TPaddedBufferedBlockCipher.Create(blockCipher, TZeroBytePadding.Create() as IZeroBytePadding);

  KeyParametersWithIV := TParametersWithIV.Create(TKeyParameter.Create(keyBytes) as IKeyParameter, AIV);

  cipher.Init(True, KeyParametersWithIV);
  EncryptionResult := cipher.DoFinal(InputBytes);
  // check excess padding and remove to conform to certain implementations like CryptoJs
  if System.Length(InputBytes) mod 32 = 0 then
  begin
    EncryptionResult := TArrayUtils.CopyOf(EncryptionResult, System.Length(EncryptionResult) - 32);
  end;
  Result := TBase64.UrlEncoding.Encode(EncryptionResult);
end;
« Last Edit: May 08, 2019, 08:51:31 pm by Zaher »

Xor-el

  • Sr. Member
  • ****
  • Posts: 404
Re: Is there lib for rijndael256
« Reply #17 on: March 22, 2019, 11:56:59 pm »
You are welcome.

 

TinyPortal © 2005-2018