This is not about security. It is also not to make it hard to break. I'm looking for a simple algorithm to change a string (a url) in a way it does not resemble the original. The encryption will be done with javascript. Then I want to feed the encrypted string to a PHP function to change it back to the original. Both ends could share a secret key, or the conversions could be key-less and rely on just logic.
这与安全无关。它也不会让它难以打破。我正在寻找一种简单的算法来改变一个字符串(一个url),它的方式与原来的不一样。加密将使用javascript完成。然后我想将加密的字符串提供给PHP函数,将其更改回原始字符串。两端都可以共享一个密钥,或者转换可以是无钥匙并依赖于逻辑。
The ideal solution
理想的解决方案
- will be simple
- will use available javascript functions for encryption
- will use available php functions for decryption
- will produce encrypted string in way not to resemble the plain text at all
- will only use lower-case alphabet characters and numbers in the encrypted string
- is not a method widely used like Base64-ing as encryption.
会很简单
将使用可用的JavaScript函数进行加密
将使用可用的PHP函数进行解密
将产生加密的字符串,使其完全不像纯文本
只会在加密字符串中使用小写字母和数字
不像Base64-ing那样被广泛使用的方法作为加密。
Edit: The last requirement was added after shamittomar's answer.
编辑:在shamittomar的回答之后添加了最后一个要求。
5 个解决方案
#1
11
If that's what you want, you can Base64 encode and decode that.
如果这就是你想要的,你可以对其进行Base64编码和解码。
[EDIT]: After OP clarification:
[编辑]:OP澄清后:
As you do not want widely used methods, here is one rarely used method and that can do it for you by giving output only in LOWERCASE letters and NUMBERS. It is Base32 Encode/Decode. Use the following libraries:
由于您不希望广泛使用的方法,这里有一个很少使用的方法,并且可以通过仅以LOWERCASE字母和NUMBERS提供输出来为您完成。它是Base32编码/解码。使用以下库:
- Javascript Base32 Encoder: http://www.tumuski.com/2010/04/nibbler/
- PHP Base32 Decoder: http://php-classes.de/class/base32/
Javascript Base32编码器:http://www.tumuski.com/2010/04/nibbler/
PHP Base32解码器:http://php-classes.de/class/base32/
#2
24
You can use bitwise XOR in javascript to encode the string and again in PHP to decode it again. I wrote a little Javascript example for you. It works the same in PHP. If you call enc() a second time with the already encoded string, you'll get the original string again.
您可以在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>
In PHP do something like this (caution, this is not tested and it's been a long while since I did PHP):
在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;
#3
5
If it's not about security, and not about making it hard to break, then how about ROT-13
?
如果它不是关于安全性,而不是让它难以打破,那么ROT-13怎么样?
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/string/rot13 [rev. #1]
String.prototype.rot13 = function(){
return this.replace(/[a-zA-Z]/g, function(c){
return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
});
};
...
var s = "My String";
var enc = s.rot13(); // encrypted value in enc
PHP has a native function, str_rot13
: http://php.net/manual/en/function.str-rot13.php
PHP有一个本机函数,str_rot13:http://php.net/manual/en/function.str-rot13.php
$decrypted = str_rot13($_GET['whatever']);
#4
1
How are you planning to implement (hide) the secret in Javascript? IMHO it's not possible.
你打算如何在Javascript中实现(隐藏)秘密?恕我直言,这是不可能的。
Edit: OK - not about security.. then just use any baseXX or rot encoding mechanism. But you can't really say one of these algorythms would not be well known...
编辑:好的 - 不是关于安全性..然后只使用任何baseXX或rot编码机制。但你不能说这些算法中的一个不会众所周知......
#5
0
Well I found this page and found Redcully's program not work for me so I thought It happens with all others. finally I got reason and fixed it. Here new code is... Thanks to Redcully :)
好吧,我找到了这个页面,发现Redcully的程序不适合我,所以我认为它发生在所有其他人身上。最后我得到了理由并修复了它。这里的新代码是......感谢Redcully :)
JS function:
function encode(str) {
var encoded = "";
for (i=0; i<str.length;i++) {
var a = str.charCodeAt(i);
var b = a ^ 51; // bitwise XOR with any number, e.g. 123
encoded = encoded+String.fromCharCode(b);
}
return encoded;
}
PHP function:
function decode($encoded) {
$decoded = "";
for( $i = 0; $i < strlen($encoded); $i++ ) {
$b = ord($encoded[$i]);
$a = $b ^ 51; // <-- must be same number used to encode the character
$decoded .= chr($a);
}
return $decoded;
}
#1
11
If that's what you want, you can Base64 encode and decode that.
如果这就是你想要的,你可以对其进行Base64编码和解码。
[EDIT]: After OP clarification:
[编辑]:OP澄清后:
As you do not want widely used methods, here is one rarely used method and that can do it for you by giving output only in LOWERCASE letters and NUMBERS. It is Base32 Encode/Decode. Use the following libraries:
由于您不希望广泛使用的方法,这里有一个很少使用的方法,并且可以通过仅以LOWERCASE字母和NUMBERS提供输出来为您完成。它是Base32编码/解码。使用以下库:
- Javascript Base32 Encoder: http://www.tumuski.com/2010/04/nibbler/
- PHP Base32 Decoder: http://php-classes.de/class/base32/
Javascript Base32编码器:http://www.tumuski.com/2010/04/nibbler/
PHP Base32解码器:http://php-classes.de/class/base32/
#2
24
You can use bitwise XOR in javascript to encode the string and again in PHP to decode it again. I wrote a little Javascript example for you. It works the same in PHP. If you call enc() a second time with the already encoded string, you'll get the original string again.
您可以在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>
In PHP do something like this (caution, this is not tested and it's been a long while since I did PHP):
在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;
#3
5
If it's not about security, and not about making it hard to break, then how about ROT-13
?
如果它不是关于安全性,而不是让它难以打破,那么ROT-13怎么样?
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/string/rot13 [rev. #1]
String.prototype.rot13 = function(){
return this.replace(/[a-zA-Z]/g, function(c){
return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
});
};
...
var s = "My String";
var enc = s.rot13(); // encrypted value in enc
PHP has a native function, str_rot13
: http://php.net/manual/en/function.str-rot13.php
PHP有一个本机函数,str_rot13:http://php.net/manual/en/function.str-rot13.php
$decrypted = str_rot13($_GET['whatever']);
#4
1
How are you planning to implement (hide) the secret in Javascript? IMHO it's not possible.
你打算如何在Javascript中实现(隐藏)秘密?恕我直言,这是不可能的。
Edit: OK - not about security.. then just use any baseXX or rot encoding mechanism. But you can't really say one of these algorythms would not be well known...
编辑:好的 - 不是关于安全性..然后只使用任何baseXX或rot编码机制。但你不能说这些算法中的一个不会众所周知......
#5
0
Well I found this page and found Redcully's program not work for me so I thought It happens with all others. finally I got reason and fixed it. Here new code is... Thanks to Redcully :)
好吧,我找到了这个页面,发现Redcully的程序不适合我,所以我认为它发生在所有其他人身上。最后我得到了理由并修复了它。这里的新代码是......感谢Redcully :)
JS function:
function encode(str) {
var encoded = "";
for (i=0; i<str.length;i++) {
var a = str.charCodeAt(i);
var b = a ^ 51; // bitwise XOR with any number, e.g. 123
encoded = encoded+String.fromCharCode(b);
}
return encoded;
}
PHP function:
function decode($encoded) {
$decoded = "";
for( $i = 0; $i < strlen($encoded); $i++ ) {
$b = ord($encoded[$i]);
$a = $b ^ 51; // <-- must be same number used to encode the character
$decoded .= chr($a);
}
return $decoded;
}