Solved

Base64 encoding

Posted on 2014-03-09
6
837 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 42

Expert Comment

by:Rob Jurd, EE MVE
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 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39918815
What part are you wanting to encode? The returned hex value?
0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE 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 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39919312
thanks aikimark
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

896 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now