I'm writing a VBA function in Microsoft Access to Encrypt / Decrypt text data using RSA keys.
The following works currently:
CreateObject("System.Security.Cryptography.RSACryptoServiceProvider")
CreateObject("System.Text.UTF8Encoding")
but this is not working:
CreateObject("System.Convert")
I did some research and came across this stackoverflow question: Can I call a static method of a C# class from VBA via COM?
So as it turns out, System.Convert is a static class so therefore it is unavailable to the COM object, and therefore unavailable to VBA.
I could wrap all my .NET stuff into my own class and then add it to Access as a reference, but its a little outside my comfort zone and I'd rather stay away from having to maintain custom references anyways for this specific project.
So rather than stay fixated on the System.Convert option, I thought about what my needs are specifically. I need to be able to:
- Convert a Base 64 encoded string to a Byte Array and pass to .NET methods
- Convert a Byte Array, returned from .NET calls, into a Base 64 encoded string
The simplest option I see would be to write my own VBA code to convert between Base 64 and a byte array - however I see two pitfalls:
- Encoding issues. Converting between byte arrays and strings means having to deal with encoding issues.
- Base64 variations. Off the top of my head, the Base64 could have CR/LF in it. I am also not sure if Base64 encoded RSA strings would have / need to have a header. If I wrote my own Base64 function, I would need to be sure that it is compatible with the RSACryptoServiceProvider's FromBase64String method. I don't want to introduce extra characters into the encryption stream.
Given my concerns - Convert.FromBase64String seemed the best option since it keeps everything in .NET, but since I can't use it - I need an alternative.
If there were other standard .NET classes that provided the FromBase64String and ToBase64String functionality but were able to be instantiated via CreateObject - that would probably be the best option. I do not know of any however.