How to advance string 3 letters in the alphabet (Caesar cipher)?










2















I'm trying to make a program that encrypts a string the user submits. I want to use an encryption technique where the string is advanced 3 letters in the alphabet.

Example: abc would become def.

Currently I have a TextBox (TextBox1) and a Button (Button1).

My code so far:



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rawText As String
rawText = TextBox1.Text
Dim letterTxt As String = Chr(Asc(rawText) + 3)
MsgBox(letterTxt)

End Sub


The problem is that when I run it, it only outputs 1 letter.

What did I do wrong?










share|improve this question



















  • 2





    you need a for loop to run over each character to build your result.

    – Dragonthoughts
    Nov 15 '18 at 11:35











  • Also, use ASCW() and CHRW() as we now work in a Unicode, not ASCII world.

    – Dragonthoughts
    Nov 15 '18 at 11:37






  • 3





    XYZ and xyz will become [ ] and . Is that a viable option? Or you want to start back at A, a?

    – Jimi
    Nov 15 '18 at 11:48







  • 1





    Possible duplicate of I am having trouble making a loop for a Caesar cipher

    – Andrew Morton
    Nov 15 '18 at 12:53











  • Whatever you're doing, it's not encryption. It might just barely qualify as a cipher. .Net has several real encryption algorithms built in. You should look at one of those.

    – Joel Coehoorn
    Nov 15 '18 at 17:06
















2















I'm trying to make a program that encrypts a string the user submits. I want to use an encryption technique where the string is advanced 3 letters in the alphabet.

Example: abc would become def.

Currently I have a TextBox (TextBox1) and a Button (Button1).

My code so far:



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rawText As String
rawText = TextBox1.Text
Dim letterTxt As String = Chr(Asc(rawText) + 3)
MsgBox(letterTxt)

End Sub


The problem is that when I run it, it only outputs 1 letter.

What did I do wrong?










share|improve this question



















  • 2





    you need a for loop to run over each character to build your result.

    – Dragonthoughts
    Nov 15 '18 at 11:35











  • Also, use ASCW() and CHRW() as we now work in a Unicode, not ASCII world.

    – Dragonthoughts
    Nov 15 '18 at 11:37






  • 3





    XYZ and xyz will become [ ] and . Is that a viable option? Or you want to start back at A, a?

    – Jimi
    Nov 15 '18 at 11:48







  • 1





    Possible duplicate of I am having trouble making a loop for a Caesar cipher

    – Andrew Morton
    Nov 15 '18 at 12:53











  • Whatever you're doing, it's not encryption. It might just barely qualify as a cipher. .Net has several real encryption algorithms built in. You should look at one of those.

    – Joel Coehoorn
    Nov 15 '18 at 17:06














2












2








2








I'm trying to make a program that encrypts a string the user submits. I want to use an encryption technique where the string is advanced 3 letters in the alphabet.

Example: abc would become def.

Currently I have a TextBox (TextBox1) and a Button (Button1).

My code so far:



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rawText As String
rawText = TextBox1.Text
Dim letterTxt As String = Chr(Asc(rawText) + 3)
MsgBox(letterTxt)

End Sub


The problem is that when I run it, it only outputs 1 letter.

What did I do wrong?










share|improve this question
















I'm trying to make a program that encrypts a string the user submits. I want to use an encryption technique where the string is advanced 3 letters in the alphabet.

Example: abc would become def.

Currently I have a TextBox (TextBox1) and a Button (Button1).

My code so far:



Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rawText As String
rawText = TextBox1.Text
Dim letterTxt As String = Chr(Asc(rawText) + 3)
MsgBox(letterTxt)

End Sub


The problem is that when I run it, it only outputs 1 letter.

What did I do wrong?







string vb.net encryption caesar-cipher






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 26 at 19:36









Jimi

9,43242035




9,43242035










asked Nov 15 '18 at 11:32









ShadewoodShadewood

134




134







  • 2





    you need a for loop to run over each character to build your result.

    – Dragonthoughts
    Nov 15 '18 at 11:35











  • Also, use ASCW() and CHRW() as we now work in a Unicode, not ASCII world.

    – Dragonthoughts
    Nov 15 '18 at 11:37






  • 3





    XYZ and xyz will become [ ] and . Is that a viable option? Or you want to start back at A, a?

    – Jimi
    Nov 15 '18 at 11:48







  • 1





    Possible duplicate of I am having trouble making a loop for a Caesar cipher

    – Andrew Morton
    Nov 15 '18 at 12:53











  • Whatever you're doing, it's not encryption. It might just barely qualify as a cipher. .Net has several real encryption algorithms built in. You should look at one of those.

    – Joel Coehoorn
    Nov 15 '18 at 17:06













  • 2





    you need a for loop to run over each character to build your result.

    – Dragonthoughts
    Nov 15 '18 at 11:35











  • Also, use ASCW() and CHRW() as we now work in a Unicode, not ASCII world.

    – Dragonthoughts
    Nov 15 '18 at 11:37






  • 3





    XYZ and xyz will become [ ] and . Is that a viable option? Or you want to start back at A, a?

    – Jimi
    Nov 15 '18 at 11:48







  • 1





    Possible duplicate of I am having trouble making a loop for a Caesar cipher

    – Andrew Morton
    Nov 15 '18 at 12:53











  • Whatever you're doing, it's not encryption. It might just barely qualify as a cipher. .Net has several real encryption algorithms built in. You should look at one of those.

    – Joel Coehoorn
    Nov 15 '18 at 17:06








2




2





you need a for loop to run over each character to build your result.

– Dragonthoughts
Nov 15 '18 at 11:35





you need a for loop to run over each character to build your result.

– Dragonthoughts
Nov 15 '18 at 11:35













Also, use ASCW() and CHRW() as we now work in a Unicode, not ASCII world.

– Dragonthoughts
Nov 15 '18 at 11:37





Also, use ASCW() and CHRW() as we now work in a Unicode, not ASCII world.

– Dragonthoughts
Nov 15 '18 at 11:37




3




3





XYZ and xyz will become [ ] and . Is that a viable option? Or you want to start back at A, a?

– Jimi
Nov 15 '18 at 11:48






XYZ and xyz will become [ ] and . Is that a viable option? Or you want to start back at A, a?

– Jimi
Nov 15 '18 at 11:48





1




1





Possible duplicate of I am having trouble making a loop for a Caesar cipher

– Andrew Morton
Nov 15 '18 at 12:53





Possible duplicate of I am having trouble making a loop for a Caesar cipher

– Andrew Morton
Nov 15 '18 at 12:53













Whatever you're doing, it's not encryption. It might just barely qualify as a cipher. .Net has several real encryption algorithms built in. You should look at one of those.

– Joel Coehoorn
Nov 15 '18 at 17:06






Whatever you're doing, it's not encryption. It might just barely qualify as a cipher. .Net has several real encryption algorithms built in. You should look at one of those.

– Joel Coehoorn
Nov 15 '18 at 17:06













1 Answer
1






active

oldest

votes


















1














A Caesar cipher method. Accepts positive and negative shifts and, optionally, a number of alphabet letters.

The latter, to be tested with ASCII tables different than the usual US-ASCII.



It doesn't alter digits (skipped) but you can modify it using the same pattern, if needed.



Use the Scramble parameter to select scramble (True) or unscramble (False).



Sample test code:





Dim Scrambled1 As String = CaesarCipher("ABCXYZabcxyz", 3, True)
Dim Scrambled2 As String = CaesarCipher("ABCXYZabcxyz", -5, True)

'Scrambled1 is now DEFABCdefabc
'Scrambled2 is now VWXSTUvwxstu

Dim Unscrambled As String = CaesarCipher(Scrambled2, -5, false)

'Unscrambled is now ABCXYZabcxyz



Function CaesarCipher(Input As String, CaesarShift As Integer, Scramble As Boolean, Optional AlphabetLetters As Integer = 26) As String

Dim CharValue As Integer
Dim MinValue As Integer = AscW("A"c)
Dim MaxValue As Integer = AscW("Z"c)
Dim ScrambleMode As Integer = If((Scramble), 1, -1)
Dim output As StringBuilder = New StringBuilder(Input.Length)

If Math.Abs(CaesarShift) >= AlphabetLetters Then
CaesarShift = (AlphabetLetters * Math.Sign(CaesarShift)) - Math.Sign(CaesarShift)
End If

For Each c As Char In Input
CharValue = AscW(c)
If Not Char.IsNumber(c) Then
CharValue = CharValue + (CaesarShift * ScrambleMode) Mod AlphabetLetters
CharValue = If(AscW(Char.ToUpper(c)) + (CaesarShift * ScrambleMode) > MaxValue, CharValue - AlphabetLetters, CharValue)
CharValue = If(AscW(Char.ToUpper(c)) + (CaesarShift * ScrambleMode) < MinValue, CharValue + AlphabetLetters, CharValue)
End If
output.Append(ChrW(CharValue))
Next
Return output.ToString()
End Function





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318544%2fhow-to-advance-string-3-letters-in-the-alphabet-caesar-cipher%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    A Caesar cipher method. Accepts positive and negative shifts and, optionally, a number of alphabet letters.

    The latter, to be tested with ASCII tables different than the usual US-ASCII.



    It doesn't alter digits (skipped) but you can modify it using the same pattern, if needed.



    Use the Scramble parameter to select scramble (True) or unscramble (False).



    Sample test code:





    Dim Scrambled1 As String = CaesarCipher("ABCXYZabcxyz", 3, True)
    Dim Scrambled2 As String = CaesarCipher("ABCXYZabcxyz", -5, True)

    'Scrambled1 is now DEFABCdefabc
    'Scrambled2 is now VWXSTUvwxstu

    Dim Unscrambled As String = CaesarCipher(Scrambled2, -5, false)

    'Unscrambled is now ABCXYZabcxyz



    Function CaesarCipher(Input As String, CaesarShift As Integer, Scramble As Boolean, Optional AlphabetLetters As Integer = 26) As String

    Dim CharValue As Integer
    Dim MinValue As Integer = AscW("A"c)
    Dim MaxValue As Integer = AscW("Z"c)
    Dim ScrambleMode As Integer = If((Scramble), 1, -1)
    Dim output As StringBuilder = New StringBuilder(Input.Length)

    If Math.Abs(CaesarShift) >= AlphabetLetters Then
    CaesarShift = (AlphabetLetters * Math.Sign(CaesarShift)) - Math.Sign(CaesarShift)
    End If

    For Each c As Char In Input
    CharValue = AscW(c)
    If Not Char.IsNumber(c) Then
    CharValue = CharValue + (CaesarShift * ScrambleMode) Mod AlphabetLetters
    CharValue = If(AscW(Char.ToUpper(c)) + (CaesarShift * ScrambleMode) > MaxValue, CharValue - AlphabetLetters, CharValue)
    CharValue = If(AscW(Char.ToUpper(c)) + (CaesarShift * ScrambleMode) < MinValue, CharValue + AlphabetLetters, CharValue)
    End If
    output.Append(ChrW(CharValue))
    Next
    Return output.ToString()
    End Function





    share|improve this answer





























      1














      A Caesar cipher method. Accepts positive and negative shifts and, optionally, a number of alphabet letters.

      The latter, to be tested with ASCII tables different than the usual US-ASCII.



      It doesn't alter digits (skipped) but you can modify it using the same pattern, if needed.



      Use the Scramble parameter to select scramble (True) or unscramble (False).



      Sample test code:





      Dim Scrambled1 As String = CaesarCipher("ABCXYZabcxyz", 3, True)
      Dim Scrambled2 As String = CaesarCipher("ABCXYZabcxyz", -5, True)

      'Scrambled1 is now DEFABCdefabc
      'Scrambled2 is now VWXSTUvwxstu

      Dim Unscrambled As String = CaesarCipher(Scrambled2, -5, false)

      'Unscrambled is now ABCXYZabcxyz



      Function CaesarCipher(Input As String, CaesarShift As Integer, Scramble As Boolean, Optional AlphabetLetters As Integer = 26) As String

      Dim CharValue As Integer
      Dim MinValue As Integer = AscW("A"c)
      Dim MaxValue As Integer = AscW("Z"c)
      Dim ScrambleMode As Integer = If((Scramble), 1, -1)
      Dim output As StringBuilder = New StringBuilder(Input.Length)

      If Math.Abs(CaesarShift) >= AlphabetLetters Then
      CaesarShift = (AlphabetLetters * Math.Sign(CaesarShift)) - Math.Sign(CaesarShift)
      End If

      For Each c As Char In Input
      CharValue = AscW(c)
      If Not Char.IsNumber(c) Then
      CharValue = CharValue + (CaesarShift * ScrambleMode) Mod AlphabetLetters
      CharValue = If(AscW(Char.ToUpper(c)) + (CaesarShift * ScrambleMode) > MaxValue, CharValue - AlphabetLetters, CharValue)
      CharValue = If(AscW(Char.ToUpper(c)) + (CaesarShift * ScrambleMode) < MinValue, CharValue + AlphabetLetters, CharValue)
      End If
      output.Append(ChrW(CharValue))
      Next
      Return output.ToString()
      End Function





      share|improve this answer



























        1












        1








        1







        A Caesar cipher method. Accepts positive and negative shifts and, optionally, a number of alphabet letters.

        The latter, to be tested with ASCII tables different than the usual US-ASCII.



        It doesn't alter digits (skipped) but you can modify it using the same pattern, if needed.



        Use the Scramble parameter to select scramble (True) or unscramble (False).



        Sample test code:





        Dim Scrambled1 As String = CaesarCipher("ABCXYZabcxyz", 3, True)
        Dim Scrambled2 As String = CaesarCipher("ABCXYZabcxyz", -5, True)

        'Scrambled1 is now DEFABCdefabc
        'Scrambled2 is now VWXSTUvwxstu

        Dim Unscrambled As String = CaesarCipher(Scrambled2, -5, false)

        'Unscrambled is now ABCXYZabcxyz



        Function CaesarCipher(Input As String, CaesarShift As Integer, Scramble As Boolean, Optional AlphabetLetters As Integer = 26) As String

        Dim CharValue As Integer
        Dim MinValue As Integer = AscW("A"c)
        Dim MaxValue As Integer = AscW("Z"c)
        Dim ScrambleMode As Integer = If((Scramble), 1, -1)
        Dim output As StringBuilder = New StringBuilder(Input.Length)

        If Math.Abs(CaesarShift) >= AlphabetLetters Then
        CaesarShift = (AlphabetLetters * Math.Sign(CaesarShift)) - Math.Sign(CaesarShift)
        End If

        For Each c As Char In Input
        CharValue = AscW(c)
        If Not Char.IsNumber(c) Then
        CharValue = CharValue + (CaesarShift * ScrambleMode) Mod AlphabetLetters
        CharValue = If(AscW(Char.ToUpper(c)) + (CaesarShift * ScrambleMode) > MaxValue, CharValue - AlphabetLetters, CharValue)
        CharValue = If(AscW(Char.ToUpper(c)) + (CaesarShift * ScrambleMode) < MinValue, CharValue + AlphabetLetters, CharValue)
        End If
        output.Append(ChrW(CharValue))
        Next
        Return output.ToString()
        End Function





        share|improve this answer















        A Caesar cipher method. Accepts positive and negative shifts and, optionally, a number of alphabet letters.

        The latter, to be tested with ASCII tables different than the usual US-ASCII.



        It doesn't alter digits (skipped) but you can modify it using the same pattern, if needed.



        Use the Scramble parameter to select scramble (True) or unscramble (False).



        Sample test code:





        Dim Scrambled1 As String = CaesarCipher("ABCXYZabcxyz", 3, True)
        Dim Scrambled2 As String = CaesarCipher("ABCXYZabcxyz", -5, True)

        'Scrambled1 is now DEFABCdefabc
        'Scrambled2 is now VWXSTUvwxstu

        Dim Unscrambled As String = CaesarCipher(Scrambled2, -5, false)

        'Unscrambled is now ABCXYZabcxyz



        Function CaesarCipher(Input As String, CaesarShift As Integer, Scramble As Boolean, Optional AlphabetLetters As Integer = 26) As String

        Dim CharValue As Integer
        Dim MinValue As Integer = AscW("A"c)
        Dim MaxValue As Integer = AscW("Z"c)
        Dim ScrambleMode As Integer = If((Scramble), 1, -1)
        Dim output As StringBuilder = New StringBuilder(Input.Length)

        If Math.Abs(CaesarShift) >= AlphabetLetters Then
        CaesarShift = (AlphabetLetters * Math.Sign(CaesarShift)) - Math.Sign(CaesarShift)
        End If

        For Each c As Char In Input
        CharValue = AscW(c)
        If Not Char.IsNumber(c) Then
        CharValue = CharValue + (CaesarShift * ScrambleMode) Mod AlphabetLetters
        CharValue = If(AscW(Char.ToUpper(c)) + (CaesarShift * ScrambleMode) > MaxValue, CharValue - AlphabetLetters, CharValue)
        CharValue = If(AscW(Char.ToUpper(c)) + (CaesarShift * ScrambleMode) < MinValue, CharValue + AlphabetLetters, CharValue)
        End If
        output.Append(ChrW(CharValue))
        Next
        Return output.ToString()
        End Function






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 26 at 23:44

























        answered Nov 15 '18 at 15:34









        JimiJimi

        9,43242035




        9,43242035





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318544%2fhow-to-advance-string-3-letters-in-the-alphabet-caesar-cipher%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto