Encrypt a short string

Guys, how would you encrypt-convert-encode a name (5-10 chars long) to a short, unique sequence of numbers ?


BASE64 ?

Will that return a sequence of numbers?


If case doesn’t matter, one possibility is to store it into a Variant, then use its Hash property.

(Without knowing the details of what you’re really trying to do.)

Just use some hash algorithm and take as many characters as you need:

[code]Dim s As String = “Hello World”
Dim h As String = SHA256MBS.HashText(s)

dim r as string = left(h,12) // whatever length you need.[/code]


you could write the bytes from a memory block as string numbers?
means a byte with value 48 is “48”

What I am trying to do is hide the name of a database.

Here’s the situation. I had created a webapp which originally connected to one single SQLite database for a single user. That DB had a fixed name like MyDB. It didn’t care since no one would ever see/know/need that DB name.

I want the app now to be able to connect to different DB"s. And for that the user will need to input the DB name to connect to. So I want to distribute short numeric codes instead of DB names.

What’s the best way to do it now without actually changing the filename of the db ?

You are asking for 2 way coding.
A Hash is not perfect for that, since it is conceivable that two items will generate the same hash.

Here is a simplistic approach. The number version of the filename will have twice as many characters as the original filename has letters.
So a 10 char filename gets a 20 digit unique number.

If your system is case-sensitive then A <> a
That means (if no special characters are allowed) that you need A…Z a…z and 0…9
62 items

So in an array of 62 items, randomly set up in your code

arr(1) = "F’
arr(2) = “b”
arr(3) = “6”
arr(4) = “C”

and so on until you have all 62 characters covered

You generate a numeric value for C6F.db by gluing together the index numbers of the letters you need:
C 6 F
04 03 01 = “040301”

To get the true name back, glue together the equivalents
arr(04) + arr(03) + arr(01)

This might help:

HashIds is: Numeric > (Encode) > String Hash > (Decode) > Numeric

Although it seems you are looking for the opposite: String > (Encode) > Numeric hash > (Decode) > String

Yep, I am looking for the opposite… Encode a string to a number (to give to user his DB number) and then the opposite so the user can type the encoded DB number and the app will look for the decoded DB name…

I didn’t look at your project yet… can this be done ?

you cant use a config file?
as example

@Markus Rauch : You are correct. I could do that, but I was looking for a solution vwhere I didn’t have to setup anything

Possibly that’s the way I’ll take anyway.

My suggestion will do that. But if you dont want to use any of the suggestions, feel free

Call these functions like this:

dim f as string f = "Hello World" msgbox EncodeStringToNumbers (f) f = EncodeStringToNumbers (f) msgbox DecodeStringFromNumber (f)

Private Function EncodeStringToNumbers(s as string) as string
  const strLookup as string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
  dim outstring as string
  dim c as string
  for x as integer = 1 to len(s)
    c = mid(s,x,1)
    for y as integer = 1 to len(strlookup)
      if StrComp(c,mid(strlookup,y,1),0) =0 then  //case sensitive
        outstring = outstring + format(y,"00")
      end if
  return outstring
End Function

[code]Private Function DecodeStringFromNumber(s as string) as string
const strLookup as string = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789”

dim outstring as string
dim c as string
dim v as integer

for x as integer = 1 to len(s) step 2

c = mid(s,x,2)
v = val(c)

outstring = outstring + mid(strLookup,v,1)

return outstring
End Function[/code]

@Jeff Tullin : Hey Jeff, thanks a lot for taking the time to answer, I appreciate it. The issue I have with your suggestion is that DB names usually are 8-10 chars long… that will give a 20 chars long code… which is kindda too long… but I haven’t 100% made up my mind regarding this…

I would have a “master” database that has a table to match a number to a database name. The number would be random between 100000 & 999999 and be the unique primary key.

Change of direction:-
If each username has a unique db, why not make the db name just a hash of the user name?
They presumably have to enter a user name anyway?
(They do need a user password, right?)

Why ?

Because ideally the user should remember his own property ID