您可以在javascript中使用按位XOR对字符串进行编码,并在PHP中再次对其进行解码。我为你写了一个小的Javascript示例。它在PHP中的工作方式相同。如果使用已编码的字符串再次调用 enc(),则将再次获得原始字符串。
<html>
<head><title></title></head>
<body>
<script type="text/javascript">
function enc(str) {
var encoded = "";
for (i=0; i<str.length;i++) {
var a = str.charCodeAt(i);
var b = a ^ 123; // bitwise XOR with any number, e.g. 123
encoded = encoded+String.fromCharCode(b);
}
return encoded;
}
var str = "hello world";
var encoded = enc(str);
alert(encoded); // shows encoded string
alert(enc(encoded)); // shows the original string again
</script>
</body>
</html>
在PHP中做这样的事情(注意,这没有经过测试,而且我已经很久没有做PHP了):
$encoded = "..."; // <-- encoded string from the request
$decoded = "";
for( $i = 0; $i < strlen($encoded); $i++ ) {
$b = ord($encoded[$i]);
$a = $b ^ 123; // <-- must be same number used to encode the character
$decoded .= chr($a)
}
echo $decoded;