Results 1 to 5 of 5

Thread: Vigenere Cipher: Help to show output in ONLY letters

  1. #1

    Thread Starter
    New Member Clooder's Avatar
    Join Date
    Apr 2016
    Posts
    5

    Vigenere Cipher: Help to show output in ONLY letters

    Hello everybody. I'm making project for school and it includes a Vigenere Encipher. There are 3 textboxes. Enter the keyword in Textbox 1, Enter message in Textbox 2, Press button, Output displays in Textbox 3. The following code does the job.

    Code:
    Public Shared Function VIGEncrypt(ByVal cipherTxt As String, ByVal key As String)
    
            Dim encryptedText As String = ""
            For i As Integer = 1 To cipherTxt.Length
                Dim temp As Integer = Asc(GetChar(cipherTxt, i)) + Asc(GetChar(key, i Mod key.Length + 1))
                encryptedText += Chr(temp)
            Next
            Return encryptedText
    
        End Function
    But, this code shows the output in ASCII beyond the letter Z such as / * & ^. How can this code be modified to only show characters between a - z & A - Z. If I didn't explain it well enough, please let me know. I also don't know whether the solution is simple, but any help would be greatly appreciated. Thanks in advance!

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Vigenere Cipher: Help to show output in ONLY letters

    After you get the value for temp, you need to check it to see if it has exceeded the boundaries you've set... if so, then you need to decide what to do... does the value wrap back around so that it goes ...XYZabcd... or do you simply truncate the value at the specified ceiling (which may present a problem at decryption time)... or something else...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Vigenere Cipher: Help to show output in ONLY letters

    You should validate the data before you do the conversion. For example, you should check if the input in your first TextBox contains any non-letters and display a message if it does. While I cannot provide you with a Windows Form Application example, I can provide you with a Console Application equivalent.

    Fiddle: https://dotnetfiddle.net/DFmevu
    Last edited by dday9; May 3rd, 2016 at 09:18 AM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Vigenere Cipher: Help to show output in ONLY letters

    My guess is that even with pre-processed values all being alphas.... the end resulting cipher is what's containing the special chars....
    Given that's he's getting the ASCII values and adding to them:
    Asc(GetChar(cipherTxt, i)) + Asc(GetChar(key, i Mod key.Length + 1))

    It's not too difficult to see that letters in the latter part of the alphabet get manipulated into a special char. The OP wants to prevent that, but doesn't say what they want it to do instead ... but your point is valid too because if the original ASCII value is already outside the bounds...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Vigenere Cipher: Help to show output in ONLY letters

    I only did a quick Google search of Vigenere Cipher. I found that you have a message followed by a keyword where the keyword will repeat its characters, in order, until it matches the length of the message. Then each letter in the keyword represents how many letters to move the keyword forward. So if I have a message of hello and the keyword is ab then the keyword get's transformed to ababa and the message get's transformed to:

    h: h + a(0) = h
    e: e + b(1) = f
    l: l + a(0) = l
    l: l + b(1) = m
    o: o + a(0) = o

    So the ciphered message results in hflmo. So non-letter characters do not make sense, at least to me in the short time I studied it, that is why I would not allow for non-letter characters.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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