简体   繁体   English

Delphi:AES CBC加密与PHP mcrypt_encrypt兼容

[英]Delphi: AES CBC encryption compatible with PHP mcrypt_encrypt

I'm trying to encrypt data in Delphi then decrypt it in PHP, but the Delphi output differs from what I get with PHP, so I am first testing whether both the PHP and Delphi output from encrypting a string is the same: 我正在尝试在Delphi中加密数据然后用PHP解密它,但是Delphi输出与我用PHP得到的不同,所以我首先测试加密字符串的PHP和Delphi输出是否相同:

Delphi encryption, using DCPcrypt: 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;

Now the code in PHP: 现在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);
?>

Results: 结果:

在此输入图像描述

What's wrong? 怎么了?

Change: 更改:

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

to: 至:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM