2017-01-09 125 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")));