PHP中的Xor加密 [英] Xor encryption in PHP

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

问题描述

我不熟悉Xor加密,并且在以下代码方面遇到了一些麻烦:

I'm new to Xor encryption, and I'm having some trouble with the following code:

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

当我运行它时,它适用于普通字符串,例如'dog',但仅部分适用于包含数字的字符串,例如'12345'.

When I run this it works for normal strings, like 'dog' but it only partially works for strings containing numbers, like '12345'.

演示...

xor_this('dog') ='UYV'

xor_this('123') =''

有趣的是,正如我期望的那样, xor_this(xor_this('123')) ='123'.我很确定问题出在我对按位运算符的不稳定理解中,或者可能是PHP处理包含数字的字符串的方式中的某个问题.我敢打赌,那里有人聪明地知道这里到底出了什么问题.谢谢.

It's also interesting to note that xor_this( xor_this('123') ) = '123', as I expect it to. I'm pretty sure the problem resides somewhere in my shaky understanding of bitwise operators, OR possibly the way PHP handles strings that contain numbers. I'm betting there's someone clever out there that knows exactly what's wrong here. Thanks.

编辑#1:它不是真正的加密".我想混淆是正确的术语,这就是我在做什么.我需要传递一个包含用户不重要数据的代码,而他们却不能轻易对其进行篡改.他们正在离线完成定时活动,并通过此代码将时间提交给在线记分板.离线活动将混淆其时间(以毫秒为单位).我需要编写一个脚本来接收此代码,然后将其转回包含其时间的字符串.

EDIT #1: It's not truly 'encryption'. I guess obfuscation is the correct term, which is what I'm doing. I need to pass a code containing unimportant data from a user without them being able to easily tamper with it. They're completing a timed activity off-line and submitting their time to an online scoreboard via this code. The off-line activity will obfuscate their time (in milliseconds). I need to write a script to receive this code and turn it back into the string containing their time.

推荐答案

尽管有所有明智的建议,我还是用一种更简单的方法解决了这个问题:

Despite all the wise suggestions, I solved this problem in a much simpler way:

我改变了钥匙!事实证明,通过将密钥更改为更多类似这样的内容:

I changed the key! It turns out that by changing the key to something more like this:

$ key ='ISINUS0478331006';

...它将生成模糊的可打印字符输出.

...it will generate an obfuscated output of printable characters.

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

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