Delphi:AES CBC加密与PHP mcrypt_encrypt兼容

时间:2016-11-03 14:12:12

标签: php delphi encryption aes

我正在尝试在Delphi中加密数据然后用PHP解密它,但是Delphi输出与我用PHP得到的不同,所以我首先测试加密字符串的PHP和Delphi输出是否相同:

Delphi加密,使用DCPcrypt:

Uses DCPcrypt2, DCPblockciphers, DCPrijndael;

var Cipher : TDCP_rijndael;
     Data, Key, IV : Ansistring;
begin
  key := '12345678901234567890123456789012';
  iv  := '1234567890123456';
  Data := 'thisis128bitstxt';

  Cipher := TDCP_rijndael.Create(nil);
  Cipher.Init(Key[1],128,@IV[1]);
  Cipher.EncryptCBC(Data[1],Data[1],Length(Data));
  Cipher.Free;

  With TMemoryStream.Create do begin
    Write(Data[1],Length(Data));
    SaveToFile('z:\delphi');
    Free;
  end;

现在PHP中的代码:

<?php
    $source = "thisis128bitstxt"; //128-bits block
    $key = "12345678901234567890123456789012"; // 32
    $iv = "1234567890123456"; // 16
    $source = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$key,$source,MCRYPT_MODE_CBC,$iv);
    $out = fopen ('php', 'w');
    fwrite ($out, $source);
    fclose ($out);
?>

结果:

enter image description here

怎么了?

1 个答案:

答案 0 :(得分:0)

变化:

$source = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$key,$source,MCRYPT_MODE_CBC,$iv);

为:

$source = mcrypt_cbc(MCRYPT_RIJNDAEL_128,$KEYkey$source,MCRYPT_ENCRYPT,$iv);