如何在PHP中使用扩展的ASCII而不是Unicode

发布于 2025-01-28 11:54:19 字数 936 浏览 1 评论 0原文

我有一个在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花桑 2025-02-04 11:54:19

这是一个错误...在VFP中,第一个字符具有索引1。以下代码为您提供了密码字符串的第一个字符:

SUBSTR(key,1,1)

PHP基于零。以下代码为您提供了密码字符串的第一个字符:

$clAve[0]

您是用1而不是0来初始化密码的索引($ clave)。因此,您最终进行了编码(无法调用此加密)与VFP相比,PHP中的字节不同。这是PHP中的工作版本:

function Encripta($teXto){
    $clAve = ")&H%\$V1#@^+=?/><:MN*-";
    $teXtoenc = "";
    $c = 0;
    for($j = 0; $j < strlen($teXto); $j++){
        $leTra = (ord($teXto[$j]) + ord($clAve[$c])) % 256;
        $c ++;
        $teXtoenc .= chr($leTra);
        if($c >= strlen($clAve))
            $c = 0;
    }
    return $teXtoenc;
}
?>

您在其中写$ C = 1;我将其更改为$ C = 0;。其余代码没有改变。

您本可以自己发现的方式是通过在VFP中打印PHP和ASC()调用的两个ord​​()调用的值。 PHP的结果将是:

72 38 / 111 72 / 108 37 / 97 36 

在VFP中,您会

72 41 / 111 38 / 108 72 / 97 37

注意到第二个数字(来自密码的字符)如何移动到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:

SUBSTR(key,1,1)

PHP is zero based. The following code gives you the first character of the password string:

$clAve[0]

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:

function Encripta($teXto){
    $clAve = ")&H%\$V1#@^+=?/><:MN*-";
    $teXtoenc = "";
    $c = 0;
    for($j = 0; $j < strlen($teXto); $j++){
        $leTra = (ord($teXto[$j]) + ord($clAve[$c])) % 256;
        $c ++;
        $teXtoenc .= chr($leTra);
        if($c >= strlen($clAve))
            $c = 0;
    }
    return $teXtoenc;
}
?>

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:

72 38 / 111 72 / 108 37 / 97 36 

and in VFP it would have been

72 41 / 111 38 / 108 72 / 97 37

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文