PHP AES 128 ECB密码和Delphi AES 128 ECB [英] PHP AES 128 ECB Ciphers and Delphi AES 128 ECB

查看:865
本文介绍了PHP AES 128 ECB密码和Delphi AES 128 ECB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是加密的sifrelenecek字符串与德尔福使用AES 128 ECB使用键作为KRPTTT101103,它给了我FBE4A4405D6C1B54503D9B213E41AE56,我检查与 http://aes.online-domain-tools.com/ ,这是正确的。我试图使用这个函数与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)

php给我的结果为+ wL2yf + 72thixicjw0duQA ==,我如何加密在Delphi和解密在PHP或对立?



在网上搜索,发现这么多函数,但是没有任何这些函数结果与Delphi或 http://aes.online-domain-tools.com/



提前感谢。

解决方案

我们可以看到你尝试比较使用十六进制编码的。另一个用base64。



$ p


  • 让pading离开(这是自动完成的)。

  • 不要执行 base64_encode (您在delphi中也没有这么做)。






php手动



说明:







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



加密资料并传回。



......



data



将使用给定的密码和模式加密的数据。如果
的数据大小不是n * blocksize,数据将用'\0'填充



返回的crypttext可以大于由数据给出的数据
的大小。


ECB模式忽略IV,是误导显示使用MCRYPT_MODE_ECB和IV(示例在手册中的示例显示相同的事情)的示例。另外,重要的是要知道ECB对随机数据有用,但结构化数据应使用更强的模式,如MCRYPT_MODE_CBC



php代码

  function encrypt($ input){
// $ iv = mcrypt_create_iv(32);
$ mcr = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,KRPTTT101103,
$ input,MCRYPT_MODE_ECB);
$ hex2 = bin2hex($ mcr); //将二进制数据转换为十六进制表示形式
return strtoupper($ hex2);
// base64_encode($ mcr);
}

$ encryptedhextext = encrypt(sifrelenecek);

if($ encryptedhextext ==FBE4A4405D6C1B54503D9B213E41AE56){
echoDelphi和php中的加密Hex文本等于< br />
echo $ encryptedhextext。== FBE4A4405D6C1B54503D9B213E41AE56;
}

输出



Delphi和php中的加密十六进制文本相等

FBE4A4405D6C1B54503D9B213E41AE56 == FBE4A4405D6C1B54503D9B213E41AE56


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. I'm trying to create same encryption with php using this function ;

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 ?

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/

Thanks in advance.

解决方案

As we can see you try to compare one which is clearly encoded using hex. The other with base64.

in php

  • let the pading away(this is done automatically).
  • don't do a base64_encode (that you have also not done in delphi).

php manual

Description :


string mcrypt_encrypt ( string $cipher , string $key , string $data , string $mode [, string $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'.

The returned crypttext can be larger than the size of the data that was given by 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). 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

php Code

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
FBE4A4405D6C1B54503D9B213E41AE56 == FBE4A4405D6C1B54503D9B213E41AE56

这篇关于PHP AES 128 ECB密码和Delphi AES 128 ECB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆