Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
971 views
in Technique[技术] by (71.8m points)

vb.net - I am having trouble making a loop for a Caesar cipher

For a course-work task at school, I have to code a simple Caesar cipher where the user can choose their own shift (+1, +2, etc.). For example, if the user selects +3 (a-->d etc.) and types in a message that contains letters x,y, or z, these letters must wrap around to the start of the alphabet so x becomes a, y becomes b, z becomes c. I am using the ASCII values to do this.

Another less important issue is that I have used quite a lot of if-else statements for each radio button to alter the Caesar shift. If possible, is there anyway to tidy this up and make it more efficient. (I have just included three if statements for each encrypt and decrypt button for examples.)

I have included my current code below.

Public Class
  Dim FnlValue As String = "" 
  Dim FnlChar As Char 
  Dim VariableChr As Single
  Dim caesar As Integer

  Private Sub encrypt_btn_Click(sender As Object, e As EventArgs) Handles encrypt_btn.Click
    FnlValue = "" 

    For VariableChrNo = 0 To (input.Text.Length - 1) 
      VariableChr = Asc(input.Text.Chars(VariableChrNo)) 
      FnlChar = Chr(VariableChr + caesar) 
      FnlValue = FnlValue + FnlChar 
    Next

    output.Text = FnlValue 

    If rad_2.Checked Then
      caesar = 2
    Else If

    If rad_3.Checked Then
      caesar = 3
    Else If

    If rad_4.Checked Then
      caesar = 4
    Else If
  End Sub

  Private Sub decrypt_btn_Click(sender As Object, e As EventArgs) Handles decrypt_btn.Click
    FnlValue = "" 

    For VariableChrNo = 0 To (output.Text.Length - 1)
      VariableChr = Asc(output.Text.Chars(VariableChrNo)) 
      FnlChar = Chr(VariableChr - caesar)
      FnlValue = FnlValue + FnlChar 
    Next

    input.Text = FnlValue 
  End Sub

  If rad_2.Checked Then
    caesar = -2
  Else If

  If rad_3.Checked Then
    caesar = -3
  Else If

    If rad_4.Checked Then
    caesar = -4
  Else If

End Class
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Computers use numbers for characters. When you're doing a Caesar cipher on a computer, you have to get the number representations of the characters ["A","B","C","D"] to go from, say, [0,1,2,3]->[1,2,3,0], which looks like ["D","A","B","C"] if you regard "A" as being 0. So you can see that mostly you have to add 1 (in this case) to get the output value. However, if you add 1 to 3 you get 4, which is not one of the values you want. But! if you subtract 4 from the 1+3 then you get 0, which is what you want.

ASCII uses 65 for an "A", but you need it to be 0 for the maths to work (the [0,1,2,3] thing). So, if you get the ASCII value for "A" and subtract 65 then you get 0. Now you have something to work with mathematically. After the maths is done, you will have to add back the starting number (65 for uppercase letters) so that the character number corresponds with the representation of the character.

The other part of the ASCII character set you are probably concerned with is "a-z" (i.e. lowercase letters). You can do a similar thing with them, but there isn't really any need to know that Asc("a") is 97: you can get the computer to work that out for you by writing Asc("a").

As you are astute enough to realise that your repeated checks of the radio buttons could do with tidying up into one piece of code (part of what is called refactoring), I suspect that the following code might answer your question:

Function GetCaesarOffset() As Integer
    Dim offset As Integer = -1
    ' put your RadioButton names here in order of 1, 2, ...
    ' where RadioButton1 represents a shift of 2 (no shift is a bit pointless)
    Dim radButtons() As RadioButton = {RadioButton1, RadioButton2}

    ' iterate over the RadioButtons to find out which one is selected...
    For i As Integer = 0 To radButtons.Length - 1
        If radButtons(i).Checked Then
            ' we can return a value from the function right now
            Return i + 1
        End If
    Next

    Return -1 ' nothing was selected

End Function

Private Sub encrypt_btn_Click(sender As Object, e As EventArgs) Handles encrypt_btn.Click
    Dim caesarOffset As Integer = GetCaesarOffset()

    If caesarOffset = -1 Then
        ' the user has not chosen an offset value... tell them
        MessageBox.Show("Please choose a value for the offset.")
        ' now we don't need to continue in this Sub
        Exit Sub
    End If

    Dim txt As String = input.Text
    Dim alphabetLength As Integer = Asc("Z") - Asc("A") + 1 ' this will usually be 26

    Dim enciphered As String = ""

    For i As Integer = 0 To txt.Length - 1
        ' get the ASCII character code (a number)
        Dim c As Integer = Asc(txt(i))

        ' Check what range the ASCII code is in and take appropriate action

        If c >= Asc("a") AndAlso c <= Asc("z") Then
            ' Look at lowercase letters
            ' make it into a number in the range 0-25 by subtracting the
            ' number which corresponds to the letter "a"
            c = c - Asc("a")
            c = c + caesarOffset

            If c > alphabetLength Then
                ' oops! it has fallen off the end of the allowed values
                ' we can correct this by subtracting the length of the alphabet
                c -= alphabetLength
            End If

            c = c + Asc("a")
            enciphered &= Chr(c)

        ElseIf c >= Asc("A") AndAlso c <= Asc("Z") Then
            ' Look at uppercase letters
            c = c - Asc("A")
            ' we can use the Mod function instead of the If...Then
            c = (c + caesarOffset) Mod alphabetLength
            c = c + Asc("A")
            enciphered &= Chr(c)

            ' you could put another ElseIf here for numbers if you wanted to

        Else
            ' it wasn't an uppercase or lowercase character, so
            ' don't do anything to it.
            enciphered &= txt(i)
        End If

    Next

    output.Text = enciphered

End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...