Results 1 to 5 of 5

Thread: Changing a character

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    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!

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Changing a character

    Asc() returns the character/ASCII value of a letter, ie:

    VB Code:
    1. MsgBox Asc("a")

    Chr$() converts that ASCII value into a character, ie:

    VB Code:
    1. Chr$(34)

    To change the value of a character you can add/subtract the ASC value, ie:

    VB Code:
    1. Dim strCurChar As String
    2. Dim strNewChar As String
    3.  
    4. strCurChar = Mid$(TheString, LoopVar, 1)
    5. strNewChar = Chr$(Asc(strCurChar) + 13)
    6.  
    7. 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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    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?

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    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:
    1. Option Explicit
    2.  
    3. Private Const KEY_STRING As String = "this_is_an_insecure_key"
    4.  
    5. Private Function CaeserCrypt(PlainText As String, ByVal Key As String, Optional ByVal Encrypt As Boolean = True) As String
    6.     Dim lonLoop As Long, lonLenKey As Long
    7.     Dim lonLen As Long, lonKeyPos As Long
    8.     Dim intCur As Integer, intNew As Integer
    9.     Dim strRet As String, intCurKey As Integer
    10.    
    11.     lonLen = Len(PlainText)
    12.     lonLenKey = Len(Key)
    13.    
    14.     'Loop through plain text.
    15.     For lonLoop = 1 To lonLen
    16.         lonKeyPos = lonLoop
    17.        
    18.         'If we reach end of key, then start over.
    19.         If lonKeyPos > lonLenKey Then
    20.             lonKeyPos = 1
    21.         End If
    22.        
    23.         'Get current char of plaintext.
    24.         intCur = Asc(Mid$(PlainText, lonLoop, 1))
    25.         'Get current char of key.
    26.         intCurKey = Asc(Mid$(Key, lonKeyPos, 1))
    27.        
    28.         If Encrypt = True Then
    29.             'Encrypt the values.
    30.             intNew = AddASC(intCur, intCurKey)
    31.         Else
    32.             'Decrypt the values.
    33.             intNew = SubtractASC(intCur, intCurKey)
    34.         End If
    35.        
    36.         strRet = strRet & Chr$(intNew)
    37.     Next lonLoop
    38.    
    39.     CaeserCrypt = strRet
    40.    
    41.     strRet = ""
    42. End Function
    43.  
    44. Private Function AddASC(ByVal ASC1 As Integer, ByVal ASC2 As Integer) As Integer
    45.    
    46.     If ASC1 + ASC2 > 255 Then
    47.         AddASC = Abs(255 - (ASC1 + ASC2))
    48.     Else
    49.         AddASC = ASC1 + ASC2
    50.     End If
    51.    
    52. End Function
    53.  
    54. Private Function SubtractASC(ByVal ASC1 As Integer, ByVal ASC2 As Integer) As Integer
    55.    
    56.     If ASC1 - ASC2 < 0 Then
    57.         SubtractASC = 255 + (ASC1 - ASC2)
    58.     Else
    59.         SubtractASC = ASC1 - ASC2
    60.     End If
    61.    
    62. End Function
    63.  
    64. Private Sub Form_Load()
    65.     Dim strPlain As String, strCrypt As String
    66.     Dim strDeCrypt As String
    67.    
    68.     strPlain = "this is some text to encrypt"
    69.    
    70.     MsgBox strPlain, , "Plain Text"
    71.    
    72.     'Encrypt.
    73.     strCrypt = CaeserCrypt(strPlain, KEY_STRING, True)
    74.     MsgBox strCrypt, , "Encrypted"
    75.    
    76.     'Decrypt.
    77.     strDeCrypt = CaeserCrypt(strCrypt, KEY_STRING, False)
    78.     MsgBox strDeCrypt, , "Decrypted"
    79.    
    80.    
    81.    
    82.     strPlain = ""
    83.     strCrypt = ""
    84.     strDeCrypt = ""
    85. End Sub
    Last edited by DigiRev; Dec 28th, 2006 at 11:38 PM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2005
    Posts
    248

    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
  •  



Click Here to Expand Forum to Full Width