PDA

View Full Version : Where would I put this code?


meteo
12-23-2002, 09:35 PM
I know, this is a really strange question, but I have no clue where to put it. Here's the code:


Private Function SubstitutionEncode(ByVal PlainText As String) As String
'encodes plaintext by using a simple substitution cipher.
Dim s As String
Dim i As Long, j As Long
Const InText As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkl" & _
"mnopqrstuvwxyz0123456789 .,?!"
Const OUTCODE As String = "Kv iFyaehOVGpM.HfT60StRDBZ3XUmWdCo" & _
"P8u2,cqIwj!J9sbLnQ?EAlz7rk41xg5NY"
For i = 1 To Len(PlainText)
j = InStr(InText, Mid$(PlainText, i, 1))
If j Then
s = s & Mid$(OUTCODE, j, 1)
Else
s = s & Mid$(PlainText, i, 1)
End If
Next i
SubstitutionEncode = s
End Function

And this also:

Private Function SubstitutionDecode(ByVal CodeText As String) As String
'Decodes codetext by using a simple substitution cipher.
Dim s As String
Dim i As Long, j As Long
Const OUTTEXT As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkl" & _
"mnopqrstuvwxyz0123456789 .,?!"
Const INCODE As String = "Kv iFyaehOVGpM.HfT60StRDBZ3XUmWdCo" & _
"P8u2,cqIwj!J9sbLnQ?EAlz7rk41xg5NY"
For i = 1 To Len(CodeText)
j = InStr(INCODE, Mid$(CodeText, i, 1))
If j Then
s = s & Mid$(OUTTEXT, j, 1)
Else
s = s & Mid$(CodeText, i, 1)
End If
Next i
SubstitutionDecode = s
End Function


As you can see, it's encryption. It is an example from BillSoo. I'm trying to familiarize myself with the methods. I have a simple project I'm trying to put this code into. One simple form. Here's the code:


Private Sub cmdEncr_Click()


strText = txtTest.Text

Open ("C:\Encrypt\file.txt") For Output As #1
Write #1, strText
Close #1

Open ("C:\encrypt\file.txt") For Input As #1
Input #1, strEncrypt
Close #1

lbl1.Caption = strEncrypt
End Sub

Private Sub cmdSplit_Click()
Dim str As String
Dim strArray() As String
Dim x As Integer
str = txtTest.Text
For x = 0 To Len(str) - 1
ReDim Preserve strArray(x)
strArray(x) = Mid(str, x + 1, 1)
Next x
lbl1.Caption = Join(strArray, " ")
End Sub

Private Sub Form_Load()
Dim strText As String
Dim strEncrypt As String
End Sub


Basically, the user types a string into the textbox, and hits the "encrypt" command button. I thought the above sample code would go in here, but it didn't work. Any help is appreciated.

Thanks!

Termor
12-23-2002, 10:00 PM
Where in your code are you calling Bill soo's methods? I don't see a call anywhere to them?

meteo
12-23-2002, 10:02 PM
I haven't put the code into mine yet... because I don't know where to put it. I tried under the cmdEncr_click, but obviously that wouldn't work. I only want the code to initialize when they click "Encrypt" (cmdEncr)

mms
12-23-2002, 10:10 PM
Put the functions in either a form or a module.

A simple example to 'call' them:

Dim a As String
Dim b As String

Private Sub Command1_Click()
a = SubstitutionEncode("This is a test")
Debug.Print a
End Sub

Private Sub Command2_Click()
b = SubstitutionDecode(a)
Debug.Print b
End Sub

Termor
12-23-2002, 10:13 PM
Assuming the method is coded correctly, this should work.
Private Sub cmdEncr_Click()
' encode the text.
strEncrypt = SubstitutionEncode(txtTest.Text)
lbl1.Caption = strEncrypt

' decode the text back.
lbl2.Caption = SubstitutionDecode(strEncrypt)
End Sub

Keeping in mind that while you can use encoding as a simple encryption, that's not what is was designed to do. Encoding, specifically 64 bit, is mainly used to turn non-printable characters (i.e. output from encrypting) into printable characters so you can send it as plain text. This is needed because encryption may turn the letter 'a' into a line feed character, so saving it to a file would confuse an application reading things in line by line for example.

meteo
12-23-2002, 11:08 PM
Thanks. Using that example, I was actually able to put it into my "test" program, get it to work, and then implement it into me actual program... to a degree. I got it to encode to the registry:

Password = SubstitutionEncode(UCase(txtUser.Text))


Username = SubstitutionEncode(UCase(txtPass.Text))





SaveSetting App.Title, Username, "Password", Password
'saves the password in the reg

SaveSetting App.Title, Username, "Username", Username
'saves the username in the reg


Now, I just need to figure a way out to get it to decode back when the user logs in. Here's that code:


Private Sub cmdOK_Click()
Dim strUN As String
Dim strPW As String
Dim bfound As Boolean


If Trim$(txtPassword.Text) = "" Or Trim$(txtUserName.Text) = "" Then
MsgBox ("Please enter your username and password before pressing accept")
Exit Sub
End If

If UCase(txtUserName.Text) = GetSetting(App.Title, txtUserName.Text, "Username") Then
If UCase(txtPassword.Text) = GetSetting(App.Title, txtUserName.Text, "Password") Then
strUser = txtUserName.Text
frmStart.Show
Unload Me
Else: MsgBox ("Try Again")
End If
End If
End Sub


This is where I'm stumped. Notice that when I am encoding/writing to the registry on my first code, I am writing everything in Ucase. So if you plan to help me out, just remember that what is in the registy is encoded in Uppercase. What the user is entering isn't always Uppercase. Anyway, I know I need to use:
SubstitutionDecode(*SOMETHING IN HERE*)
but I'm not sure where. I've tried it in many different aspects, but to no avail.
If you can help, please do.

Thanks!

meteo
12-23-2002, 11:35 PM
Nevermind. I caught my problem. No biggie. Thanks to everyone for the help though.