title: different Delphi / PHP Encryption-Decryption using Rijndael
body: <p>I am trying to encrypt / decrypt a string using Rijndael from php to Delphi and back.</p>
  
  <p>If I decrypt a PHP string from Delphi ... works fine.</p>
  
  <p>If I encrypt a string with Delphi the result string is ok but shorter</p>
  
  <p>for test I used a string with 62 character. the encrypted string with delphi is long 4 char less of PHP</p>
  
  <p>these the strings ... last characters:</p>
  
  <p>PHP:     GyLWj1anBJRmE8mBsaO5cvTrcbvvA==</p>
  
  <p>Delphi:  GyLWj1anBJRmE8mBsaO5cvTrcbv</p>
  
  <p>thanks for any advices</p>
  
  <p>I use this source code example:</p>
  
  <p>PHP:</p>
  
  <pre><code>function encrypt ($key, $value)
  {
    $padSize = 16 - (strlen ($value) % 16) ;
    $value = $value . str_repeat (chr ($padSize), $padSize) ;
    $output = mcrypt_encrypt (MCRYPT_RIJNDAEL_128, $key, $value, MCRYPT_MODE_CBC, 'xxxxxxx') ;
    return base64_encode ($output) ;
  }
  </code></pre>
  
  <p>Delphi encrypt:</p>
  
  <pre><code>function EncryptData3(Data: string; AKey: AnsiString; AIv: AnsiString): string;
  var
    cipher: TDCP_rijndael;
    key, iv, src, dest, b64: TBytes;
    index, slen, bsize, pad: integer;
  begin
    //key := Base64DecodeBytes(TEncoding.UTF8.GetBytes(AKey));
    //iv := Base64DecodeBytes(TEncoding.UTF8.GetBytes(AIv));
    key := TEncoding.ASCII.GetBytes(AKey);
    iv := TEncoding.ASCII.GetBytes(AIv);
  
    src := TEncoding.ascii.GetBytes(Data);
  
    cipher := TDCP_rijndael.Create(nil);
    try
      cipher.CipherMode := cmCBC;
      // Add padding.
      // Resize the Value array to make it a multiple of the block length.
      // If it's already an exact multiple then add a full block of padding.
      slen := Length(src);
      bsize := (cipher.BlockSize div 8);
      pad := bsize - (slen mod bsize);
      Inc(slen, pad);
      SetLength(src, slen);
  
      for index := pad downto 1 do
      begin
        src[slen - index] := pad;
      end;
  
      SetLength(dest, slen);
      cipher.Init(key[0], 256, @iv[0]); // DCP uses key size in BITS not BYTES
      cipher.Encrypt(src[0], dest[0], slen);
  
      b64 := Base64EncodeBytes(dest);
      result := TEncoding.Default.GetString(b64);
    finally
      cipher.Free;
    end;
  end;
  </code></pre>
  
  <p>Delphi decrypt ... not works:</p>
  
  <pre><code>function DecryptData3(Data: string; AKey: AnsiString; AIv: AnsiString): string;
  var
    key, iv, src, dest: TBytes;
    cipher: TDCP_rijndael;
    slen, pad: integer;
  begin
    //key := Base64DecodeBytes(TEncoding.UTF8.GetBytes(AKey));
    //iv := Base64DecodeBytes(TEncoding.UTF8.GetBytes(AIv));
    key := TEncoding.ASCII.GetBytes(AKey);
    iv := TEncoding.ASCII.GetBytes(AIv);
  
    src := Base64DecodeBytes(TEncoding.UTF8.GetBytes(Data));
  
    cipher := TDCP_rijndael.Create(nil);
    try
      cipher.CipherMode := cmCBC;
      slen := Length(src);
      SetLength(dest, slen);
      cipher.Init(key[0], 256, @iv[0]); // DCP uses key size in BITS not BYTES
      cipher.Decrypt(src[0], dest[0], slen);
      // Remove the padding. Get the numerical value of the last byte and remove
      // that number of bytes
      pad := dest[slen - 1];
      SetLength(dest, slen - pad);
  
      // Base64 encode it
      result := TEncoding.Default.GetString(dest);
    finally
      cipher.Free;
    end;
  end;
  </code></pre>