代码之家  ›  专栏  ›  技术社区  ›  Majid Fouladpour

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

  •  13
  • Majid Fouladpour  · 技术社区  · 14 年前

    加密的

    理想溶液

    1. 很简单
    2. 将使用可用的javascript函数进行加密
    3. 将使用可用的php函数进行解密
    4. 将以与纯文本完全不同的方式生成加密字符串
    5. 将仅在加密字符串中使用小写字母字符和数字
    6. 不是像加密那样广泛使用的方法。

    shamittomar 的答案。

    5 回复  |  直到 8 年前
        1
  •  12
  •   shamittomar    6 年前

    如果那是你想要的,你可以 比较基准64

    【编辑】:OP澄清后:

    使用的方法,它可以为您提供仅小写字母和数字的输出。它是 Base32 Encode/Decode . 使用以下库:

        2
  •  29
  •   Ridcully    10 年前

    您可以在javascript中使用按位异或对字符串进行编码,然后在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;
    
        3
  •  6
  •   Fosco    14 年前

    如果不是为了安全,也不是为了让它很难被打破,那么 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
    

    str_rot13 : http://php.net/manual/en/function.str-rot13.php

    $decrypted = str_rot13($_GET['whatever']);
    
        4
  •  2
  •   Manav Akela    7 年前

    好吧,我发现这个页面,发现Redcully的程序不适合我,所以我认为它发生在所有其他人。最后我找到了原因,把它修好了。新代码是。。。 感谢Redcully:)

    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 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;
    }
    
        5
  •  1
  •   Jan.    14 年前

    你打算如何在Javascript中实现(隐藏)这个秘密?我觉得这不可能。