如何在PHP中使用扩展的ASCII而不是Unicode
我有一个在Visual Fox Pro中开发的应用程序,在该应用程序中,我具有要转移到PHP代码的加密方法,该代码如下:
FUNCTION Encrypt
LPARAMETER text
LOCAL KEY, TEXT, J, LETTER
key = ')&H%$V1#@^+=?/><:MN*-'
textText = SPACE(0)
c = 1
FOR j = 1 TO LEN(text)
letter = MOD(ASC(SUBSTR(text, j, 1))+ASC(SUBSTR(key, c, 1)), 256)
teXtoenc = teXtoenc+CHR(letter)
c = c+1
IF c>=LEN(password)
c = 1
ENDIF
ENDFOR
RETURN text
ENDFUNC
PHP中的转换为:
<?php
function Encripta($teXto){
$clAve = ")&H%\$V1#@^+=?/><:MN*-";
$teXtoenc = "";
$c = 1;
for($j = 0; $j < strlen($teXto); $j++){
$leTra = (odr($teXto[$j]) + ord($clAve[$c])) % 256;
$c ++;
$teXtoenc .= chr($leTra);
if($c >= strlen($clAve))
$c = 1;
}
return $teXtoenc;
}
?>
但是,当在PHP中执行代码时,两种方法都不一样,研究我发现原因是因为PHP可与Unicode一起使用,而不是扩展的ASCII
I have an application that is developed in visual fox pro in which I have an encryption method that I want to transfer to php code, the code is as follows:
FUNCTION Encrypt
LPARAMETER text
LOCAL KEY, TEXT, J, LETTER
key = ')&H%$V1#@^+=?/><:MN*-'
textText = SPACE(0)
c = 1
FOR j = 1 TO LEN(text)
letter = MOD(ASC(SUBSTR(text, j, 1))+ASC(SUBSTR(key, c, 1)), 256)
teXtoenc = teXtoenc+CHR(letter)
c = c+1
IF c>=LEN(password)
c = 1
ENDIF
ENDFOR
RETURN text
ENDFUNC
the transformation in php would be:
<?php
function Encripta($teXto){
$clAve = ")&H%\$V1#@^+=?/><:MN*-";
$teXtoenc = "";
$c = 1;
for($j = 0; $j < strlen($teXto); $j++){
$leTra = (odr($teXto[$j]) + ord($clAve[$c])) % 256;
$c ++;
$teXtoenc .= chr($leTra);
if($c >= strlen($clAve))
$c = 1;
}
return $teXtoenc;
}
?>
but when executing the code in php the results of both methods are not the same, investigating I found that reason is because php works with Unicode and not extended ASCII
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个错误...在VFP中,第一个字符具有索引1。以下代码为您提供了密码字符串的第一个字符:
PHP基于零。以下代码为您提供了密码字符串的第一个字符:
您是用1而不是0来初始化密码的索引(
$ clave
)。因此,您最终进行了编码(无法调用此加密)与VFP相比,PHP中的字节不同。这是PHP中的工作版本:您在其中写
$ C = 1;
我将其更改为$ C = 0;
。其余代码没有改变。您本可以自己发现的方式是通过在VFP中打印PHP和ASC()调用的两个ord()调用的值。 PHP的结果将是:
在VFP中,您会
注意到第二个数字(来自密码的字符)如何移动到PHP中,因为您从字符串中的第二个字符开始。
It's an off by one error... In VFP the first character has the index 1. The following code gives you the first character of the password string:
PHP is zero based. The following code gives you the first character of the password string:
You are initializing the index for the password (
$clAve
) with 1 instead of 0. Therefore you end up encoding (can't call this encrypting) with a different byte in PHP compared to VFP. Here's a working version in PHP:Where you wrote
$c = 1;
I changed this to$c = 0;
. The remaining code is unchanged.The way you could have found this yourself is by printing the value of both ord() calls in PHP and the ASC() calls in VFP. The result for PHP would have been:
and in VFP it would have been
You notice how the second number, the character from the password, moved to the left in PHP, because you started with the second character in the string.