简单的 Javascript 加密,PHP 使用共享密钥解密

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3609005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:25:16  来源:igfitidea点击:

Simple Javascript encrypt, PHP decrypt with shared secret key

phpjavascriptalgorithmencryption

提问by Majid Fouladpour

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 encryptionwill be done with javascript. Then I want to feed the encryptedstring 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.

这与安全无关。也不是让它难以打破。我正在寻找一种简单的算法来以一种与原始字符串不同的方式更改字符串(网址)。该加密会用JavaScript来完成。然后我想将加密的字符串提供给 PHP 函数以将其更改回原始字符串。两端可以共享一个秘密密钥,或者转换可以是无密钥的并且只依赖于逻辑。

The ideal solution

理想的解决方案

  1. will be simple
  2. will use available javascript functions for encryption
  3. will use available php functions for decryption
  4. will produce encrypted string in way not to resemble the plain text at all
  5. will only use lower-case alphabet characters and numbers in the encrypted string
  6. is not a method widely used like Base64-ing as encryption.
  1. 会很简单
  2. 将使用可用的 javascript 函数进行加密
  3. 将使用可用的 php 函数进行解密
  4. 将以完全不类似于纯文本的方式生成加密字符串
  5. 将只在加密字符串中使用小写字母字符和数字
  6. 不是像 Base64-ing 那样广泛使用的加密方法。

Edit: The last requirement was added after shamittomar's answer.

编辑:最后一个要求是在shamittomar的回答之后添加的。

采纳答案by shamittomar

If that's what you want, you can Base64encode and decode that.

如果这是您想要的,您可以对它进行Base64编码和解码。

[EDIT]: After OP clarification:

[编辑]:在 OP 澄清后:

As you do not want widely used methods, here is one rarelyused 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:

由于您不想要广泛使用的方法,这里是一种很少使用的方法,它可以通过仅以小写字母和数字给出输出来为您完成。它是Base32 编码/解码。使用以下库:

  • Javascript Base32 编码器:http://www.tumuski.com/2010/04/nibbler/
  • PHP Base32 解码器:https://www.phpclasses.org/package/3484-PHP-Encode-and-decode-data-with-MIME-base-32-encoding.html

回答by Ridcully

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;

回答by Fosco

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_rot13http: //php.net/manual/en/function.str-rot13.php

$decrypted = str_rot13($_GET['whatever']);

回答by Jan.

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 编码机制。但是你真的不能说这些算法之一不会为人所知......

回答by Manav Akela

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:

JS函数:

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:

PHP函数:

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