Last active
August 29, 2015 14:01
-
-
Save tmplinshi/2f69c09d6bc5a800df76 to your computer and use it in GitHub Desktop.
Base64 Encode & Decode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Base64Encode(string) { | |
; js code from http://www.hcidata.info/base64.htm | |
static js_code := " | |
(LTrim | |
var base64s = ""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/""; | |
function aToB64(rawData){ | |
var encOut = '' | |
var b64 = ''; | |
var i = 0; | |
for (var i = 0 ; i <= rawData.length - 3 ; i += 3) { | |
var snipit = | |
(rawData.charCodeAt(i ) & 0xff) << 16 | |
| (rawData.charCodeAt(i+1) & 0xff) << 8 | |
| (rawData.charCodeAt(i+2) & 0xff); | |
b64 += | |
base64s.charAt(snipit >> 18 & 0x3F) + | |
base64s.charAt(snipit >> 12 & 0x3f) + | |
base64s.charAt(snipit >> 6 & 0x3f) + | |
base64s.charAt(snipit & 0x3f); | |
} | |
switch (rawData.length - i) { | |
case (2): | |
var snipit = | |
(rawData.charCodeAt(i ) & 0xff) << 16 | |
| (rawData.charCodeAt(i+1) & 0xff) << 8; | |
b64 += | |
base64s.charAt(snipit >> 18 & 0x3F) + | |
base64s.charAt(snipit >> 12 & 0x3f) + | |
base64s.charAt(snipit >> 6 & 0x3f) + | |
'='; | |
break; | |
case (1): | |
var snipit = | |
(rawData.charCodeAt(i ) & 0xff) << 16; | |
b64 += | |
base64s.charAt(snipit >> 18 & 0x3F) + | |
base64s.charAt(snipit >> 12 & 0x3f) + | |
'=='; | |
break; | |
case (0): // all done | |
} | |
return b64; | |
} | |
)" | |
static oSC := ComObjCreate("ScriptControl") | |
oSC.Language := "JScript" | |
oSC.ExecuteStatement(js_code) | |
Return oSC.Eval("aToB64('" string "')") | |
} | |
Base64Decode(string) { | |
; js code from http://www.hcidata.info/base64.htm | |
static js_code := " | |
(LTrim | |
var base64s = ""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/""; | |
function b64ToA(b64String) { | |
b64String = b64String.replace(/\n/g,''); // remove any CR/LF | |
b64String = b64String.replace(/\r/g,''); // remove any CR/LF | |
var asciiString = ''; | |
var i; | |
for(i = 0 ; i < b64String.length ; i += 4) { | |
var asciiBitString = | |
(base64s.indexOf(b64String.charAt(i )) & 0xff) << 18 | |
| (base64s.indexOf(b64String.charAt(i+1)) & 0xff) << 12 | |
| (base64s.indexOf(b64String.charAt(i+2)) & 0xff) << 6 | |
| (base64s.indexOf(b64String.charAt(i+3)) & 0xff); | |
// 2011-06-17 amended to cope with imbedded NULLS | |
// asciiString += String.fromCharCode( | |
// asciiBitString >> 16 & 0xff, | |
// asciiBitString >> 8 & 0xff, | |
// asciiBitString & 0xff); | |
for (j = 2 ; j >= 0 ; j--) { | |
if ((asciiBitString >> (8*j) & 0xff) == 0x00) { | |
asciiString += String.fromCharCode(0x2205); | |
} | |
else { | |
asciiString += String.fromCharCode(asciiBitString >> (8*j) & 0xff); | |
} | |
} | |
} | |
if (b64String.charAt(i-2) == '=') { | |
return asciiString.substring(0, asciiString.length -2); | |
} | |
else if (b64String.charAt(i-1) == '=') { | |
return asciiString.substring(0, asciiString.length -1); | |
} | |
else { | |
return asciiString; | |
} | |
} | |
)" | |
static oSC := ComObjCreate("ScriptControl") | |
oSC.Language := "JScript" | |
oSC.ExecuteStatement(js_code) | |
Return oSC.Eval("b64ToA('" string "')") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment