About encryption and decryption Base64 and URL and Hex Encoding and Decoding

Source: Internet
Author: User
Tags base64 encode javascript urlencode

I want to change the style of the Discuz forum today. After downloading the style file, I found it was encrypted by Base64.
Kobayashi recommends a decryption page. The extracted code is as follows: Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> Base64 and URL and Hex Encoding and Decoding </title>
<Meta name = "description" content = "Encodes or decodes data in Base64 or URL encoding using client side JavaScript"/>
<Meta name = "keywords" content = "base64, base 64, urlencode, urldecode, hexencode, hex encode, hexdecode hex decode, javascript base64, javascript base 64, javascript urlencode, javascript urldecode, javascript hexencode, javascript hexdecode "/>
<Link rel = "shortcut icon" href = "http://ostermiller.org/favicon.ico" type = "image/x-icon"/>
<Script language = javascript type = "text/javascript">
<! --
Function urlDecode (str ){
Str = str. replace (new RegExp ('\\+', 'G '),'');
Return unescape (str );
}
Function urlEncode (str ){
Str = escape (str );
Str = str. replace (new RegExp ('\\+', 'G'), '% 2B ');
Return str. replace (new RegExp ('% 20', 'G'),' + ');
}
Var END_OF_INPUT =-1;
Var base64Chars = new Array (
'A', 'B', 'C', 'D', 'E', 'E', 'F', 'G', 'h ',
'I', 'J', 'k', 'l', 'M', 'n', 'O', 'P ',
'Q', 'R', 's', 't', 'U', 'V', 'w', 'x ',
'Y', 'z', 'A', 'B', 'C', 'D', 'E', 'F ',
'G', 'h', 'I', 'J', 'k', 'l', 'M', 'n ',
'O', 'P', 'Q', 'R', 's', 't', 'U', 'V ',
'W', 'x', 'y', 'z', '0', '1', '2', '3 ',
'4', '5', '6', '7', '8', '9', '+ ','/'
);
Var reverseBase64Chars = new Array ();
For (var I = 0; I <base64Chars. length; I ++ ){
ReverseBase64Chars [base64Chars [I] = I;
}
Var base64Str;
Var base64Count;
Function setBase64Str (str ){
Base64Str = str;
Base64Count = 0;
}
Function readBase64 (){
If (! Base64Str) return END_OF_INPUT;
If (base64Count> = base64Str. length) return END_OF_INPUT;
Var c = base64Str. charCodeAt (base64Count) & 0xff;
Base64Count ++;
Return c;
}
Function encodeBase64 (str ){
SetBase64Str (str );
Var result = '';
Var inBuffer = new Array (3 );
Var lineCount = 0;
Var done = false;
While (! Done & (inBuffer [0] = readBase64 ())! = END_OF_INPUT ){
InBuffer [1] = readBase64 ();
InBuffer [2] = readBase64 ();
Result + = (base64Chars [inBuffer [0]> 2]);
If (inBuffer [1]! = END_OF_INPUT ){
Result + = (base64Chars [(inBuffer [0] <4) & 0x30) | (inBuffer [1]> 4)]);
If (inBuffer [2]! = END_OF_INPUT ){
Result + = (base64Chars [(inBuffer [1] <2) & 0x3c) | (inBuffer [2]> 6)]);
Result + = (base64Chars [inBuffer [2] & 0x3F]);
} Else {
Result + = (base64Chars [(inBuffer [1] <2) & 0x3c)]);
Result + = ('= ');
Done = true;
}
} Else {
Result + = (base64Chars [(inBuffer [0] <4) & 0x30)]);
Result + = ('= ');
Result + = ('= ');
Done = true;
}
LineCount + = 4;
If (lineCount> = 76 ){
Result + = ('\ n ');
LineCount = 0;
}
}
Return result;
}
Function readReverseBase64 (){
If (! Base64Str) return END_OF_INPUT;
While (true ){
If (base64Count> = base64Str. length) return END_OF_INPUT;
Var nextCharacter = base64Str. charAt (base64Count );
Base64Count ++;
If (reverseBase64Chars [nextCharacter]) {
Return reverseBase64Chars [nextCharacter];
}
If (nextCharacter = 'A') return 0;
}
Return END_OF_INPUT;
}
Function ntos (n ){
N = n. toString (16 );
If (n. length = 1) n = "0" + n;
N = "%" + n;
Return unescape (n );
}
Function decodeBase64 (str ){
SetBase64Str (str );
Var result = "";
Var inBuffer = new Array (4 );
Var done = false;
While (! Done & (inBuffer [0] = readReverseBase64 ())! = END_OF_INPUT
& (InBuffer [1] = readReverseBase64 ())! = END_OF_INPUT ){
InBuffer [2] = readReverseBase64 ();
InBuffer [3] = readReverseBase64 ();
Result + = ntos (inBuffer [0] <2) & 0xff) | inBuffer [1]> 4 ));
If (inBuffer [2]! = END_OF_INPUT ){
Result + = ntos (inBuffer [1] <4) & 0xff) | inBuffer [2]> 2 ));
If (inBuffer [3]! = END_OF_INPUT ){
Result + = ntos (inBuffer [2] <6) & 0xff) | inBuffer [3]);
} Else {
Done = true;
}
} Else {
Done = true;
}
}
Return result;
}
Var digitArray = new Array ('0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F ');
Function toHex (n ){
Var result =''
Var start = true;
For (var I = 32; I> 0 ;){
I-= 4;
Var digit = (n> I) & 0xf;
If (! Start | digit! = 0 ){
Start = false;
Result + = digitArray [digit];
}
}
Return (result = ''? '0': result );
}
Function pad (str, len, pad ){
Var result = str;
For (var I = str. length; I <len; I ++ ){
Result = pad + result;
}
Return result;
}
Function encodeHex (str ){
Var result = "";
For (var I = 0; I <str. length; I ++ ){
Result + = pad (toHex (str. charCodeAt (I) & 0xff), 2, '0 ');
}
Return result;
}
Function decodeHex (str ){
Str = str. replace (new RegExp ("s/[^ 0-9a-zA-Z] // g "));
Var result = "";
Var nextchar = "";
For (var I = 0; I <str. length; I ++ ){
Nextchar + = str. charAt (I );
If (nextchar. length = 2 ){
Result + = ntos (eval ('0x '+ nextchar ));
Nextchar = "";
}
}
Return result;
}
// --> </Script>
</Head>
<Body>
<Form name = code onsubmit = "return false ()">
<Textarea name = text style = 'width: 100%; height: 75%; 'onfocus = 'if (this. value = "Enter text to encode or decode here. ") {this. value = "";} '> Enter text to encode or decode here. </textarea>
<Table>
<Tr> <td align = center>
<Input value = "Encode" type = button onclick = "document. code. text. value = urlEncode (document. code. text. value);">
</Td> <td align = center>
URL
</Td> <td align = center>
<Input value = "Decode" type = button onclick = "document. code. text. value = urlDecode (document. code. text. value);">
</Td> </tr>
<Tr> <td align = center>
<Input value = "Encode" type = button onclick = "document. code. text. value = encodeBase64 (document. code. text. value);">
</Td> <td align = center>
Base 64
</Td> <td align = center>
<Input value = "Decode" type = button onclick = "document. code. text. value = decodeBase64 (document. code. text. value);">
</Td> </tr>
<Tr> <td align = center>
<Input value = "Encode" type = button onclick = "document. code. text. value = encodeHex (document. code. text. value);">
</Td> <td align = center>
Hex
</Td> <td align = center>
<Input value = "Decode" type = button onclick = "document. code. text. value = decodeHex (document. code. text. value);">
</Td> </tr>
<Tr> <td align = center>
</Td> <td align = center>
<Input type = reset value = Clear>
</Td> <td align = center>
</Td> </tr>
</Table>
</Form>
<Hr>
Base64 encode/decode was ported from a <a href = "http://ostermiller.org/utils/Base64.html"> Java Base64 encoder/decoder </a>. <br>
Base64 encode/decode was ported to <a href = "http://ostermiller.org/base64_actionscript.html"> Macromedia Actionscript </a>. <br>
<H3> License <P> This program is free software; you can redistribute it and/or modify it
Under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
Any later version. </p>
<P> This program is distributed in the hope that it will be useful,
But without any warranty; without even the implied warranty of MERCHANTABILITY
Or fitness for a particle PURPOSE. See
<A href = "http://www.gnu.org/copyleft/gpl.html"> GNU
General Public License </a> for more details. </p>
<Div style = "padding: 0.2;"> <a href = "http://ostermiller.org/calc/"> More converters, calculators, and other JavaScript goodies </a> </div>
<Div style = "padding: 0.2; text-align: right; "> <a href =" http://ostermiller.org/"> ostermiller.org </a> (<a href =" http://ostermiller.org/siteindex.html "> site index </a>) </div>
<Div style = "padding: 0.2;"> <p> Copyright <a href = "http://ostermiller.org/contact.pl? Regarding = JavaScript + Encoding "class = mail> Stephen Ostermiller </a> 2003-2006 </p> </div>
</Body>
</Html>

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.