Thread: Need help with Vigenere

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    28

    Need help with Vigenere

    Ok,s o my vigenere program sort of works. It encrypts most of the messages properly, but not all. And, of course, I'm not sure what I'm doing wrong. This is the code I have:
    Code:
     for (int j=0;j<26;j++)
             {
               if (isspace(KeyString[j])) 
                   {
                     
                KeyString[j]=KeyString[j+1];
                    n=n-1; -----if there is a blank space in the input, this is used to subtract the white space from the length of the string.
                  k++;-increments to the next 
                
                    }
             
               else
               {
             --
             
             EncryptedValue[z] = (PlainText[k] + KeyString[j] - 2*'a') % 26 +'a';     ----------determines what letter to return
             
              z++;---- goes to the next index in the array.
              k++;-----increments to the next letter in
            i=0;
             
               }
    any suggestions? Also, if there is a suggestion I should remove any blank spaces, how does one do so in a array? thanks!

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    This code is illegible. Half of the variables you use are not defined in the code provided and the comments you use are improper syntax. Please edit your post and
    include more relevant code (PROPERLY formatted). A comment in C is like so:
    Code:
    #include <stdio.h>                  
    
    int main(void)
    {
        int x;
        /* This is a comment */
        x = x+1;
        /* This is a multi-line comment
         *  line 2
         */
        // Another single line comment..
        x *= 5;
    
        return x;
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    To remove a space from an array, when you copy the clear text into the cipher text array, simply don't copy the spaces. So you'll use two variables, one will be the index to the clear text array, and the second will be to the proper index in the cipher text array.

    Say you had i for the clear text, and j for the iterator of the cipher text array. The general idea is:

    Code:
    for(i=0,j=0;i<length of Clear text; i++) {
       if(clear[i] != ' ') {
          cipher[j] = key[j] + etc.
          ++j;
       }
    }
    i is always incrementing, as it must, but j only increments when a non-space char is found.

  4. #4
    Registered User
    Join Date
    Dec 2012
    Posts
    28
    Quote Originally Posted by saldar05 View Post
    Ok,so my vigenere program sort of works. It encrypts most of the messages properly, but not all. And, of course, I'm not sure what I'm doing wrong. This is the code I have:
    Code:
     for (int j=0;j<26;j++)
             {
               if (isspace(KeyString[j])) 
                   {
                     
                KeyString[j]=KeyString[j+1];//increments to the next index.
                    n=n-1;//if there is a blank space in the input, this is used to subtract the white space from the length of the string.
                  k++;//increments to the next index in PlainText
                
                    }
             
               else
               {
          
    EncryptedValue[z] = (PlainText[k] + KeyString[j] - 2*'a') % 26 +'a';    //determines what letter to return
             
              z++; // goes to the next index in the array.
              k++;//increments to the next letter in PlainText
              i=0;
             
               }
       }
    any suggestions? Also, if there is a suggestion I should remove any blank spaces, how does one do so in a array? thanks!
    Hopefully this is better. If not, let me know and I'll try to edit some more. sorry about that

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't understand the need for the variable i at all, and I don't understand this:
    Code:
    KeyString[j]=KeyString[j+1];//increments to the next index.
    One, I don't believe you need it, and two, it's not incrementing anything, it's overwriting part of the keyString. Do you REALLY want to do that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vigenère chiper
    By drew99 in forum C Programming
    Replies: 1
    Last Post: 10-09-2011, 06:44 AM
  2. Vigenere Cipher
    By wexvenemium in forum C Programming
    Replies: 14
    Last Post: 09-06-2011, 01:15 PM
  3. Vigenere Encryption
    By vasanth in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2002, 02:53 PM
  4. Vigenere Decipher/Encipher
    By Xander in forum C++ Programming
    Replies: 5
    Last Post: 02-15-2002, 09:24 AM
  5. Vigenere
    By Xander in forum C++ Programming
    Replies: 1
    Last Post: 02-15-2002, 01:34 AM