将加密/解密转换为其PHP等价物

时间:2014-03-16 14:01:05

标签: php flash encryption actionscript mcrypt

我正在尝试转换使用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 个答案:

答案 0 :(得分:0)

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