|
-
Dec 28th, 2006, 11:06 PM
#1
Thread Starter
Addicted Member
Changing a character
Hi all,
I'm writing a ceaser-cipher type program and I'm just making a sample of the first part of it as a test to make sure I know what I'm doing before I start on the main program. Just a quick question to ask: How do you change a character value?
For example, consider the following code:
Code:
Private Sub Enc_Click()
Dim a As String
Dim b As String
a = TextIn.Text
b = Mid$(a, 1, 1)
End Sub
In this sample example, I make the string "a" contain the content of a textbox, which will contain the plaintext that needs encrypting. the string "b" then takes the first character of that textbox (I'll also need to figure out how to make it go through some kind of loop until it reaches the end of the textbox content stored in "a", but that's a question for later).
What I need to know is, how can I "bit-shift" that value ("b")? I tried some fiddling around with "Chr$()" and "KeyAscii", but with not much success. Obviously I could create 26 variables and have each one store a bit-shifted value for the letters 1-26, and then make a big-ass "If-else" statement, but that would make the code ugly and wouldn't be efficient. Surely there is some command like "b.AsciiCharacterValue = b.AsciiCharacterValue +1" or something? (Of course, it would be more like "+ x", where x would contain a randomly generated number from 1-25 to use as the bitshift).
Thanks!
-
Dec 28th, 2006, 11:19 PM
#2
Re: Changing a character
Asc() returns the character/ASCII value of a letter, ie:
Chr$() converts that ASCII value into a character, ie:
To change the value of a character you can add/subtract the ASC value, ie:
VB Code:
Dim strCurChar As String
Dim strNewChar As String
strCurChar = Mid$(TheString, LoopVar, 1)
strNewChar = Chr$(Asc(strCurChar) + 13)
strReturnVal = strReturnVal & strNewChar
That's just a really basic/poor example.
You'll also need a function to add/subtract ASC values to make sure they don't exceed 255 or you will get an error.
-
Dec 28th, 2006, 11:31 PM
#3
Thread Starter
Addicted Member
Re: Changing a character
Ah, okie, cool. Thanks!
What do you mean by adding or subtracting the ASC values though? AFAIK no keyboard characters exceed ASCII(255), so that shouldn't be a problem, should it?
-
Dec 28th, 2006, 11:34 PM
#4
Re: Changing a character
Here's a quick example I wrote. Didn't really test it out that much but it should work as an example.
VB Code:
Option Explicit
Private Const KEY_STRING As String = "this_is_an_insecure_key"
Private Function CaeserCrypt(PlainText As String, ByVal Key As String, Optional ByVal Encrypt As Boolean = True) As String
Dim lonLoop As Long, lonLenKey As Long
Dim lonLen As Long, lonKeyPos As Long
Dim intCur As Integer, intNew As Integer
Dim strRet As String, intCurKey As Integer
lonLen = Len(PlainText)
lonLenKey = Len(Key)
'Loop through plain text.
For lonLoop = 1 To lonLen
lonKeyPos = lonLoop
'If we reach end of key, then start over.
If lonKeyPos > lonLenKey Then
lonKeyPos = 1
End If
'Get current char of plaintext.
intCur = Asc(Mid$(PlainText, lonLoop, 1))
'Get current char of key.
intCurKey = Asc(Mid$(Key, lonKeyPos, 1))
If Encrypt = True Then
'Encrypt the values.
intNew = AddASC(intCur, intCurKey)
Else
'Decrypt the values.
intNew = SubtractASC(intCur, intCurKey)
End If
strRet = strRet & Chr$(intNew)
Next lonLoop
CaeserCrypt = strRet
strRet = ""
End Function
Private Function AddASC(ByVal ASC1 As Integer, ByVal ASC2 As Integer) As Integer
If ASC1 + ASC2 > 255 Then
AddASC = Abs(255 - (ASC1 + ASC2))
Else
AddASC = ASC1 + ASC2
End If
End Function
Private Function SubtractASC(ByVal ASC1 As Integer, ByVal ASC2 As Integer) As Integer
If ASC1 - ASC2 < 0 Then
SubtractASC = 255 + (ASC1 - ASC2)
Else
SubtractASC = ASC1 - ASC2
End If
End Function
Private Sub Form_Load()
Dim strPlain As String, strCrypt As String
Dim strDeCrypt As String
strPlain = "this is some text to encrypt"
MsgBox strPlain, , "Plain Text"
'Encrypt.
strCrypt = CaeserCrypt(strPlain, KEY_STRING, True)
MsgBox strCrypt, , "Encrypted"
'Decrypt.
strDeCrypt = CaeserCrypt(strCrypt, KEY_STRING, False)
MsgBox strDeCrypt, , "Decrypted"
strPlain = ""
strCrypt = ""
strDeCrypt = ""
End Sub
Last edited by DigiRev; Dec 28th, 2006 at 11:38 PM.
-
Dec 28th, 2006, 11:51 PM
#5
Thread Starter
Addicted Member
Re: Changing a character
Woah.. That looks... Complex, but thank you .
I was just going to say: I tested some sample code:
Code:
Dim RD As Integer
Private Sub Enc_Click()
Dim a As String
Dim b As String
a = TextIn.Text
b = Mid$(a, 1, 1)
Randomize
RD = RandomNumber
b = Chr$(Asc(b) + RD)
MsgBox b
End Sub
Private Function RandomNumber() As Double
Randomize Timer
RandomNumber = Int(Rnd * 25)
End Function
And it works ok, except that it often returns random odd characters. I've looked up an Ascii table and I think this is because it's adding in a numberwhich sometimes takes it over the actual alpha character boundries. I guess what I need it to do is increase the Ascii character within the bounds of either 65-90 (for uppercase) or 97-122 (in lowercase), and if the number would exceed that, start from the first character in that set again (65 or 72). Is this possible?
Sorry, I don't mean to seem rude by pushing your example away - it looks great, and thank you for putting in the time to make it for me, but the code is beyond me and I am not trying to actually make a simple Ceaser Cipher - that is just the first stage in my encryption program. It then translates the "Ceasered" string into strings of what looks like binary (i.e. 10010011010100101) and drops them together in a block. It's not binary though, each string of 0s and 1s has an association (i.e. 0110=a, 1001=b, and so on). There will be several different "Cases" (functions) that define A-Z and a-z with a string of 0s and 1s, and the case that is used in encryption will be picked by a random number in the program, the same as the bit-shift for the ceaser cipher. The bitshift and case number will then be encrypted using a standard encoding case and dumped at the front or end of the file - this will tell the program how to unencrpy the data. Of course it will also encrypt a password, too. It looks like a great idea in my mind but do you think maybe that this is too advanced a project for someone with my level of knowledge? All I'm trying to say, I guess is that I really need to understand whats going on, or the rest of the program wil be unworkable.
Thanks for any help on that question anyway, and thank you for taking the time to write that code for me
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|