Solved

Base64 encoding

Posted on 2014-03-09
6
993 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 46

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

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…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

632 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