Solved

Base64 encoding

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Excel VBA 10 39
Excel Question 17 15
Msbbox Notice (4 days) 27 58
Why do my Excel files become huge? 27 30
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 article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

776 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