JS implementation Base64 encryption and decryption

Source: Internet
Author: User
Tags 0xc0 base64 encode

JS implementation Base64 encryption and decryption
Reprinted from Http://blog.csdn.net/fengzheng0306/archive/2006/04/25/676055.aspx

Method One:

<HTML>
<HEAD>
<TITLE>Base64</TITLE>
<script language=javascript>
var base64encodechars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64decodechars = new Array (
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 62,-1,-1,-1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,-1,-1,-1,-1,-1,-1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,-1,-1,-1,-1,-1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,-1,-1,-1,-1,-1);

<HTML>
<HEAD>
<TITLE>Base64</TITLE>
<script language=javascript>
var base64encodechars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64decodechars = new Array (
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 62,-1,-1,-1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,-1,-1,-1,-1,-1,-1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,-1,-1,-1,-1,-1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,-1,-1,-1,-1,-1);
function Base64Encode (str) {
var out, I, Len;
var C1, C2, C3;
len = str.length;
i = 0;
out = "";
while (I < Len) {
C1 = Str.charcodeat (i++) & 0xFF;
if (i = = len)
{
Out + = Base64encodechars.charat (C1 >> 2);
Out + = Base64encodechars.charat ((C1 & 0x3) << 4);
Out + = "=";
Break
}
C2 = Str.charcodeat (i++);
if (i = = len)
{
Out + = Base64encodechars.charat (C1 >> 2);
Out + = Base64encodechars.charat (((C1 & 0x3) << 4) | ((C2 & 0xF0) >> 4));
Out + = Base64encodechars.charat ((C2 & 0xF) << 2);
Out + = "=";
Break
}
C3 = Str.charcodeat (i++);
Out + = Base64encodechars.charat (C1 >> 2);
Out + = Base64encodechars.charat (((C1 & 0x3) << 4) | ((C2 & 0xF0) >> 4));
Out + = Base64encodechars.charat (((C2 & 0xF) << 2) | ((C3 & 0xC0) >>6));
Out + = Base64encodechars.charat (C3 & 0x3F);
}
return out;
}
function Base64decode (str) {
var C1, C2, C3, C4;
var i, Len, out;
len = str.length;
i = 0;
out = "";
while (I < Len) {
/* C1 */
do {
C1 = Base64decodechars[str.charcodeat (i++) & 0xFF];
} while (I < len && C1 = =-1);
if (C1 = =-1)
Break
/* C2 */
do {
C2 = Base64decodechars[str.charcodeat (i++) & 0xFF];
} while (I < len && C2 = =-1);
if (C2 = =-1)
Break
Out + = String.fromCharCode ((C1 << 2) | ((C2 & 0x30) >> 4));
/* C3 */
do {
C3 = Str.charcodeat (i++) & 0xFF;
if (C3 = = 61)
return out;
C3 = Base64decodechars[c3];
} while (I < Len && C3 = =-1);
if (C3 = =-1)
Break
Out + = String.fromCharCode (((C2 & 0XF) << 4) | ((C3 & 0x3C) >> 2));
/* C4 */
do {
C4 = str.charcodeat (i++) & 0xFF;
if (C4 = = 61)
return out;
C4 = base64decodechars[c4];
} while (I < len && C4 = =-1);
if (C4 = =-1)
Break
Out + = String.fromCharCode (((C3 & 0x03) << 6) | c4);
}
return out;
}
function Utf16to8 (str) {
var out, I, Len, C;
out = "";
len = str.length;
for (i = 0; i < len; i++) {
c = str.charcodeat (i);
if ((c >= 0x0001) && (c <= 0x007F)) {
Out + = Str.charat (i);
} else if (C > 0x07ff) {
Out + = String.fromCharCode (0xE0 | ((c >> b) & 0x0F));
Out + = String.fromCharCode (0x80 | ((c >> 6) & 0x3F));
Out + = String.fromCharCode (0x80 | ((c >> 0) & 0x3F));
} else {
Out + = String.fromCharCode (0xC0 | ((c >> 6) & 0x1F));
Out + = String.fromCharCode (0x80 | ((c >> 0) & 0x3F));
}
}
return out;
}
function Utf8to16 (str) {
var out, I, Len, C;
var char2, Char3;
out = "";
len = str.length;
i = 0;
while (I < Len) {
c = str.charcodeat (i++);
Switch (c >> 4)
{
Case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:
0xxxxxxx
Out + = Str.charat (i-1);
Break
Case 12:case 13:
110x xxxx 10xx xxxx
CHAR2 = Str.charcodeat (i++);
Out + = String.fromCharCode (((C & 0x1F) << 6) | (Char2 & 0x3F));
Break
Case 14:
1110 xxxx 10xx xxxx 10xx xxxx
CHAR2 = Str.charcodeat (i++);
CHAR3 = Str.charcodeat (i++);
Out + = String.fromCharCode (((C & 0x0F) << 12) |
((Char2 & 0x3F) << 6) |
((Char3 & 0x3F) << 0));
Break
}
}
return out;
}

function doit () {
var f = document.f
F.output.value = Base64Encode (Utf16to8 (F.source.value))
F.decode.value = Utf8to16 (Base64decode (F.output.value))
}
</script>
</HEAD>
<BODY>
<H1>Base64</H1>
<form name= "F" >
Original Code <BR>
<textarea name= "source" rows=4 cols=60 wrap= "soft" ></TEXTAREA><BR><BR>
Base64 encode<br>
<textarea name= "Output" rows=4 cols=60 wrap= "soft" ></TEXTAREA><BR><BR>
Base64 decode<br>
<textarea name= "decode" rows=4 cols=60 wrap= "soft" ></TEXTAREA><BR><BR>
<input Type=button value= "Convert" onclick= "doit ()" >
</FORM>
</BODY>

Method Two:

/**** Base64 encode/decode** @author haitao.tu* @date 2010-04-26* @email [email protected]**/function Base6 4 () {//Private PROPERTY_KEYSTR = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/=";//Public Method for encodingthis.encode = function (input) {var output = ""; var chr1, CHR2, CHR3, Enc1, Enc2, enc3, Enc4;var i = 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 =;} else if (IsNaN (CHR3)) {Enc4 = 64;} Output = output +_keystr.charat (enc1) + _keystr.charat (ENC2) +_keystr.charat (enc3) + _keystr.charat (ENC4);} return output;} Public method for Decodingthis.decode = function (input) {var output = ""; var chr1, CHR2, Chr3;var enc1, Enc2, enc3, en C4;var i = 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! =) {output = output + String.fromCharCode (CHR2);} if (enc4! =) {output = output + String.fromCharCode (CHR3);}} Output = _utf8_decode (output); return output;} Private method for UTF-8 Encoding_utf8_encode = function (string) {string = String.Replace (/\r\n/g, "\ n"); var utftext = ""; for (var n = 0; n < string.length;  n++) {var c = string.charcodeat (n), if (C < +) {Utftext + = String.fromCharCode (c);} else if ((C > 127) && (c < 2048)) {Utftext + = String.fromCharCode ((c >> 6) | 192); Utftext + = String.fromCharCode ((c & N) |);} EL Se{Utftext + = String.fromCharCode ((c >> b) | 224) Utftext + = String.fromCharCode ((((c >> 6) & ()); UTF Text + = String.fromCharCode ((C & 63) | 128);} }return Utftext;}  Private method for UTF-8 Decoding_utf8_decode = function (utftext) {var string = ""; var i = 0;var c = C1 = C2 = 0;while (I < Utftext.length)  {c = utftext.charcodeat (i), if (C < +) {string + = String.fromCharCode (c); i++;} else if ((C > 191) && (C < 224) {C2 = Utftext.charcodeat (i+1); string + = String.fromCharCode (((C & D) << 6) | (C2 & 63)); i + = 2;}  else {c2 = Utftext.charcodeat (i+1); C3 = Utftext.charcodeat (i+2); string + = String.fromCharCode (((C & D) << 12) | ((C2 &) << 6) | (C3 & 63)); i + = 3;}} return string;}}

<!DOCTYPE HTML>
<Html>
<Head>
<MetaCharSet= "Utf-8">
<Title&GT;BASE64 Encryption and decryption</Title>
<ScriptType= "Text/javascript">
VarB=NewBase64 ();
VarStr=B.encode ("Admin:admin");
Alert"Base64 encode:"+STR);
Decrypt
Str=B.decode (str);
Alert"Base64 Decode:"+STR);
</Script>
</Head>

<Body>
</Body>
</Html>

Method Three:
/**
 *
* Btoa (): string or binary value converted to BASE64 encoding
* Atob (): BASE64 encoding to original encoding
  */
var string = ' Hello world! ';
Console.log (Btoa (String))//"SGVSBG8GV29YBGQH"
Console.log (Atob (' SGVSBG8GV29YBGQH '))//"Hello world!"

function B64encode (str) {
return Btoa (encodeURIComponent (str));
  }

function B64decode (str) {
return decodeURIComponent (Atob (str));
  }
Console.log (B64encode (' Hello world! Hello! '))
Console.log (B64decode (' sgvsbg8lmjbxb3jszcelmjalrtqlqkqlqtalrtulqtulqkqlruylqkmlode= '))

JS implementation Base64 encryption and decryption

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.