Using JS to encrypt form forms base64

Source: Internet
Author: User

JS can be used to encrypt the foreground data to prevent malicious access, the following code, with Base64 data encryption, can be decrypted in the background.

Introduction of JS

<script type= "Text/javascript" src= "jquery.min.js" ></script><script type= "Text/javascript" src= " Jquery.base64.js "></script>

  

The data in the HTML

Name:<inputtype= "text"ID= "Name"  ><BR/>Password:<inputtype= "Password"ID= "Password" ><BR/><Buttononclick= "SubmitData ()">Submit</Button>

JS Processing

<script>functionSubmitData () {varobj =NewObject (); Obj.name= $ (' #name '). Val (); Obj.password= $ (' #password '). Val (); //converted to JSON after encryptionvarstr =$.base64.encode (json.stringify (obj)); $.post ("Your URL address", {data:str},function(data) {Console.log (data);    }); }    //extended base64, support ChineseJquery.base64 = (function($) {        //Private Property        varKeystr = "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/="; //Private method for UTF-8 encoding        functionUtf8encode (String) {string= String.Replace (/\r\n/g, "\ n"); varUtftext = "";  for(varn = 0; n < string.length; n++) {                varc =string.charcodeat (n); if(C < 128) {Utftext+=String.fromCharCode (c); }                Else if((C > 127) && (C < 2048) ) {Utftext+ = String.fromCharCode ((c >> 6) | 192); Utftext+ = String.fromCharCode ((C & 63) | 128); }                Else{Utftext+ = String.fromCharCode ((c >> 12) | 224); Utftext+ = String.fromCharCode (((c >> 6) & 63) | 128); Utftext+ = String.fromCharCode ((C & 63) | 128); }            }            returnUtftext; }        functionencode (input) {varOutput = ""; varchr1, CHR2, CHR3, Enc1, Enc2, enc3, Enc4; vari = 0; Input=Utf8encode (input);  while(I <input.length) {chr1= Input.charcodeat (i++); CHR2= Input.charcodeat (i++); CHR3= 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 = 64; } Else if(IsNaN (CHR3)) {Enc4= 64; } Output= output +Keystr.charat (ENC1)+ Keystr.charat (ENC2) +Keystr.charat (enc3)+Keystr.charat (ENC4); }            returnoutput; }        return{encode:function(str) {returnencode (str);    }        };    } (JQuery)); </script>

Using JS to encrypt form forms base64

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.