2017-01-09 80 views
0

我想加密所有链接如下所示的URL GET参数。春季加密和解密URL参数

在HTML加密:

home.htm?ecryptParam=aajsbjabsjvyhasyayasy 

实际:

home.htm?fName=samir&lName=vora 

同时,在控制器类会自动拥有解密值,我可以从请求检索它。例如:

链接:

home.htm?ecrypt=SKJIIU86iuGkJGkJFHGBVBVn 

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

+1

dont't做heavylifting,使用https和请求和响应将被加密和由该服务器解密。 – ares

+0

好吧,然后告诉它是如何工作给我的例子,但我们怎么可能会这样 –

+0

谷歌如何配置https <您正在使用的服务器> – ares

回答

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")));