Base-64 Encoding: Vbscript


By: nbrane
August 6, 2012

Working with base-64 (6-bit) encoding, according to wikipedia etc.

Every 6 bits is made into a 8-bit output value, so every 3 input bytes (24 bits) constitutes an even number of output-bytes (3 bytes=4 outputvalues). The outputvalues (0-63) are converted to printable text, using two alphabets (caps, then lowers, then numbers, then +\).

Starting, "A" is zero. The reason for all the caps (in the code) is cause it was qbasic before puberty.

Now, "what need of base 64?". I only wrote it to get past my mail-server's "authentication", which requires user and pass be coded thusly.

I'll post the decoder as well. If you needed to examine mail in "raw" form, you might want something to do this. No promises, it may "crash and burn", but it worked for my purposes.

FOR I = 65 TO 90
X = X + CHR(I)
NEXT
X = X & LCASE(X) & "0123456789+\"
set fso=wscript.createobject("scripting.filesystemobject")
set infile=fso.opentextfile ("B64.IN",1)
S=infile.readall
OUT = ""
TEST = LEN(S)
L = TEST * 8 MOD 3
AB = STRING(L, "=")
S = S + STRING(L, CHR(0))
FOR I = 1 TO TEST STEP 3
ADDX = 0
ZZ = 64
FOR J = 1 TO 3
M = I + J - 1
IF M > TEST + 1 THEN
EXIT FOR
END IF
N = MID(S, M, 1)
T = ASC(N)
Y = T \ 4 ^ J
'Remainder held for NEXT cycle in XADD
XADD = T - Y * 4 ^ J
Y = Y + ADDX * ZZ
ADDX = XADD
Z = MID(X, Y + 1, 1)
ZZ = ZZ / 4
OUT = OUT & Z
NEXT
IF M > TEST THEN
EXIT FOR
END IF
OUT = OUT & MID(X, XADD + 1, 1)
NEXT
OUT=OUT&AB
set outfile=fso.createtextfile("b64.out",true)
outfile.write OUT


Need more help?
Describe your Problem
Example: Hard Drive Not Detected on My PC

Ask Question