Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Base64 decoding from asp

3 views
Skip to first unread message

Wojtek Rappak

unread,
Jul 13, 1998, 3:00:00 AM7/13/98
to
Is there a simple way of docoding a Base64-encoded string passed to an
asp page?

Thanks for any help on this.
--
______________________________________________________________________________
Wojtek Rappak 88 Tyrwhitt Rd. London SE4 1QB +44 (0)181-691 2089
BKB Client/Server (UK) http://www.bkbcs.com w...@cssys.demon.co.uk

Paul Williams

unread,
Jul 20, 1998, 3:00:00 AM7/20/98
to
> -----Original Message-----
> From: Wojtek Rappak [mailto:woj...@cssys.demon.co.uk]

> Is there a simple way of docoding a Base64-encoded string passed to an
> asp page?

ASP provides no base 64 decoding API. However, you can use the
following code provided you credit me in your source. You'll need to
define an application-scope dictionary called Base64DecodeTable inside
of your global.asa file.


Paul F. Williams
Criterion, Inc. (http://www.criterioninc.com)


------------- Begin Base64.inc -----------------------

<script language=JavaScript runat=Server>

function shiftLeft(data, numBits)
{
return data << numBits;
}

function shiftRight(data, numBits)
{
return data >> numBits;
}

// returns the requested byte of the integer data
// e.g. getByte( 0x00112233, 0) returns 0x00
// getByte( 0x00112233, 1) returns 0x11 etc.

function getByte(data, numByte)
{
return (data >> (24 - (8 * numByte))) & 0xff;
}

</script>

<script language=VBScript runat=Server>

' Decodes a base-64 encoded string.

Function Base64Decode (data)

' If first time running, populate decode table.

If (Base64DecodeTable.Count = 0) Then

Application.Lock

Dim value, i
value = 0

' A - Z

For i = 65 to 90
Base64DecodeTable.Add Chr(i), value
value = value + 1
Next

' a - z

For i = 97 to 122
Base64DecodeTable.Add Chr(i), value
value = value + 1
Next

' 0 - 9

For i = 48 to 57
Base64DecodeTable.Add Chr(i), value
value = value + 1
Next

Base64DecodeTable.Add "+", 62
Base64DecodeTable.Add "/", 63

' The character '=' pads the encoded stream to a
multiple of 4 characters.
' It has no actual value, but we use it as a place
holder.

Base64DecodeTable.Add "=", 0

Application.Unlock
End If

dataLength = Len(data)

If dataLength Mod 4 <> 0 Then
Base64Decode = ""
Err.Raise 6, "CornerStone", "Base-64 encoded string was
not an even multiple of 4 characters long."
Exit Function
End If

' Count the number of data groups.

numGroups = dataLength / 4

Dim result
result = ""

' Now decode each group:

For groupBegin = 1 to dataLength Step 4

Dim numDataBytes, j, k, thisChar, thisData, groupData

' Each data group encodes up to 3 actual bytes.

numDataBytes = 3
groupData = 0

For j = 0 to 3

' Convert each character into 6 bits of data,
and add it to
' an integer for temporary storage. If a
character is a '=', there
' is one fewer data byte. (There can only be a
maximum of 2 '=' in
' the whole string.)

thisChar = Mid(data, groupBegin + j, 1)
thisData = Base64DecodeTable.Item(thisChar)
groupData = shiftLeft(groupData, 6) + thisData

If thisChar = "=" Then
numDataBytes = numDataBytes - 1
End If

Next

' Convert 4-byte integer into up to 3 characters

For k = 1 to NumDataBytes
result = result & Chr( getByte(groupData, k) )
Next

Next

Base64Decode = result

End Function

</script>

------------------ end Base64.inc ---------------------------


Here's an example of how to use it:


------------------- Begin Base64.asp -----------------------

<!--#INCLUDE FILE="Base64.inc"-->

<%

encodedData = Request("encodedData")
decodedData = Request("decodedData")

If encodedData <> "" Then
decodedData = Base64Decode(encodedData)
End If

%>

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Developer Studio">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Base-64 Decoding Test</TITLE>
</HEAD>
<BODY>
<br>Base-64 Decoder<br>
<form method="POST">
<table border=0>
<tr>
<td>Data to be decoded:</td>
<td><input type="text" size="30" name="encodedData"
value="<%= encodedData %>"></td>
</tr>
<tr>
<td>Result:</td>
<td><input type="text" size="30" name="decodedData"
value="<%= decodedData %>"></td>
</tr>
</table>
<input type="submit" name="Action" value="Submit">
</form>

</BODY>
</HTML>


------------------------ End Base64.asp


0 new messages