Encryption and decryption algorithm for URL parameters (original)

Source: Internet
Author: User

      基本思路是:前端对参数进行加密,并经过urlrewriter进行地址改写传入后台,后台再进行解密。如:对这样一个url--????http://1.1.1.1:8080/a.do?param=1,加密后变为:http://1.1.1.1:8080/a.do?param=‘k230101io934jksd32r4‘,再经过urlrewriter转换可能变为http://1.1.1.1:8080/a/b/k230101io934jksd32r4 ?? ??

前端加密算法:

/** function: The URL encryption algorithm (only for window.location.href jump, not for post form submission and AJAX) * algorithm: For the value of the property exposed in the browser address bar is encrypted, such as a property is agentid=1,*       If the 1 encryption is K230101IO934JKSD32R4, the following:*      the first three bits is a random number;*       fourth to fifth bits the number of digits to convert the encrypted character to 16,*        such as: To encrypt the character to 15 to 16 in F, and 1 for the number of digits. The fourth to fifth bit is 01;*      sixth digit identifies the character to be encrypted, 0: pure number, 1: Character *        In the case of a mixture of characters and numbers, the non-encrypted;*      characters (letters and non-numbers are converted to ASC code) starting from the seventh bit of the 16-based conversion.;*       If the total number of characters after encryption is less than 20 bits, the random number is used to 20 bits, and if the 20 bits are exceeded, no random number is added. *      that is, the total number of digits after encryption is at least 20 bits. */function encode16 (str) {    str=str.tolowercase ();    if  ( Str.match (/^[-+]?\d*$/)  == null) {//non-integer, convert each character to 16, then stitch          var s=str.split ("");         var temp= "";         for (var i=0;i<s.length;i++) {            s[ I]=s[i].charcodeat ();//Convert first to Unicode encoding             s[ I]=s[i].tostring (;            temp=temp+s[i];)         }        return  temp+ "{" +1;//1 represents the character     }else{//the number is converted directly to 16 binary          Str=parseint (str). toString (+);     }    return str+ "{" +0;//0 stands for pure numbers }  function producerandom (n) {    var num= "" ";      for (var i=0;i<n;i++)      {          num+=math.floor (Math.random () *10);     }     return  num;}  //Primary Encryptionfunction Function encrypt (str) {    var encryptstr= "";//Final return of the encrypted string      encryptstr+=producerandom (3);//Generate 3-bit random numbers          var  Temp=encode16 (str). Split ("{");//convert the characters to be encrypted into 16 binary     var numlength=temp[0].length;// The converted character length is     numlength=numlength.tostring (16);//The character length is converted into 16 binary     if ( Numlength.length==1) {//If it is 1, fill a 0        numlength= "0" +numLength;     }else if (NUMLENGTH.LENGTH&GT;2) {//converted 16-character length if it is greater than 2 digits, then returned, not supported          return  "";    }    encryptstr+=numlength;          if (temp[1]== "0") {         encryptstr+=0;    }else if (temp[1]== "1") {         encryptstr+=1;    }         encryptstr+=temp[0];          if (encryptstr.length<20) {//if less than 20 bits, fill in the random number          var ran=producerandom (20-encryptstr.length);         encryptstr+=ran;    }    return encryptstr;}

Background decryption algorithm:

* Decrypt the inverse process for encryption */public static string decodevalue (String value) {    if ( Value.equals ("")) {        throw new nullpointerexception ();     }    if (Value.length () <20) {         throw new nullpointerexception ();     }    string  charlength=value.substring (3, 5);//How many bits of the encrypted character are there     int charlen= Integer.parseint (charlength,16);//Convert to 10 binary     int type=integer.parseint ( Value.substring (5, 6));//The type of the cryptographic character (0: number, 1: string)     string valueenc=value.substring ( 6, 6+charlen);//16 binary string     if (type==0) {         int truevalue=integer.parseint (valueenc,16);         return  string.valueof (truevalue);   &nbSp; }else{        stringbuffer sb=new stringbuffer ();         string[] valueencarray=valueenc.split ("");         for (int i=1;i<valueencarray.length;i+=2) {             int value10=integer.parseint (valueEncArray[i]+ valueencarray[i+1],16);//Convert to 10-binary ASC code              Sb.append (string.valueof (char) value10);//asc code converted to character         }         return sb.tostring ();     }}


Encryption and decryption algorithm for URL parameters (original)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.