小编Joe*_*yen的帖子

PHP将字符串转换为十六进制,十六进制转换为

我在PHP中转换这两种类型时遇到了问题.这是我在谷歌搜索的代码

function strToHex($string){
    $hex='';
    for ($i=0; $i < strlen($string); $i++){
        $hex .= dechex(ord($string[$i]));
    }
    return $hex;
}


function hexToStr($hex){
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2){
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}
Run Code Online (Sandbox Code Playgroud)

当我使用XOR加密时,我检查并发现了这一点.

我有字符串"this is the test",在带有键的XOR之后,我的结果是字符串???§P?§P ?§T?§?.之后,我尝试通过函数strToHex()将其转换为十六进制,我得到了这些12181d15501d15500e15541215712.然后,我测试了函数hexToStr(),我有???§P?§P?§T?§q.那么,我该怎么做才能解决这个问题呢?当我转换这个2样式值时为什么会出错?

php string hex

43
推荐指数
4
解决办法
18万
查看次数

在PHP中使用XOR加密/解密

我正在研究加密.我遇到了这样的问题:

在我用密钥对XOR明文进行后,我得到一个密码,"010e010c15061b4117030f54060e54040e0642181b17",作为十六进制类型.如果我想从这个地穴获取明文,我该怎么做PHP?

我尝试将它转换为string/int,之后用键(三个字母)将它们转换为XOR.但它不起作用.

这是代码:

function xor_this($string) {

    // Let's define our key here
    $key = 'fpt';

    // Our plaintext/ciphertext
    $text = $string;

    // Our output text
    $outText = '';

    // Iterate through each character
    for($i=0; $i<strlen($text); )
    {
        for($j=0; $j<strlen($key); $j++,$i++)
        {
            $outText .= ($text[$i] ^ $key[$j]);
            //echo 'i=' . $i . ', ' . 'j=' . $j . ', ' . $outText{$i} . '<br />'; // For debugging
        }
    }
    return $outText;
}

function strToHex($string)
{
    $hex = ''; …
Run Code Online (Sandbox Code Playgroud)

php encryption

14
推荐指数
2
解决办法
3万
查看次数

标签 统计

php ×2

encryption ×1

hex ×1

string ×1