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

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

问题描述

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

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

在我用密钥对明文进行异或之后,我得到一个 crypt,010e010c15061b4117030f54060e54040e0642181b17",作为十六进制类型.如果我想从这个 crypt 中获取明文,我应该在 PHP 中做什么?

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.

这是代码:

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 注意:未初始化的字符串偏移量:C: 中的 29UsersAdministratorDesktop est.php on line 210 PHP Stack trace: PHP1. {main}() C:UsersAdministratorDesktop est.php:0 PHP 2. xor_this() C:UsersAdministratorDesktop est.php:239

PHP Notice: Uninitialized string offset: 29 in C: UsersAdministratorDesktop est.php on line 210 PHP Stack trace: PHP 1. {main}() C:UsersAdministratorDesktop est.php:0 PHP 2. xor_this() C:UsersAdministratorDesktop est.php:239

注意:未初始化的字符串偏移量:29 inC:UsersAdministratorDesktop est.p hp on line 210

Notice: Uninitialized string offset: 29 in C:UsersAdministratorDesktop est.p hp on line 210

调用栈:0.0005 674280 1. {main}() C:UsersAdministratorDesktop est.php:00.0022 674848 2. xor_this() C:UsersAdministratorDesktop est.php:23 9

Call Stack: 0.0005 674280 1. {main}() C:UsersAdministratorDesktop est.php:0 0.0022 674848 2. xor_this() C:UsersAdministratorDesktop est.php:23 9

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

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

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

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

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

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

推荐答案

试试这个:

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;
}

基本上要恢复文本(偶数在),您可以使用相同的功能:

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

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

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