在PHP中使用XOR加密/解密

我正在学习加密我有一个这样的问题:

在我的XOR明文带有一个密钥后,我得到一个地址,“010e010c15061b4117030f54060e54040e0642181b17”,作为十六进制类型.如果我想从这个地下室得到明文,我该怎么办PHP

我尝试将其转换为字符串/ 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 = '';
    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;
}

$a = "This is the test";
$b = xor_this($a);
echo xor_this($b),'-------------';
//
$c = strToHex($b);
$e = xor_this($c);
echo $e,'++++++++';
//
$d = hexToStr($c);
$f = xor_this($d);
echo $f,'=================';

这就是结果:

This is the test————-

PHP Notice: Uninitialized string offset: 29 in C:\
Users\Administrator\Desktop\test.PHP on line 210 PHP Stack trace: PHP
1. {main}() C:\Users\Administrator\Desktop\test.PHP:0 PHP 2. xor_this() C:\Users\Administrator\Desktop\test.PHP:239

Notice: Uninitialized string offset: 29 in
C:\Users\Administrator\Desktop\test.p hp on line 210

Call Stack:
0.0005 674280 1. {main}() C:\Users\Administrator\Desktop\test.PHP:0
0.0022 674848 2. xor_this() C:\Users\Administrator\Desktop\test.PHP:23 9

UBE^A►WEAVA►WEAV@◄WEaraFWECWB++++++++

This is zs$fs☺=================

为什么? “UBE ^A►WEAVA►WEAV@◄WEaraFWECWB”是我在实际工作中遇到麻烦的结果.

尝试这个:
function xor_this($string) {

    // Let's define our key here
    $key = ('magic_key');

    // 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) && $i<strlen($text)); $j++,$i++)
        {
            $outText .= $text{$i} ^ $key{$j};
            //echo 'i=' . $i . ',' . $outText{$i} . '<br />'; // For debugging
        }
    }
    return $outText;
}

基本上还原文本(甚至数字都在),您可以使用相同的功能

$textToObfuscate = "Some Text 12345";
$obfuscatedText = xor_this($textToObfuscate);
$restoredText = xor_this($obfuscatedText);

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...