JS URL using base64 encryption and decryption

Source: Internet
Author: User
Tags decrypt

JS Encoding method:
<script type= "Text/javascript" >document.write (encodeURI ("Http://www.w3school.com.cn/My first/") + "<BR/ > ")//Compile document.write (decodeURI (" Http://www.w3school.com.cn/My first/") +" <br/> ")//Decompile document.write ( encodeURI (",/?:@&=+$#") + "<br/>") document.write (encodeURIComponent ("Http://www.w3school.com.cn/My first/") +" <br/> ") document.write (Escape (" Http://www.w3school.com.cn/My first/123 ") +" <br/> ")// Compile document.write (unescape ("Http://www.w3school.com.cn/My first/123") + "<br/>")//anti-compilation </script>

  


<script type= "Text/javascript" ><!--var keystr = "Abcdefghijklmnop" + "qrstuvwxyzabcdef" + "GHIJKLMNOPQRSTUV" + "wxyz0123456789+/" + "=";//Cryptographic function encode64 (input) {input = Escape (input),//note escape () function var output = "", var chr1, CHR2, CHR3 = ""; var enc1, Enc2, enc3, Enc4 = ""; var i = 0;do {chr1 = Input.charcodeat (i++); chr2 = Input.charcodeat (i++); ch R3 = Input.charcodeat (i++); enc1 = chr1 >> 2;enc2 = ((Chr1 & 3) << 4) | (CHR2 >> 4); enc3 = ((CHR2 &) << 2) | (CHR3 >> 6); Enc4 = Chr3 & 63;if (IsNaN (CHR2)) {enc3 = Enc4 =;} else if (IsNaN (CHR3)) {Enc4 = 64;} Output = output +keystr.charat (enc1) +keystr.charat (ENC2) +keystr.charat (enc3) +keystr.charat (ENC4); chr1 = CHR2 = CHR3 = " "; enc1 = Enc2 = enc3 = Enc4 =" ";} while (I < input.length); return output;} Decryption function decode64 (input) {var output = "", var chr1, chr2, chr3 = ""; var enc1, Enc2, enc3, Enc4 = ""; var i = 0;//re Move all characters that is not A-Z, A-Z, 0-9, +,/, or =var base64Test =/[^a-za-z0-9\+\/\=]/g;if (base64test.exec (input)) {alert ("There were invalid base64 characters in the input text.\n "+" Valid base64 characters is a-Z, A-Z, 0-9, ' + ', '/', and ' = ' \ n ' + "Expect errors in decoding.");} input = Input.replace (/[^a-za-z0-9\+\/\=]/g, "");d o {enc1 = Keystr.indexof (Input.charat (i++)); Enc2 = Keystr.indexof ( Input.charat (i++)) enc3 = Keystr.indexof (Input.charat (i++)); Enc4 = Keystr.indexof (Input.charat (i++)); chr1 = (enc1 << 2) | (Enc2 >> 4); chr2 = ((Enc2 &) << 4) | (enc3 >> 2); chr3 = ((enc3 & 3) << 6) | Enc4;output = output + String.fromCharCode (CHR1), if (enc3! =) {output = output + String.fromCharCode (CHR2);} if (enc4! =) {output = output + String.fromCharCode (CHR3);} CHR1 = CHR2 = CHR3 = ""; enc1 = Enc2 = enc3 = Enc4 = "";} while (I < input.length); return unescape (output);//note unescape () function}//--></script>

  

When we encrypt data using the ENCODE64 (input) function, we cannot invoke the JS decoder (input) function when we want to decrypt the data in Java code, and we must use a function written in the Java language.

The Org.apache.commons.codec.binary.Base64 package from the Commons-codec-1.3.jar class library provided by Apache provides the ENCODEBASE64 (byte[] BTS) and DecodeBase64 (byte[] BTS) method to achieve the data Base64 () encryption, but with the above JS code encryption and decryption function is not one by one corresponding, for example implementation with JS code encryption, with Java function decryption, we must call another Java function, Unescape (String src) with the following code:

private static String unescape (string src) {stringbuffer tmp = new StringBuffer ();           Tmp.ensurecapacity (Src.length ());           int lastpos = 0, pos = 0;           Char ch;               while (Lastpos < Src.length ()) {pos = Src.indexof ("%", lastpos); if (pos = = Lastpos) {if (Src.charat (pos + 1) = = ' u ') {ch = (char) Integer.parsei                       NT (src. SUBSTRING (pos + 2, pos + 6), 16);                       Tmp.append (CH);                   Lastpos = pos + 6; } else {ch = (char) integer.parseint (src. substring (pos + 1, pos + 3)                       , 16);                       Tmp.append (CH);                   Lastpos = pos + 3;                       }} else {if (pos = =-1) {Tmp.append (src.substring (Lastpos));                   Lastpos = Src.length (); } else {tmp.append (src.substring (Lastpos, POS));                   Lastpos = pos;       }}} return Tmp.tostring (); }

The

corresponds to the unescape () function in the JS code to decrypt the data information correctly, and the decryption method is:

public static string Decode64 (String encode) {//Call Org.apache.commons.codec.binary.Base64 package, in Commons-codec-1.3.jar  Base64 base64 = new Base64 ();  byte[] Byteofencode = Encode.getbytes ();  byte[] Byteofdecode = base64.decodebase64 (Byteofencode);//Call decodeBase64 method String decode = new string (Byteofdecode); Return unescape (decode);//Call unescape (String src) method} appended: The Escape () method written in Java found online: public class Escapeunescape {p           Ublic static string Escape (string src) {int i;           Char J;           StringBuffer tmp = new StringBuffer ();           Tmp.ensurecapacity (Src.length () * 6);               for (i = 0; i < src.length (); i++) {j = Src.charat (i); if (Character.isdigit (j) | | | Character.islowercase (j) | |               Character.isuppercase (j)) Tmp.append (j);                   else if (J < n) {tmp.append ("%");                   if (J < tmp.append) ("0"); TMp.append (Integer.tostring (J, 16));                   } else {tmp.append ("%u");               Tmp.append (Integer.tostring (J, 16));       }} return tmp.tostring (); }

ASP. base64 encryption and Decryption methods:

<summary>///Encrypt Summary description///</summary>public class encrypt{public    Encrypt ()    {        /        // /TODO: Add constructor logic here/        /}///<summary>//    Base64 encryption with UTF8 encoding///    </summary> //    <param name= "source" > PlainText to be encrypted </param>//    <returns> encrypted string </returns>    public static string EncodeBase64 (string source)    {       return convert.tobase64string ( Encoding.Default.GetBytes (source));    }    <summary>//Base64 decryption///    </summary>/    <param name= "result" > ciphertext to be decrypted </ Param>    //<returns> decrypted string </returns> public    Static Strings DecodeBase64 (string result)    {        return Encoding.Default.GetString (convert.frombase64string (Result));}    }

  

JS URL using base64 encryption and decryption

Related Article

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.