Encoding Of Strings

Posted on 2011-04-19
Last Modified: 2013-11-27
Say I've got a simple two field table in access (Sample data and possible results listed)
STRING -------------------------------------------------- CODED-RESULT
John Smith, 1 Main Street --------------------------- 44654844werrw4we4fw+4fwe4wfwef
Mary Quinn, 22 Green Street ----------------------- 4g489ewr4g4w4gwre4huioyt4t4hr4r4t4rh
Barry Quinn, Lissenhall, Swords -------------------- g48r4gwg49w4gfw4wyuh4a68e84dsg4s68g4a

What I need is a function that will convert the name/address string into some kind of unrecognisable string (and of course the ability to convert back)

I'm not looking for encryption, or any kind of high security. Once the coded result is unrecognisable as a name and address, I'm happy.

It is important that coded result is unique. (every unique name/address should produce a unique result).

If the function used some kind of "seed word" that would be great (but not necessary).

I know that the above methodology is used by providers of some software products where the name and address and other info is stored in a long key for activating software. However I would prefer something built into Access where I will have 1000's of listings.

I have access 2003 & 2007 but I would like any solution provided to work in 2003.
Question by:Eirman
    LVL 41

    Assisted Solution

    I'd recommend something as simple as Base64 encoding.

    This is pretty standard on higher-level languages, but can be added to VBA with the following:
    LVL 26

    Assisted Solution

    Nothing super-fancy
    Turn the letters into comma delimited strings of numbers and vice versa

    Private Function Obfuscate(mystring As String) As String
    Dim Result As String
    Dim myLength As Integer
    Dim x As Integer
    myLength = Len(mystring)
    For x = 1 To myLength
        Result = Result & Asc(Right(Left(mystring, x), 1)) & ","
    Next x
    Obfuscate = Result
    End Function
    Private Function DeObfuscate(mystring As String) As String
    On Error Resume Next
    Dim Result As String
    Dim myLength As Integer
    Dim x As Integer
    Dim TheLetters() As String
    TheLetters() = Split(mystring, ",")
    For x = 0 To UBound(TheLetters)
        Result = Result & Chr(CInt(TheLetters(x)))
    Next x
    DeObfuscate = Result
    End Function

    Open in new window

    LVL 23

    Author Comment

    Thanks for the excellent suggestions ..... Any more anyone ?
    LVL 11

    Accepted Solution

    Here is a simple example of using XOR to reverably obsucre text.

    Option Compare Database
    Option Explicit
    Dim strKey As String
    Function ObscureString(strIn As String) As String
    Dim nCode As Long
    Dim n As Long
    Dim l As Long
    Dim ch As String
    Dim cMask As String
    Dim cAsc As Long
    Dim cMaskAsc As Long
    Dim strOut As String
        l = Len(strIn)
        For l = 1 To Len(strIn)
            For n = 0 To 3
                If l + n <= l Then
                    ch = Mid(strIn, l + n, 1)
                    cAsc = Asc(ch)
                    cMask = Mid(strKey, n + 1, 1)
                    cMaskAsc = Asc(cMask)
                    cAsc = cAsc Xor cMaskAsc
                    strOut = strOut & Chr(cAsc)
                End If
            Next n
        Next l
        ObscureString = strOut
    End Function
    Sub testObs(strInput As String)
    Dim str As String
        strKey = ""
        While Len(strKey) < 4
        strKey = InputBox("Give me four characters", , "WXYZ")
        strKey = Left(strKey, 4)
        Debug.Print "Input:" & strInput
        str = ObscureString(strInput)
        Debug.Print "Obscured: " & str
        Debug.Print "Clear: " & ObscureString(str)
    End Sub

    Open in new window

    LVL 11

    Expert Comment

    That should be "reversably"
    LVL 44

    Expert Comment

    uhuh - reversibly
    LVL 23

    Author Closing Comment

    Thanks everyone
    LVL 11

    Expert Comment

    @GrayL - yup. "Reversibly". Spelling always trips me up! :-)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    This is an explanation of a simple data model to help parse a JSON feed
    A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    760 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

    9 Experts available now in Live!

    Get 1:1 Help Now