Sprache:

Base64 decode

Sprache: English
Programmiersprache: VBScript
Veröffentlicht von: Dennis Pallett [nicht registriert]
Letzte Änderung: 09.05.2006
Aufrufe: 1508

Beschreibung

Use this function to decode base64 strings/files

Code

1 ' Decodes a base-64 encoded string. 2 Function Base64Decode(ByVal base64String) 3 Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 4 Dim dataLength, sOut, groupBegin 5 6 'remove white spaces, If any 7 base64String = Replace(base64String, vbCrLf, doublequote) 8 base64String = Replace(base64String, vbTab, doublequote) 9 base64String = Replace(base64String, " ", doublequote) 10 11 'The source must consists from groups with Len of 4 chars 12 dataLength = Len(base64String) 13 If dataLength Mod 4 <> 0 Then 14 Err.Raise 1, "Base64Decode", "Bad Base64 string." 15 Exit Function 16 End If 17 18 19 ' Now decode each group: 20 For groupBegin = 1 To dataLength Step 4 21 Dim numDataBytes, CharCounter, thisChar, thisData, nGroup, pOut 22 ' Each data group encodes up To 3 actual bytes. 23 numDataBytes = 3 24 nGroup = 0 25 26 For CharCounter = 0 To 3 27 ' Convert each character into 6 bits of data, And add it To 28 ' an integer For temporary storage. If a character is a '=', there 29 ' is one fewer data byte. (There can only be a maximum of 2 '=' In 30 ' the whole string.) 31 32 thisChar = Mid(base64String, groupBegin + CharCounter, 1) 33 34 If thisChar = "=" Then 35 numDataBytes = numDataBytes - 1 36 thisData = 0 37 Else 38 thisData = InStr(Base64, thisChar) - 1 39 End If 40 If thisData = -1 Then 41 Err.Raise 2, "Base64Decode", "Bad character In Base64 string." 42 Exit Function 43 End If 44 45 nGroup = 64 * nGroup + thisData 46 Next 47 48 'Hex splits the long To 6 groups with 4 bits 49 nGroup = Hex(nGroup) 50 51 'Add leading zeros 52 nGroup = String(6 - Len(nGroup), "0") & nGroup 53 54 'Convert the 3 byte hex integer (6 chars) To 3 characters 55 pOut = Chr(Cbyte("&H" & Mid(nGroup, 1, 2))) + _ 56 Chr(Cbyte("&H" & Mid(nGroup, 3, 2))) + _ 57 Chr(Cbyte("&H" & Mid(nGroup, 5, 2))) 58 59 'add numDataBytes characters To out string 60 sOut = sOut & Left(pOut, numDataBytes) 61 Next 62 63 Base64Decode = sOut 64 End Function

Noch kein Kommentar vorhanden

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS