加密&在春天解密URL参数

时间:2017-01-09 12:27:02

标签: javascript spring-mvc

我想加密URL GET参数,所有链接都将如下所示。

在html中加密:

home.htm?ecryptParam=aajsbjabsjvyhasyayasy

实际值:

home.htm?fName=samir&lName=vora

同时,在控制器类中,它将自动获得解密值,我可以从请求中检索它。 e.g:

链接:

home.htm?ecrypt=SKJIIU86iuGkJGkJFHGBVBVn

控制器类: request.getParameter("fName");将返回samir

2 个答案:

答案 0 :(得分:0)

Samir,如果您在网址中将值作为加密格式传递,并且您将使用请求参数作为加密格式进行检索,并且您需要解密这些值,请参阅此链接“http://www.code2learn.com/2011/06/encryption-and-decryption-of-data-using.html”。这可能有所帮助。

答案 1 :(得分:0)

虽然你真的应该处理安全后端,但是在JavaScript中很容易实现简单的文本混淆。

以下是一个例子:

//Encrypter class
var stringEncrypter = (function() {
  function stringEncrypter() {}
  stringEncrypter.encrypt = function(str) {
    str = str.toString();
    var returnCode = [];
    for (var strIndex = 0; strIndex < str.length; strIndex++) {
      var element = str[strIndex];
      //Push with bitwise offset (or remove the bitwise offset here and in decrypt)
      returnCode.push(element.charCodeAt(0) << this.off);
    }
    //return a joined string
    return returnCode.join(this.splitter);
  };
  stringEncrypter.decrypt = function(str) {
    var list = str.split(this.splitter);
    var returnCode = "";
    for (var strIndex = 0; strIndex < list.length; strIndex++) {
      var element = list[strIndex];
      //Push with bitwise offset (or remove the bitwise offset here and in encrypt)
      returnCode += String.fromCharCode(parseInt(element) >> this.off);
    }
    return returnCode;
  };
  stringEncrypter.encryptUrl = function(url) {
    if (url.substr(url.indexOf("?") >= 0)) {
      var str = url.substr(url.indexOf("?") + 1);
      if (str.lastIndexOf("#") >= 0) {
        str = str.substr(0, str.lastIndexOf("#"));
      }
      var params = str.split("&");
      for (var paramIndex = 0; paramIndex < params.length; paramIndex++) {
        var param = params[paramIndex].split("=");
        param[0] = this.encrypt(param[0]);
        param[1] = this.encrypt(param[1]);
        params[paramIndex] = param.join("=");
      }
      url = url.substr(0, url.indexOf("?") + 1) + params.join("&") +
        (url.lastIndexOf("#") >= 0 ? url.substr(url.lastIndexOf("#")) : "");
    }
    return url;
  };
  stringEncrypter.decryptUrl = function(url) {
    if (url.substr(url.indexOf("?") >= 0)) {
      var str = url.substr(url.indexOf("?") + 1);
      if (str.lastIndexOf("#") >= 0) {
        str = str.substr(0, str.lastIndexOf("#"));
      }
      var params = str.split("&");
      for (var paramIndex = 0; paramIndex < params.length; paramIndex++) {
        var param = params[paramIndex].split("=");
        param[0] = this.decrypt(param[0]);
        param[1] = this.decrypt(param[1]);
        params[paramIndex] = param.join("=");
      }
      url = url.substr(0, url.indexOf("?") + 1) + params.join("&") +
        (url.lastIndexOf("#") >= 0 ? url.substr(url.lastIndexOf("#")) : "");
    }
    return url;
  };
  return stringEncrypter;
}());
//Bitwise offset. Completely optional
stringEncrypter.off = 2;
stringEncrypter.splitter = "|";
//Encrypt a string
console.log(stringEncrypter.encrypt("Testing123"));
//Decrypt a string
console.log(stringEncrypter.decrypt(stringEncrypter.encrypt("Testing123")));
//Decrypt a url
console.log(stringEncrypter.encryptUrl("www.test.dk?test=45"));
//Encrypt a url
console.log(stringEncrypter.decryptUrl(stringEncrypter.encryptUrl("www.test.dk?test=45")));
//Decrypt a url with #
console.log(stringEncrypter.encryptUrl("www.test.dk?test=45#title1"));
//Encrypt a url with #
console.log(stringEncrypter.decryptUrl(stringEncrypter.encryptUrl("www.test.dk?test=45#title1")));