将加密/解密转换为其 PHP 等效项

Posted

技术标签:

【中文标题】将加密/解密转换为其 PHP 等效项【英文标题】:Convert encryption/decryption to its PHP equivalent 【发布时间】:2014-04-21 16:21:40 【问题描述】:

我正在尝试转换用 ActionScript 编写的加密/解密程序并找到它的 php 等效程序。编写动作脚本代码的开发人员告诉我,他使用“TEA”算法来做,但我不再与他联系。当我去寻找等效的 TEA 算法并运行它时,不幸的是,我得到的结果与 ActionScript 的结果不同。任何人都可以帮助我吗?提前致谢。

我在此处粘贴了 ActionScript 代码。

WIN_apiTEA.prototype.encrypt = function(src, key) 
          if(src.length == 0) return "";
          var v = this.charsToLongs(this.strToChars(src));
          var k = this.charsToLongs(this.hexToChars(key));
          var n = v.length;
          if (n % 2==1) v[n++] = 0;

          tempv = new Array(2);
          newv = new Array(v.length);
          for (i=0; i<v.length; i=i+2)
               tempv = this.mcrypt_encrypt(k,v,v[i+1]);
               newv[i] = tempv[0];
               newv[i+1] = tempv[1];
          

          var encStr = this.charsToHex(this.longsToChars(newv));

          return [encStr];

     

WIN_apiTEA.prototype.mcrypt_encrypt = function(k,y,z)

     sum = 0;
     while (sum != -957401312) 
          y = int(y + ((((z << 4) ^ (z >>> 5)) + z) ^ (sum + k[sum & 3])));
          sum = int(sum + 0x9E3779B9);
          z = int(z + ((((y << 4) ^ (y >>> 5)) + y) ^ (sum + k[(sum >>> 11) & 3])));
     
     return [y,z];


WIN_apiTEA.prototype.hexToChars = function(hex) 
          var codes = [];
          var hexArr = hex.split("");
          var hl = hexArr.length/2;
          for(var i=0;i<hl;i++) 
               codes[i] = int("0x"+hexArr[i*2]+hexArr[(i*2)+1]);
          
          return codes;
     

WIN_apiTEA.prototype.charsToLongs = function(chars) 
     var tlength = Math.ceil(chars.length/4);
     var temp = [];
     var ti = 0;
     for(var i = 0; i<tlength; i++)
          ti = i*4;
          temp[i] = (((chars[ti] << 24) + (chars[ti+1]<<16)) + (chars[ti+2]<<8)) + chars[ti+3];
     
     return temp;


WIN_apiTEA.prototype.longsToChars = function(longs) 
          var codes = [];
          var ll = longs.length;
          var x = 0;
          for(var i = 0; i<ll; i++) 
               codes[x++] = longs[i]>>>24 & 0xFF;
               codes[x++] = longs[i]>>>16 & 0xFF;
               codes[x++] = longs[i]>>>8 & 0xFF;
               codes[x++] = longs[i] & 0xFF;
          
          return codes;


WIN_apiTEA.prototype.charsToStr = function(chars) 
          var temp = "";
          var cl = chars.length;
          for(var i = 0; i<cl; i++) 
               temp += chr(chars[i]);
          
          return temp;


WIN_apiTEA.prototype.strToChars = function(src) 
     var codes = [];
     var strArr = src.split("");
     var sl = strArr.length;
     var cnt = 0;
     for(var i = 0; i<sl; i++) 
          codes[i] = ord(strArr[i]);
     
     return codes;


WIN_apiTEA.prototype.changeKey = function(enctext, keytext)
          return (enctext.substr(0, 8) + keytext.substr(8, 24));

我开始将它转换为 PHP,这就是我所拥有的,但是它不起作用(它给我的加密字符串与我运行原始 AS 版本时得到的不同):

class apiTea

public function encrypt($src, $key, $token)

    if (strlen($src) == 0) 
        return ("");
    
    $_loc1 = $this->charsToLongs($this->strToChars($src));
    $_loc3 = $this->charsToLongs($this->hexToChars($key));
    $n     = count($_loc1);
    if ($n % 2 == 1) 
        $_loc1[$n++] = 0;
    
    $tempv = array(
        2
    );
    $newv  = array(
        count($_loc1)
    );
    for ($i = 0; $i < count($_loc1); $i = $i + 2) 
        $tempv       = $this->mcrypt_encrypt($_loc3, $_loc1[$i], $_loc1[$i + 1]);
        $newv[$i]    = $tempv[0];
        $newv[i + 1] = $tempv[1];
    
    $encStr = $this->charsToHex($this->longsToChars($newv));
    return ($token . ':' . $encStr);

function mcrypt_encrypt($k, $y, $z)

    $_loc1 = $z;
    $_loc2 = $y;
    $_loc3 = $k;
    $sum   = 0;
    while ($sum != -957401312) 
        $_loc2 = (int) ($_loc2 + (($_loc1 << 4 ^ $_loc1 >> 5) + $_loc1 ^ $sum + $_loc3[$sum & 3]));
        $sum   = (int) ($sum + 2654435769.000000);
        $_loc1 = (int) ($_loc1 + (($_loc2 << 4 ^ $_loc2 >> 5) + $_loc2 ^ $sum + $_loc3[$sum >> 11 & 3]));
    
    return (array(
        $_loc2,
        $_loc1
    ));

function mcrypt_decrypt($k, $y, $z)

    $_loc1 = $z;
    $_loc2 = $y;
    $_loc3 = $k;
    $sum   = -957401312;
    while ($sum) 
        $_loc1 = (int) ($_loc1 - (($_loc2 << 4 ^ $_loc2 >> 5) + $_loc2 ^ $sum + $_loc3[$sum >> 11 & 3]));
        $sum   = (int) ($sum - 2654435769.000000);
        $_loc2 = (int) ($_loc2 - (($_loc1 << 4 ^ $_loc1 >> 5) + $_loc1 ^ $sum + $_loc3[$sum & 3]));
    
    return (array(
        $_loc2,
        $_loc1
    ));

function decrypt($src, $key)

    if (substr($src, 0, 1) == "<") 
        return ($src);
     else 
        if (strlen($src) == 0) 
            return ("");
        
        $_loc1 = $this->charsToLongs($this->hexToChars($src));
        $_loc3 = $this->charsToLongs($this->hexToChars($key));
        $tempv = array(
            2
        );
        $newv  = array(
            strlen($_loc1)
        );
        for ($i = 0; $i < strlen($_loc1); $i = $i + 2) 
            $tempv        = $this->mcrypt_decrypt($_loc3, $_loc1[$i], $_loc1[$i + 1]);
            $newv[$i]     = $tempv[0];
            $newv[$i + 1] = $tempv[1];
        
        return ($this->charsToStr($this->longsToChars($newv)));
    

function charsToHex($chars)

    $_loc2 = $chars;
    $temp  = (string) ("");
    $_loc3 = array(
        "0",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "a",
        "b",
        "c",
        "d",
        "e",
        "f"
    );
    for ($_loc1 = 0; $_loc1 < count($_loc2); $_loc1++) 
        $temp = $temp + ($_loc3[$_loc2[$_loc1] >> 4] + $_loc3[$_loc2[$_loc1] & 15]);
    
    return ($temp);

function charsToLongs($chars)

    $_loc3   = $chars;
    $tlength = ceil(count($_loc3) / 4);
    $temp    = array();
    $_loc2   = 0;
    for ($_loc1 = 0; $_loc1 < $tlength; $_loc1++) 
        $_loc2        = $_loc1 * 4;
        $temp[$_loc1] = ((($_loc3[$_loc2] << 24) + ($_loc3[$_loc2 + 1] << 16)) + ($_loc3[$_loc2 + 2] << 8)) + $_loc3[$_loc2 + 3];
    
    return ($temp);

function longsToChars($longs)

    $_loc2 = array();
    $ll    = count($longs);
    $_loc3 = 0;
    for ($_loc1 = 0; $_loc1 < $ll; $_loc1++) 
        $_loc2[$_loc3++] = $longs[$_loc1] >> 24 & 255;
        $_loc2[$_loc3++] = $longs[$_loc1] >> 16 & 255;
        $_loc2[$_loc3++] = $longs[$_loc1] >> 8 & 255;
        $_loc2[$_loc3++] = $longs[$_loc1] & 255;
    
    return ($_loc2);

function hexToChars($hex)

    $chars = array();
    for ($i = (substr($hex, 0, 2) == "0x" ? 2 : 0); $i < strlen($hex); $i += 2) 
        array_push($chars, intval(substr($hex, $i, 2), 16));
    
    return $chars;

function charsToStr($chars)

    $str = "";
    for ($i = 0; $i < count($chars); $i++) 
        $str .= chr($chars[$i]);
    
    return $str;

function strToChars($str)

    $chars = array();
    foreach (str_split($str) as $ord) 
        array_push($chars, ord($ord));
    
    return $chars;

function changeKey($enctext, $keytext)

    // not needed yet


【问题讨论】:

【参考方案1】:

使用 AS3Crypto,您可以在这种情况下找到与 ASE-CBC 兼容的解决方案,请看这里: decrypt a as3crypto encrypted text in PHP

【讨论】:

以上是关于将加密/解密转换为其 PHP 等效项的主要内容,如果未能解决你的问题,请参考以下文章

加密/解密java到PHP

php源代码被加密了,请问如何解密?

2个比较经典的PHP加密解密函数分享

PHP加密解密数字

这是PHP啥加密方式 如何解密?

如何在php中加密/解密数据?