Solved

Base64 encoding

Posted on 2014-03-09
6
874 Views
Last Modified: 2014-06-23
Hello,

The code below currently translates AES256 encryption data into hex, how to modify so it encodes using base64?

Public Function cryptaes(text As String, key As String, crypt As Boolean) As String
    Application.Volatile

    Dim oTest           As CRijndael
    Dim sTemp           As String
    Dim bytIn()         As Byte
    Dim bytOut()        As Byte
    Dim bytPassword()   As Byte
    Dim bytClear()      As Byte
    Dim lCount          As Long
    Dim lLength         As Long
    
    Set oTest = New CRijndael

    bytIn = text
    bytPassword = key
    bytOut = oTest.EncryptData(bytIn, bytPassword)
    sTemp = ""
    For lCount = 0 To UBound(bytOut)
        sTemp = sTemp & Right("0" & Hex(bytOut(lCount)), 2)
    Next

    If Not crypt Then
        lLength = Len(text)
        ReDim bytOut((lLength \ 2) - 1)
        For lCount = 1 To lLength Step 2
          bytOut(lCount \ 2) = CByte("&H" & Mid(text, lCount, 2))
        Next
        sTemp = oTest.DecryptData(bytOut, bytPassword)
    End If
    cryptaes = sTemp
End Function

Open in new window

0
Comment
Question by:DemonForce
  • 4
6 Comments
 
LVL 43

Expert Comment

by:Rob
ID: 39917323
You'll need a reference to MSXML (tools -> references)

Private Function EncodeBase64(ByRef arrData() As Byte) As String
    Dim objXML As MSXML2.DOMDocument
    Dim objNode As MSXML2.IXMLDOMElement
    
    ' help from MSXML
    Set objXML = New MSXML2.DOMDocument
    
    ' byte array to base64
    Set objNode = objXML.createElement("b64")
    objNode.dataType = "bin.base64"
    objNode.nodeTypedValue = arrData
    EncodeBase64 = objNode.Text

    Set objNode = Nothing
    Set objXML = Nothing

End Function

Open in new window

0
 
LVL 43

Expert Comment

by:Rob
ID: 39918815
What part are you wanting to encode? The returned hex value?
0
 
LVL 43

Accepted Solution

by:
Rob earned 250 total points
ID: 39919079
so to base64 encode the output of your function:

Sub Base64TestAES()
    Dim b() As Byte
    Dim aes as String
    Dim aes64 as String

    aes = cryptaes("Now is the time for all", "yourkey", True)
    b = StrConv(aes, vbUnicode)
    aes64 = EncodeBase64_Fast( b )
    Debug.Print aes64

End Sub

Public Function EncodeBase64_Fast(ByRef arrData() As Byte) As String
    Static objXML As Object
    Static objNode As Object
    Const cNodename As String = "b64"
    Const cDatatype As String = "bin.base64"
    
    ' instantiate the object and the base64 node
    If objXML Is Nothing Then
        Set objXML = CreateObject("MSXML2.DOMDocument60")
        Set objNode = objXML.createElement(cNodename)
        objNode.DataType = cDatatype
    End If
    
    ' byte array to base64
    objNode.nodeTypedValue = arrData
    EncodeBase64_Fast = objNode.text

End Function

Open in new window

0
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 250 total points
ID: 39919282
@Rob
MSXML2.DOMDocument60
If you use MSXML2.DOMDocument (non version specific), more people will be able to use your code.  Some older systems may not have version 6 and later readers may have version 7 or 8 (or something higher).
0
 
LVL 43

Expert Comment

by:Rob
ID: 39919312
thanks aikimark
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question