Randomize character positions in string

C

Chlaris

Dear all,

I want to randomize character positions in string.

Example :

jhonny --> onyhjn
freddy --> dfyerd

Please help. Thanks.

Chlaris
 
T

Tom van Stiphout

On Fri, 25 Jan 2008 09:51:27 +0700, "Chlaris"

What are you REALLY trying to accomplish?
-Tom.
 
C

Chlaris

I have employee name field.
I want create a module to change the character position in employee name
field, so it's hard for the user to guess the real employee name.
Thx.
 
T

Tom van Stiphout

On Fri, 25 Jan 2008 11:19:02 +0700, "Chlaris"

Got you. Do you want to be able to de-scramble the name, or can the
algorithm be uni-directional?

Btw, you know you can set a text field's InputMask to "password", to
conceal a value?

-Tom.
 
C

Chlaris

When my clients send me their database, they want I can't know the employee
name.
So I need a module to scramble the employee name.
I don't need de-scramble module to change back the field value.
Thanks.
 
J

John W. Vinson

When my clients send me their database, they want I can't know the employee
name.
So I need a module to scramble the employee name.

Just scrambling the order of letters won't be very good security. Hojn is
surely John, and probably not any other name.

I'd suggest a random substitution cypher, getting each letter of the name and
changing it randomly into some other letter - so John might become Wlta (for
John Smith) and Blzk (for John Jones). I've got to go do dishes or I'd toss
something together - I'll try to come back to the thread tomorrow. Sounds like
fun!

John W. Vinson [MVP]
 
T

Tom van Stiphout

On Fri, 25 Jan 2008 12:11:01 +0700, "Chlaris"

OK. Try this:
Function ScrambleString(ByVal s As String) As String
'Scramble by putting characters in random order
Dim intPos As Integer
Dim i As Integer
Dim s2 As String

s2 = String(Len(s), 0) 'initially fill with ASCII 0
For i = 1 To Len(s)
intPos = 0
While intPos = 0
intPos = GetRandom(1, Len(s))
If Asc(Mid$(s2, intPos, 1)) = 0 Then
Mid$(s2, intPos, 1) = Mid$(s, i, 1)
Else
intPos = 0 'Signal that we want to try again. This
position already had a char.
End If
Wend
Next i
ScrambleString = s2
End Function

Private Function GetRandom(ByVal lowerbound As Integer, ByVal
upperbound As Integer) As Integer
GetRandom = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
End Function

In the Immediate window:
?ScrambleString("Hi There")
ihT reeH
heiHeTr
rTH ehei
h reTHei

-Tom.
 
T

Tom van Stiphout

On Thu, 24 Jan 2008 22:23:54 -0700, John W. Vinson

I totally agree, but that's what the OP asked for. I'm glad you're
putting the misses before everything else :)

-Tom.
 
K

Krzysztof Pozorek [MVP]

(...)
So I need a module to scramble the employee name.
I don't need de-scramble module to change back the field value.

You may also sort the employee name (the same level of security):

Function SortStr(s As String)
Dim a() As String
a = Split(StrConv(s, vbUnicode), Chr(0))
WizHook.SortStringArray a
SortStr = Join(a, "")
End Function

?SortStr("freddy")
ddefry

K.P. MVP, Poland
www.access.vis.pl
 
T

Tom van Stiphout

On Fri, 25 Jan 2008 12:11:01 +0700, "Chlaris"

Another thing I thought about later: replace the names using an update
query:
Update Patients
set FirstName = "FN" & PatientID
LastName = "LN" & PatientID

-Tom.
 
J

John W. Vinson

Another thing I thought about later: replace the names using an update
query:
Update Patients
set FirstName = "FN" & PatientID
LastName = "LN" & PatientID

That's a great idea, Tom. Much less work and a much more usable result.

John W. Vinson [MVP]
 
C

Chlaris

How about if I send back their data?
Thx.

Tom van Stiphout said:
On Fri, 25 Jan 2008 12:11:01 +0700, "Chlaris"

Another thing I thought about later: replace the names using an update
query:
Update Patients
set FirstName = "FN" & PatientID
LastName = "LN" & PatientID

-Tom.
 
C

Chlaris

Tom,

Thanks for providing me the function.
To make it is harder to guess the employee name, I write the code like this
:

?ScrambleString(rst!EmployeeName & "xyzaeiou")

?ScrambleString("john" & "xyzaeiou")
nyzauoejhxoi
izenxhoaojuy
jxyohnaizoeu
uonjyahxeizo
jxzuhyaooien
zjaheouiyxno
 
J

John W. Vinson

How about if I send back their data?

All they need is the ID; they can use it to join to their table to look up the
name. In fact they really need not ever send you the name at all!

John W. Vinson [MVP]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top