Solved

Base64 encoding

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

744 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

13 Experts available now in Live!

Get 1:1 Help Now