+ Reply to Thread
Results 1 to 8 of 8

Obfuscate Text

Hybrid View

  1. #1
    Registered User
    Join Date
    07-22-2009
    Location
    Los Angeles
    MS-Off Ver
    Excel 2016
    Posts
    39

    Obfuscate Text

    I have a need to obfuscate some URL paramater values:

    http://www.domain.com/?campaign_name=my_campaign01

    I'd like to apply a formula to obfuscate the 'my_campaign01' part and I'm looking to do a little brainstorming here in the forums. Doesn't need to be anything fancy - just need to make it so that 'wandering eyes' won't be able to clearly see 'oh that's my_campaign01'

    The inputs (e.g. my_campaign01) will be unique and I need the outputted values to also be unique.

    Thought about writing a long SUBSTITUTE() that would take all the vowels and replace each one of them with a pre-defined integer but I wanted to make sure I'm not overlooking something obvious here.

    Thanks
    James

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Obfuscate Text

    How about using a User Defined Function (UDF) to reverse the text?

    http://www.ozgrid.com/VBA/ReverseCell.htm
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    07-22-2009
    Location
    Los Angeles
    MS-Off Ver
    Excel 2016
    Posts
    39

    Re: Obfuscate Text

    Quote Originally Posted by MarvinP View Post
    How about using a User Defined Function (UDF) to reverse the text?

    http://www.ozgrid.com/VBA/ReverseCell.htm
    Great suggestion! That actually works pretty well ~85% of the time. Unfortunately depending on the input string, sometimes it's not 'random' enough and is easily decipherable - especially ones with fewer characters.

    Any other tips? Base64 encoding would be great but it uses characters in the output that would break a URL, Hex is a bit better if i could wrap it a VBA function somehow.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Obfuscate Text

    How about a simple character-substitution cipher?
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Registered User
    Join Date
    07-22-2009
    Location
    Los Angeles
    MS-Off Ver
    Excel 2016
    Posts
    39

    Re: Obfuscate Text

    Quote Originally Posted by shg View Post
    How about a simple character-substitution cipher?
    That's exactly what I'm trying to do. I was hoping there was a built-in function in excel to do something like this without having to code my own from scratch. You know of one?

    Found one here that I could probably expand on - http://stackoverflow.com/questions/2...pher-using-vba

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Obfuscate Text

    Row\Col
    A
    B
    1
    C:\Users\shg\Documents\App Data - Excel\Excel14 A1: Input
    2
    H:\Yuhfo\aqw\Snxfzcxke\Sip Jxxc - Yflum\Dsnrj14 A2: =Vigenere(A1, "FGXECDOWUIJQBPZVLNYKRMHSTA", TRUE)
    3
    C:\Users\shg\Documents\App Data - Excel\Excel14 A3: =Vigenere(A2, "FGXECDOWUIJQBPZVLNYKRMHSTA", FALSE)


    Function Vigenere(ByVal sMsg As String, _
                      ByVal sKey As String, _
                      Optional bEncrypt As Boolean = True) As String
        ' shg 2012, 2014
    
        ' Returns a string containing the Vigenere encryption or decryption of sMsg
        ' using key sKey. sKey must be a 26-character string containing the letters
        ' of the alphabet in scrambled order and nothing else.
    
        ' Characters in sMsg other than letters are preserved unchanged.
        ' Case is preserved.
    
        Dim sOut        As String
        Dim iChr        As Long   ' character index
        Dim iLtr        As Long   ' 0 to 25 for A to Z
    
        sOut = sMsg
        sMsg = UCase(sMsg)
    
        sKey = UCase(Replace(sKey, " ", ""))
        If sKey Like "*[!A-Z]*" Or Len(sKey) <> 26 Then
            Vigenere = "Invalid key!"
    
        Else
            sKey = sRept(sKey, Len(sMsg) \ Len(sKey) + 1)
    
            For iChr = 1 To Len(sMsg)
                If Mid(sMsg, iChr, 1) Like "[A-Za-z]" Then
                    If bEncrypt Then
                        iLtr = (Asc(Mid(sMsg, iChr, 1)) + Asc(Mid(sKey, iChr, 1))) Mod 26
                    Else
                        iLtr = (Asc(Mid(sMsg, iChr, 1)) - Asc(Mid(sKey, iChr, 1)) + 26) Mod 26
                    End If
                    Mid(sOut, iChr) = Chr(iLtr + IIf(Asc(Mid(sOut, iChr)) <= 90, 65, 97))
                End If
            Next iChr
            Vigenere = sOut
        End If
    End Function
    
    Function sRept(sMsg As String, n As Long) As String
        ' Mimics WorksheetFunction.Rept
        sRept = Replace(Space(n), " ", sMsg)
    End Function

  7. #7
    Registered User
    Join Date
    07-22-2009
    Location
    Los Angeles
    MS-Off Ver
    Excel 2016
    Posts
    39

    Re: Obfuscate Text

    Quote Originally Posted by shg View Post
    Row\Col
    A
    B
    1
    C:\Users\shg\Documents\App Data - Excel\Excel14 A1: Input
    2
    H:\Yuhfo\aqw\Snxfzcxke\Sip Jxxc - Yflum\Dsnrj14 A2: =Vigenere(A1, "FGXECDOWUIJQBPZVLNYKRMHSTA", TRUE)
    3
    C:\Users\shg\Documents\App Data - Excel\Excel14 A3: =Vigenere(A2, "FGXECDOWUIJQBPZVLNYKRMHSTA", FALSE)


    Function Vigenere(ByVal sMsg As String, _
                      ByVal sKey As String, _
                      Optional bEncrypt As Boolean = True) As String
        ' shg 2012, 2014
    
        ' Returns a string containing the Vigenere encryption or decryption of sMsg
        ' using key sKey. sKey must be a 26-character string containing the letters
        ' of the alphabet in scrambled order and nothing else.
    
        ' Characters in sMsg other than letters are preserved unchanged.
        ' Case is preserved.
    
        Dim sOut        As String
        Dim iChr        As Long   ' character index
        Dim iLtr        As Long   ' 0 to 25 for A to Z
    
        sOut = sMsg
        sMsg = UCase(sMsg)
    
        sKey = UCase(Replace(sKey, " ", ""))
        If sKey Like "*[!A-Z]*" Or Len(sKey) <> 26 Then
            Vigenere = "Invalid key!"
    
        Else
            sKey = sRept(sKey, Len(sMsg) \ Len(sKey) + 1)
    
            For iChr = 1 To Len(sMsg)
                If Mid(sMsg, iChr, 1) Like "[A-Za-z]" Then
                    If bEncrypt Then
                        iLtr = (Asc(Mid(sMsg, iChr, 1)) + Asc(Mid(sKey, iChr, 1))) Mod 26
                    Else
                        iLtr = (Asc(Mid(sMsg, iChr, 1)) - Asc(Mid(sKey, iChr, 1)) + 26) Mod 26
                    End If
                    Mid(sOut, iChr) = Chr(iLtr + IIf(Asc(Mid(sOut, iChr)) <= 90, 65, 97))
                End If
            Next iChr
            Vigenere = sOut
        End If
    End Function
    
    Function sRept(sMsg As String, n As Long) As String
        ' Mimics WorksheetFunction.Rept
        sRept = Replace(Space(n), " ", sMsg)
    End Function
    Awesome! Thanks, this works perfectly. One question though, is there a way to store sKey in the function so that I don't have to type out the 26 characters every time I want to use it? something so that I could just type =Vigenere(A1, TRUE) or =Vigenere(A1, False) to decrypt?

    I changed it to:

    HTML Code: 
    And it still encrypts but doesn't seem to decrypt properly.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Obfuscate Text

    Put it in a cell, or create a named constant:

    Name Manager > New, sKey Refers to: = "FGXECDOWUIJQBPZVLNYKRMHSTA"

    Then =Vigenere(A1, sKey, TRUE)
    Last edited by shg; 10-30-2014 at 11:44 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Need to Mod Code on red text, I have VBA to distinguish Paid(Black Text) Unpaid(Red Text)
    By Garbology in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-11-2014, 07:55 PM
  2. [SOLVED] function IF to return text based if text contains exact text
    By in nomine noctis in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 11-05-2013, 06:25 AM
  3. Replies: 0
    Last Post: 04-22-2013, 12:13 PM
  4. Macro to Parse Text - Import text to Excel from Multiple Text Files & Folders
    By Novice_To_Excel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-02-2012, 01:05 AM
  5. [SOLVED] Use .text from previous text box in form to prefill text in second text box
    By chromachem in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-23-2012, 10:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1