//1. Encryption and decryption methods using://1. Encryptionvarstr = ' 124 Chinese content '; varBase =NewBase64 (); varresult =Base.encode (str); //document.write (Result); //2. DecryptionvarRESULT2 =Base.decode (Result); document.write (RESULT2); //2. Encryption, decryption algorithm encapsulation:functionBase64 () {//Private Property_keystr = "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/="; //Public method for encoding This. Encode =function(input) {varOutput = ""; varchr1, CHR2, CHR3, Enc1, Enc2, enc3, Enc4; vari = 0; Input=_utf8_encode (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; } //Public method for decoding This. decode =function(input) {varOutput = ""; varchr1, CHR2, CHR3; varenc1, Enc2, enc3, Enc4; vari = 0; Input= Input.replace (/[^a-za-z0-9\+\/\=]/g, ""); while(I <input.length) {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! = 64) {Output= output +String.fromCharCode (CHR2); } if(Enc4! = 64) {Output= output +String.fromCharCode (CHR3); }} Output=_utf8_decode (output); returnoutput; } //Private method for UTF-8 encoding_utf8_encode =function(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; } //Private method for UTF-8 decoding_utf8_decode =function(utftext) {varString = ""; vari = 0; varc = C1 = C2 = 0; while(I <utftext.length) {c=utftext.charcodeat (i); if(C < 128) {string+=String.fromCharCode (c); I++; } Else if((C > 191) && (C < 224) ) {C2= Utftext.charcodeat (i+1); String+ = String.fromCharCode (((C &) << 6) | (C2 & 63)); I+ = 2; } Else{C2= Utftext.charcodeat (i+1); C3= Utftext.charcodeat (i+2); String+ = String.fromCharCode (((C &) << 12) | ((C2 &) << 6) | (C3 & 63)); I+ = 3; } } returnstring; } }
Turn from: 73784897
JavaScript implements Base64 encryption and decryption