(100%结贴)求把这几行代码翻译成PHP的

php中文网
发布: 2016-06-21 08:45:20
原创
1218人浏览过

const        XorKey:array[0..7] of Byte=($B2,$09,$AA,$55,$93,$6D,$84,$47); //字符串加密用 function Dec(Str:String):String;//字符解密函?vari,j:Integer;beginResult:='';j:=0;for i:=1 to Length(Str) div 2 do    begin      Result:=Result+Char(StrToInt('$'+Copy(Str,i*2-1,2)) xor XorKey[j]);      j:=(j+1) mod 8;    end;end;
登录后复制



就上面几行代码,
求懂php的帮忙把这个函数翻译成php的,
我delphi加密,然后php解密,


我对php一点也不懂,但是懂调用。

DeepBrain
DeepBrain

AI视频生成工具,ChatGPT +生成式视频AI =你可以制作伟大的视频!

DeepBrain 108
查看详情 DeepBrain

回复讨论(解决方案)

解决了200分奉上

还有一个帖子也是100分: http://bbs.csdn.net/topics/390998635

给个密文,我测试看看~

<?php$XorKey = array(0xB2,0x09,0xAA,0x55,0x93,0x6D,0x84,0x47);function Dec($str){	global $XorKey;	$result = "";	$j = 0;	for ($i = 0; $i < strlen($str)/2 - 1; $i++)	{		$result = $result . chr(hexdec($str[$i*2] . $str[$i*2+1]) ^ $XorKey[$j]);		$j = ++$j % 8;	}	return $result;}
登录后复制

function Dec($str){  $XorKey = array(0xB2,0x09,0xAA,0x55,0x93,0x6D,0x84,0x47);  $result = "";  for($i=0, $j=0; $i<strlen($str); $i+=2) {    $result .= chr(ord($str{$i} ^ 0) . chr(ord($str{$i+1}) ^ $XorKey[$j]);    $j = ++$j % 8;  }  return $result;}
登录后复制

能顺便把加密函数也翻译一下吗

function Enc(Str:String):String;//字符加密函?   ?是用的一??或加密
var
i,j:Integer;
begin
Result:='';
j:=0;
for i:=1 to Length(Str) do
    begin
      Result:=Result+IntToHex(Byte(Str[i]) xor XorKey[j],2);
      j:=(j+1) mod 8;
    end;
end;



好测试密文

给个密文,我测试看看~




密文  F76DC321A1  解出来应该是Edit2  但是用你翻译的php的解出来是Edit  少了一位


给个密文,我测试看看~




密文  F76DC321A1  解出来应该是Edit2  但是用你翻译的php的解出来是Edit  少了一位
<?php$XorKey = array(0xB2,0x09,0xAA,0x55,0x93,0x6D,0x84,0x47);function Dec($str){    global $XorKey;    $result = "";    $j = 0;    for ($i = 0; $i < strlen($str)/2; $i++)    {        $result = $result . chr(hexdec($str[$i*2] . $str[$i*2+1]) ^ $XorKey[$j]);        $j = ++$j % 8;    }    return $result;}echo Dec("F76DC321A1");
登录后复制

这样肯定没问题了。

$XorKey = array(0xB2,0x09,0xAA,0x55,0x93,0x6D,0x84,0x47);echo $s = enc('Edit2', $XorKey), PHP_EOL;echo dec($s, $XorKey);function Enc($Str, $XorKey) { //:String;//字符加密函?   ?是用的一??或加密  $Result = '';  $j = 0;  for($i=0; $i<strlen($Str); $i++) {    $Result .= sprintf('%02X', ord($Str{$i}) ^ $XorKey[$j]);    $j = ($j+1) % 8;  }  return $Result;}function Dec($str, $XorKey){  $result = "";  for($i=0, $j=0; $i<strlen($str); $i+=2) {    $result .= chr(hexdec($str{$i} . $str{$i+1}) ^ $XorKey[$j]);    $j = ++$j % 8;  }  return $result;}
登录后复制
F76DC321A1
Edit2

能顺便把加密函数也翻译一下吗

function Enc(Str:String):String;//字符加密函?   ?是用的一??或加密
var
i,j:Integer;
begin
Result:='';
j:=0;
for i:=1 to Length(Str) do
    begin
      Result:=Result+IntToHex(Byte(Str[i]) xor XorKey[j],2);
      j:=(j+1) mod 8;
    end;
end;



好测试密文



<?php$XorKey = array(0xB2,0x09,0xAA,0x55,0x93,0x6D,0x84,0x47);function Dec($str){    global $XorKey;    $result = "";    $j = 0;    for ($i = 0; $i < strlen($str)/2; $i++)    {        $result = $result . chr(hexdec($str[$i*2] . $str[$i*2+1]) ^ $XorKey[$j]);        $j = ++$j % 8;    }    return $result;}function Enc($str){	global $XorKey;	$result = "";	$j = 0;	for ($i = 0; $i < strlen($str); $i++)	{		$result = $result. dechex(ord($str[$i])^$XorKey[$j]);		$j = ++$j % 8;	}	return $result;}echo Enc("Edit2")."\n";echo Dec("F76DC321A1");
登录后复制

to: xuzuning 版主,你那个代码我执行的一直出错,不知道为什么。

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号