0

I'm trying to make a basic caesar cipher in Visual Basic 2010 Express (Console Application) but whenever it encrypts the string it correctly encrypts the first character and replaces all the others with that same encrypted character.

I have tried everything I could think of and have searched all across the internet and can't find a basic console application way that works, I know it can be done as it was set as a homework assignment and there are other people in the class who have got through this issue.

Sub Main()

    Dim Message As String
    Dim EncMessage As String = ""
    Dim counter As Integer = 0

    Do Until counter = 1     'This just repeats the program

        Console.Write("Please enter a string to encrypt: ")
        Message = Console.ReadLine

        Dim LetterArray() As Char = Message.ToCharArray

        For i = 0 To LetterArray.Length - 1
            EncMessage = EncMessage & Chr(Asc(LetterArray) + 3)     'Encrypts the characters one by one (i think this is where the issue is as it encrypts the first one correct but then just outputs the same character on top the correct amount of times)

'This if statement just loops the characters X Y Z x y z back to the start of the alphabet as it is only meant to encrypt letters

            If EncMessage = "{" Then     
                EncMessage = Chr(Asc(LetterArray) - 23)
            ElseIf EncMessage = "|" Then
                EncMessage = Chr(Asc(LetterArray) - 23)
            ElseIf EncMessage = "}" Then
                EncMessage = Chr(Asc(LetterArray) - 23)
            ElseIf EncMessage = "[" Then
                EncMessage = Chr(Asc(LetterArray) - 23)
            ElseIf EncMessage = "\" Then
                EncMessage = Chr(Asc(LetterArray) - 23)
            ElseIf EncMessage = "]" Then
                EncMessage = Chr(Asc(LetterArray) - 23)
            End If


        Next

        Console.WriteLine(EncMessage)
        Console.ReadKey()
        EncMessage = ""

    Loop

End Sub

For example, if I enter "sausage" it will output "vvvvvvv" but it should output "vdtvdjh" because I want it to shift forward 3 characters. Please help, thank you.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • Why is this question tagged as `c#`? Not being a VB guy, I can nonetheless see that you are never using the loop index `i` in your code, so I assume `Asc(LetterArray)` always takes the first character of `LetterArray`. Try `LetterArray[i]` instead. But you will run into another problem, because, you are comparing the the whole encoded message, instead of the last character only ... – derpirscher Jan 26 '19 at 17:03
  • Furthermore, you attempt of dealing with the alphabet overflow is not very robust, because it relies on certain characters to follow a-z and only works if you shift by 3. You should try find a more general approach. For instance you could calculate the offset to `a` and `A` respectively and if its > 26 you can do a modulo operation ... – derpirscher Jan 26 '19 at 17:08
  • Useful links: [ask], the WELCOME [TOUR] and **[Debugging your code using the powerful, free, built-in (!) Step Debugger](https://msdn.microsoft.com/en-us/library/y740d9d3.aspx)** – Ňɏssa Pøngjǣrdenlarp Jan 26 '19 at 18:15
  • 2
    [How to advance string 3 letters in the alphabet (Caesar cipher)?](https://stackoverflow.com/a/53322836/7444103). – Jimi Jan 26 '19 at 19:36
  • Possible duplicate of [How to advance string 3 letters in the alphabet (Caesar cipher)?](https://stackoverflow.com/questions/53318544/how-to-advance-string-3-letters-in-the-alphabet-caesar-cipher) – Icepickle Jan 26 '19 at 21:37

0 Answers0