在PHP中使用XOR加密/解密 [英] Encrypt/decrypt with XOR in PHP

查看:247
本文介绍了在PHP中使用XOR加密/解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



在我使用密钥进行XOR明文之后,我得到一个隐藏的010e010c15061b4117030f54060e54040e0642181b17,作为十六进制类型。如果我想从这个隐藏中得到明文,我应该在PHP中做什么?



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



这是代码:

  function xor_this($ string){

//让我们在这里定义我们的键
$ key ='fpt';

//我们的明文/密文
$ text = $ string;

//我们的输出文本
$ outText ='';

//通过每个字符迭代
($ 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 />'; //调试
}
}
return $ outText;
}

函数strToHex($ string)
{
$ hex =''; ($ i = 0; $ i< strlen($ string); $ i ++)
{
$ hex。= dechex(ord($ string [$ i]))
}
return $ hex;
}

函数hexToStr($ hex)
{
$ string ='';
($ i = 0; $ i< strlen($ hex)-1; $ i + = 2)
{
$ string。= chr(hexdec($ hex [$ i 。] $六角[$ I + 1]));
}
return $ string;
}

$ a =这是测试;
$ 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,'=================';

这是结果:


这是测试-------------



PHP注意:未初始化的字符串偏移量:29在C: \
用户\Administrator\Desktop\test.php第210行PHP堆栈跟踪:PHP
1. {main}()C:\Users\Administrator\Desktop\ test.php:0 PHP 2. xor_this()C:\Users\Administrator\Desktop\test.php:239



注意:未初始化的字符串偏移量: 29在
C:\Users\Administrator\Desktop\test.p hp on line 210



调用堆栈:
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++++++++



这是zs $ fs ☺=================


为什么? UBE ^A►WEAVA►WEAV@◄WEARAFWECWB+++++++是我在实际工作中遇到麻烦的结果。

解决方案

尝试这样:

  function xor_this($ string){

//让我们在这里定义我们的键
$ key =('magic_key');

//我们的明文/密文
$ text = $ string;

//我们的输出文本
$ outText ='';

//通过每个字符迭代
($ i = 0; $ i< strlen($ text);)
{
for($ j = 0 ; $($ j< strlen($ key)& $ i< strlen($ text)); $ j ++,$ i ++)
{
$ outText。= $ text {$ i} $键{附加$ J};
// echo'i ='。 $ i。 '''。 'j ='。 $ j。 '''。 $ outText {$ i}。 '< br />'; //调试
}
}
return $ outText;
}

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

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


I am studying encryption. And I got a problem like this:

After I XOR plaintext with a key, I get a crypt, "010e010c15061b4117030f54060e54040e0642181b17", as hex type. If I want to get plaintext from this crypt, what should I do in PHP?

I tried convert it to string/int and after that take them to XOR with the key (three letters). But it doesn't work.

This is the code:

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, '=================';

And this is the result:

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☺=================

Why? The "UBE^A►WEAVA►WEAV@◄WEARAFWECWB++++++++" is the result, which I got trouble in my real work.

解决方案

Try this:

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 . ', ' . 'j=' . $j . ', ' . $outText{$i} . '<br />'; // For debugging
        }
    }
    return $outText;
}

Basically to revert text back (even numbers are in) you can use the same function:

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

这篇关于在PHP中使用XOR加密/解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆