Thread: Vigenere cipher

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

    Vigenere cipher

    Hello guys, so I have to write a program that will use Vigenere cipher, i.e. encrypt every letter using a keyword. (A/a in keyword represent 0 etc.)

    My code so far looks like this

    Code:
    #include <stdio.h>#include <cs50.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    
    int main(int argc, string argv[])
    {
        string c = argv[1];
        
        int l = strlen(c);
        
        //convert keyword into separate "keys", i.e. A/a = 0 etc.
        for(int i = 0; i < l; i++)
        {
            if(c[i] < 123 && c[i] > 96)
            c[i] = c[i] - 97;
            
            else if(c[i] < 91 && c[i] > 64)
            {
            c[i] = c[i] - 65;
            }
            
            printf("%d \n", c[i]);
        }
        
        string plain = GetString();
        
        for(int i = 0; i < strlen(plain); i++)
        { 
            //if we get to i-th position in a plaintext and the i-th position in key does not exist, set this i-th position to c[i - length] 
            if(i > l)
            c[i] = c[i - l];
       
            else
            
            //if lowercase, then (plain[i] - 97) + ((c[i])%26) + 97
       
            //if uppercase, then (plain[i] - 65) + ((c[i])%26) + 65
            
            //if not a letter, print the original value
    
            //print each crypted letter
        }     
                
    }
    The whole thing should work on texts without the punctuation and spaces, however, it won't work on texts with them; if the first "letter" to encrypt would be a space, the program would print the space as needed, however for second letter, the program would use the 2nd key in keyword, which we do not want.

    If banging my head against the wall whole day, I really can not figure this out, so I came here for help. Please, keep in mind that I just began to code, so apparently the code I've written is not going to be the most effective one.

    Any help is much appreciated.

    //edit: I figured it literally second after I posted, all I have to do is introduce a new integer that will keep track of position in keyword, if the letter is going to be encrypted, increment this value by one, if it is not, keep it as it was. Q_Q
    Last edited by Kecupochren; 03-24-2013 at 07:59 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Don't increment your key with every loop, automatically. Only increment it when it has processed a letter. You're over-thinking this, undoubtedly. Take a break and when you get back to it, think simple.

  3. #3
    Noobin to the max
    Join Date
    Mar 2013
    Posts
    73
    I totally agree with that Adak said, I myself made a Vigenere cipher a few months and it is actually a lot simpler then you may think! If you are doing cs50, as I was when I wrote the program, then you might want to hurry up as you only have 3 weeks left.
    There is always one more bug...

    Remember, don't name you php arrays "hit"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Vigenere
    By saldar05 in forum C Programming
    Replies: 4
    Last Post: 12-24-2012, 10:21 PM
  2. C Vigenere Cipher Wrap-around Array Help
    By intex in forum C Programming
    Replies: 2
    Last Post: 12-08-2012, 04:49 PM
  3. Vigenere Cipher
    By wexvenemium in forum C Programming
    Replies: 14
    Last Post: 09-06-2011, 01:15 PM
  4. Vigenere Cipher decryption help needed
    By magic101 in forum C Programming
    Replies: 2
    Last Post: 02-07-2009, 07:25 PM
  5. Crashing Vigenere Cipher program
    By ultrabot90 in forum C++ Programming
    Replies: 15
    Last Post: 09-21-2007, 05:41 PM