0

I need to randomize character sets of 0-1, A-Z, -, and _. Lowercase cannot be part of it. Then xor encrypt it then eventually xor decrypt it. I don't have any issues with the xor encrypt logic as it will work fine with standard strconv(text,vbUnicode) - other than I get results that aren't the acceptable characters - but I am having trouble with how to use padding in my character set. Or alternately, I am going about this all wrong and should still use strconv and somehow restrict the results?

So to tackle this I am currently trying to make a custom conversion function that only uses the characters I mentioned. Since I only have 38 characters I know I need to use padding the get it to a factor of 2 but I am completely lost on how that would help. If I add in 26 equals signs as padding to get it to factor of 2 (38->64) I still am not sure what to do when it does a conversion and comes up with 38+ on the 0-63 scale of possibilities. Any help here would definitely be appreciated!

I did try just duplicating the string table for the last 26 characters but that obviously breaks on the decrypt since an A that originated because i was in position 38 would get translated back as having a value of 0.

So I basically have this:

Function Convert(sText As Variant, sDirection As String)
Dim aCharMap() As Variant
Dim aText() As Variant, bText() As String
Dim iText() As Byte, i As Integer, j As Integer, k As Integer
aCharMap = Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "-", "_", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "-", "_")
If sDirection = "ToBinary" Then
    ReDim aText(Len(sText) - 1)
    ReDim iText(Len(sText) - 1)
    For i = 1 To Len(sText)
        aText(i - 1) = UCase(Mid$(sText, i, 1))
    Next
    For j = 0 To UBound(aText)
        For k = 0 To UBound(aCharMap)
            If aCharMap(k) = aText(j) Then
                iText(j) = CByte(k)
                Exit For
            End If
        Next k
    Next j
    Convert = iText()
ElseIf sDirection = "FromBinary" Then
    bText = Split(sText, ",")
    ReDim aText(LBound(bText) To UBound(bText))
    For j = 0 To UBound(bText)
        aText(j) = aCharMap(bText(j))
    Next
    Convert = Join(aText(), "")
End If
End Function

(And just to head off the inevitable question... No, this doesn't need to be secure. It isn't intended as a lock on a door. It is intended to very simply hide the door. I completely understand that if they look for it, it won't be hard to find and it will open right up with a gentle nudge.)

Thanks for any help!

Community
  • 1
  • 1
Rodger
  • 845
  • 9
  • 21
  • Why use xor rather than modular arithmetic? If you only want to encrypt 38 characters -- why not "add" the characters mod 38? – John Coleman Oct 09 '15 at 16:37
  • To extend my previous comment -- if you are interested in xor because it has the property that encryption and decryption are the exact same operation -- perhaps you can consider using a *Beaufort Cipher* ( https://en.wikipedia.org/wiki/Beaufort_cipher ). This is equivalent to sending `a` to `k - a (mod n)` (n would be 38 in your case) where `a` is the 0-based index of the char to be encrypted and `k` is the index of the corresponding letter of the key stream. Since `k - (k - a) = a`, the same algorithm with the same key both encrypts and decrypts. – John Coleman Oct 09 '15 at 16:48
  • See a similar discussion here on creating unique ID's that correlate to a customer's name: http://stackoverflow.com/q/8047094/5090027 – Grade 'Eh' Bacon Oct 09 '15 at 18:55

0 Answers0