Javascript base64 encoding and decoding

Source: Internet
Author: User

<HTML>

<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> base64 </title>
</Head>

<Body>
<Script language = "JavaScript">
VaR keystr = "abcdefghijklmnop" +
"Qrstuvwxyzabcdef" +
"Ghijklmnopqrstuv" +
"Wxyz0123456789 +/" +
"= ";

Function encode64 (input ){
Input = escape (input );
VaR output = "";
VaR chr1, chr2, chr3 = "";
VaR enc1, enc2, enc3, enc4 = "";
VaR I = 0;

Do {
Chr1 = input. charcodeat (I ++ );
Chr2 = input. charcodeat (I ++ );
Chr3 = input. charcodeat (I ++ );

Enc1 = chr1> 2;
Enc2 = (chr1 & 3) <4) | (chr2> 4 );
Enc3 = (chr2 & 15) <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 );
Chr1 = chr2 = chr3 = "";
Enc1 = enc2 = enc3 = enc4 = "";
} While (I <input. Length );

Return output;
}

Function decode64 (input ){
VaR output = "";
VaR chr1, chr2, chr3 = "";
VaR enc1, enc2, enc3, enc4 = "";
VaR I = 0;

// Remove all characters that are not A-Z, A-Z, 0-9, +,/, OR =
VaR base64test =/[^ A-Za-z0-9/+ // =]/g;
If (base64test.exe C (input )){
Alert ("there were invalid base64 characters in the input text./N" +
"Valid base64 characters are A-Z, A-Z, 0-9, '+', '/', and '='/N" +
"Reverse CT errors in decoding .");
}
Input = input. Replace (/[^ A-Za-z0-9/+ // =]/g ,"");

Do {
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 & 15) <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 );
}

Chr1 = chr2 = chr3 = "";
Enc1 = enc2 = enc3 = enc4 = "";

} While (I <input. Length );

Return Unescape (output );
}

</SCRIPT>

<Form method = "Post" Action = "-- WEBBOT-SELF --">
<Table border = "1" width = "100%" style = "border-collapse: collapse">
<Tr>
<TD> <textarea rows = "20" name = "S1" Cols = "57"> </textarea> </TD>
<TD> <input type = "button" value = "base64decode -- & gt;" name = "B1" onclick = "s2.value = decode64 (s1.value);"> <br>
<Input type = "button" value = "& lt; -- base64encode" name = "B2" onclick = "s1.value = encode64 (s2.value)"> </TD>
<TD> <textarea rows = "20" name = "S2" Cols = "56"> </textarea> </TD>
</Tr>
<Tr>
<TD> </TD>
<TD> </TD>
<TD> </TD>
</Tr>
</Table>

</Form>
</Body>

</Html>

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.