简体   繁体   English

PHP AES 128 ECB密码和Delphi AES 128 ECB

[英]PHP AES 128 ECB Ciphers and Delphi AES 128 ECB

I'm encryption "sifrelenecek" string with Delphi using AES 128 ECB using key as "KRPTTT101103" and it gives me "FBE4A4405D6C1B54503D9B213E41AE56", i'm checking with http://aes.online-domain-tools.com/ and it's correct. 我正在使用Delphi使用AES 128 ECB使用密钥作为“ KRPTTT101103”来加密“ sifrelenecek”字符串,它给了我“ FBE4A4405D6C1B54503D9B213E41AE56”,我正在使用http://aes.online-domain-tools.com/进行检查,它是正确的。 I'm trying to create same encryption with php using this function ; 我正在尝试使用此功能使用php创建相同的加密;

function sifrele($str, $key){
 $block = mcrypt_get_block_size('rijndael_128', 'ecb');
 $pad = $block - (strlen($str) % $block);
 $str .= str_repeat(chr($pad), $pad);
 return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_ECB)); }

print sifrele("sifrelenecek","KRPTTT101103")

but php gives me the result as "+wL2yf+72thixicjw0duQA==", how can i encrypt in Delphi and Decrypt in php or the opposit ? 但是php给我的结果是“ + wL2yf + 72thixicjw0duQA ==”,我如何在Delphi中加密并在php或相反的地方解密?

Searched on the web and found so many functions but not any of those functions results are the same with Delphi or http://aes.online-domain-tools.com/ 在网络上搜索并发现了这么多函数,但这些函数的结果与Delphi或http://aes.online-domain-tools.com/都不相同

Thanks in advance. 提前致谢。

You are mixing two different forms of padding. 您正在混合两种不同形式的填充。 This is what is causing the mismatch. 这就是导致不匹配的原因。 It's not merely a Base64/hex difference. 这不仅仅是Base64 / hex的区别。

Your plaintext is 12 bytes: "sifrelenecek", encoded as: 您的纯文本为12个字节:“ sifrelenecek”,编码为:

[115, 105, 102, 114, 101, 108, 101, 110, 101, 99, 101, 107]

If you pad the plaintext with ZEROES , as apparently Delphi does, and as mcrypt_encrypt is documented as doing, then you are encrypting: 如果您用ZEROES填充明文,显然是Delphi这样做,并且正如mcrypt_encrypt所记录的那样,那么您正在加密:

[115, 105, 102, 114, 101, 108, 101, 110, 101, 99, 101, 107, 0, 0, 0, 0]

The resulting ciphertext is ++SkQF1sG1RQPZshPkGuVg== in Base64 which, when decoded to plain bytes and re-encoded in hex, becomes "FBE4A4405D6C1B54503D9B213E41AE56" -- just what the online tool returns. 生成的密文是Base64中的++ SkQF1sG1RQPZshPkGuVg == ,当将其解码为纯字节并以十六进制重新编码时,将变为“ FBE4A4405D6C1B54503D9B213E41AE56”,这就是在线工具返回的内容。

But if you pad the plaintext with PKCS#7 padding , as you do in your PHP code above: 但是,如果您使用PKCS#7 padding填充纯文本,就像您在上述PHP代码中那样:

$pad = $block - (strlen($str) % $block);
$str .= str_repeat(chr($pad), $pad);

then this plaintext is padded with FOURS and you are encrypting: 然后,此纯文本用FOURS填充,并且您正在加密:

[115, 105, 102, 114, 101, 108, 101, 110, 101, 99, 101, 107, 4, 4, 4, 4]

The resulting ciphertext is +wL2yf+72thixicjw0duQA== -- just what you show above in your question. 所得的密文为+ wL2yf + 72thixicjw0duQA == -就是您上面在问题中显示的内容。

Either pad on both sides with ZEROES, or pad on both sides with PKCS#7, and your results should match. 要么在两侧都填充零,要么在两侧都填充PKCS#7,您的结果应该匹配。

As we can see you try to compare one which is clearly encoded using hex. 如我们所见,您尝试比较使用十六进制清晰编码的代码。 The other with base64. 另一个与base64。

in php 在PHP中

  • let the pading away(this is done automatically). 放空(这是自动完成的)。
  • don't do a base64_encode (that you have also not done in delphi). 不要做base64_encode (您在delphi中也没有做过)。

php manual PHP手册

Description : 说明:


string mcrypt_encrypt ( string $cipher , string $key , string $data , string $mode [, string $iv ] ) 字符串mcrypt_encrypt(字符串$ cipher,字符串$ key,字符串$ data,字符串$ mode [,字符串$ iv])

Encrypts the data and returns it. 加密数据并返回。

...... ......

data 数据

The data that will be encrypted with the given cipher and mode. 将使用给定的密码和模式加密的数据。 If the size of the data is not n * blocksize, the data will be padded with '\\0'. 如果数据的大小不是n *块大小,则数据将用'\\ 0'填充。

The returned crypttext can be larger than the size of the data that was given by data. 返回的crypttext可以大于data给定的数据的大小。

ECB mode ignores the IV, so it is misleading to show an example using both MCRYPT_MODE_ECB and an IV (the example in the manual shows the same thing). ECB模式忽略IV,因此使用MCRYPT_MODE_ECB和IV演示示例(在手册中的示例显示相同的内容)时会产生误导。 Also, it's important to know that ECB is useful for random data, but structured data should use a stronger mode like MCRYPT_MODE_CBC 同样,重要的是要知道ECB对于随机数据很有用,但是结构化数据应使用更强的模式,例如MCRYPT_MODE_CBC

php Code PHP代码

function encrypt($input) {
    // $iv = mcrypt_create_iv(32);
    $mcr = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, "KRPTTT101103",
                          $input, MCRYPT_MODE_ECB);
    $hex2 = bin2hex($mcr); // Convert binary data into hexadecimal representation
    return strtoupper($hex2);
    // base64_encode($mcr);
    }

$encryptedhextext = encrypt("sifrelenecek");

 if ($encryptedhextext == "FBE4A4405D6C1B54503D9B213E41AE56" ) {
   echo   "Encrypted Hex text in Delphi and php are equal<br />";    
   echo $encryptedhextext." == FBE4A4405D6C1B54503D9B213E41AE56";
 }

Output 输出量

Encrypted Hex text in Delphi and php are equal Delphi和php中的加密十六进制文本相等
FBE4A4405D6C1B54503D9B213E41AE56 == FBE4A4405D6C1B54503D9B213E41AE56 FBE4A4405D6C1B54503D9B213E41AE56 == FBE4A4405D6C1B54503D9B213E41AE56

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

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