Thread: Vigenere Cipher

  1. #1
    Registered User wexvenemium's Avatar
    Join Date
    Aug 2011
    Posts
    12

    Vigenere Cipher

    Hey I'm taking the Hardvard CS50 course through ItunesU and I'm working on writing a code that will encrypt a message using the vigenere cipher which uses the
    equation... Ci=(Pi+Kj)%26 where P is plaintext and K is the word to encrypt by.
    I almost have it correct but I can't figure out how to make the ASCII letters wrap around from Z back to A and not go into other ASCII symbols. I've gotten the plain text to wrap around but not the key. Heres the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	printf("What message would you like to encrypt?: \n");
    	char p[50];
    	fgets(p, 50, stdin);
    	printf("What would you like your keyword to be?(do not use caps): \n");
    	char key[15];
    	fgets(key, 15, stdin);
    	int i, n, j;
    	for(i = 0, j = 0, n = strlen(p); i < n; i++, j++)
    		{
    			if(p[i] <= 'A' && p[i] >= 'Z')
    				printf("%c", (((p[i] - 'A') + (key[j] - 'A' ) %26) + 'A'));
    			if(p[i] <= 'a' && p[i] >= 'z')
    				printf("%c", (((p[i] - 'a') + (key[j] - 'a' ) %26) + 'a'));
    			else if(p[i] >= 'A' && p[i] <= 'Z')
    				printf("%c", (((p[i] - 'A') + (key[j] - 'A') %26) + 'A'));
    			else if(p[i] >= 'a' && p[i] <= 'z')
    				printf("%c", (((p[i] - 'a') + (key[j] - 'a') %26) + 'a'));
    			else
    				printf("%c", p[i]);
    		}
    	printf("\n");
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if(p[i] <= 'A' && p[i] >= 'Z')
    Do you know this can never be true?

    > printf("What would you like your keyword to be?(do not use caps): \n");
    ...
    > printf("%c", (((p[i] - 'A') + (key[j] - 'A') %26) + 'A'));
    This is wrong, if your key is always lower case.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're going to drive yourself crazy with all the different cases between the plain text beting upper or lower case (or a symbol), and the key being upper or lower case separately. Why not write a function like "convert_char_to_index" that takes a char and returns 0 for a (or A), 1 for b (or B), etc. and use that to reduce the complexity of your conversion code?

  4. #4
    Registered User wexvenemium's Avatar
    Join Date
    Aug 2011
    Posts
    12
    Ahhhh OK I will try that. Thanks for the help.

  5. #5
    Registered User wexvenemium's Avatar
    Join Date
    Aug 2011
    Posts
    12
    I'm really new to this. How would I do that tabstop?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I usually use a keyboard.

    You need to write a function that takes a char and returns an int. You'll need to decide what you want to happen for things like ' ' and '.' and other punctuation marks. For the letters you'll need to return 0 through 25.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by wexvenemium View Post
    I'm really new to this. How would I do that tabstop?
    Well you could always do like he suggested:
    Quote Originally Posted by tabstop View Post
    write a function like "convert_char_to_index"
    Is that the confusing part?
    Quote Originally Posted by tabstop View Post
    that takes a char
    Or that?
    Quote Originally Posted by tabstop View Post
    and returns 0 for a (or A), 1 for b (or B), etc.
    Or maybe that? I don't see you actually trying anything, so all I can do here is guess at why you are so bewildered.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User wexvenemium's Avatar
    Join Date
    Aug 2011
    Posts
    12
    Don't be a dick.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I think a link to a function pointer tutorial would suffice here guys.
    "what have you tried?" is the way to not come across as being the opposite of helpful.

    Sometimes you catch people on a bad day.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User wexvenemium's Avatar
    Join Date
    Aug 2011
    Posts
    12
    Thanks iMalc. That tutorial really helped.

  11. #11
    Registered User wexvenemium's Avatar
    Join Date
    Aug 2011
    Posts
    12
    I've also been trying to use atoi to convert the chars to ints, but I can't figure out how that would work for an array such as int[25] and define 'a' - 'z' and 'A' - 'Z' to 0-25 of the array. So I have a feeling that writing a separate function that does more than convert one char to int at a time would be the only way. Tell me if I'm wrong....

    Here's my updated code... I took the atoi out cuz I kept getting bus errors because I think I was trying to assign an array to an int or some ........ like that...
    The code is encrypting successfully with the exception that 'z' or 'Z' will not wrap around to 'a' or 'A'. I finally got it to stop giving me other various ascii symbols though.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
    	{
    		if(argc != 1)
    			{
    				printf("........ You!\n");
    				return 1;
    			}
    	}
    	char p[50];
    	if(argc	= 1)
    		printf("What message would you like to encrypt?\n");
    	fgets(p, 50, stdin);
    	char key[15];
    		printf("What would you like your keyword to be?\n");
    	fgets(key, 15, stdin);
    	int i, j, n; 
    	for(i = 0, j = 0, n = strlen(p); i < n; i++, j++)
    	{
    		char a = (((p[i] - 'A') + (key[j] - 'a') % 26) + 'A');
    		char b = (((p[i] - 'a') + (key[j] - 'a') % 26) + 'a');
    		if(a >= 'A' && a <= 'Z')
    			printf("%c", a);
    		if(b >= 'a' && b <= 'z')
    			printf("%c", b);
    		else
    			printf("%c", p[i]);
    	}
    	printf("\n");
    	return 0;
    	
    }

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    atoi takes a textual representation of a number, as in "145" as a string, and returns the value 145. It is not going to help you even a little bit for this problem.

    (Also the code you have wraps around correctly, so you should give some example of what you are concerned about.)

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by wexvenemium View Post
    Don't be a dick.
    Quote Originally Posted by wexvenemium View Post
    Thanks iMalc. That tutorial really helped.
    Wait a minute. I specifically ask you what part of making a function you don't understand, and you get your panties in a twist, but then he does the same thing and you thank him? What a dumbass.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User wexvenemium's Avatar
    Join Date
    Aug 2011
    Posts
    12
    iMalc gave me some help and he wasn't totally condescending, like you were. I was just looking for a little help; I wasn't really looking to be scoffed at by ..........s like you. I don't know why you need to make people feel inferior over internet forums. Did your father rape you? You know there are people that can help you get closure on your traumatic past. Anyway, your highness, why don't you take your supreme knowledge and awe inspiring intellect and just .......... off.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    OK - enough.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vigenere Cipher decryption help needed
    By magic101 in forum C Programming
    Replies: 2
    Last Post: 02-07-2009, 07:25 PM
  2. Crashing Vigenere Cipher program
    By ultrabot90 in forum C++ Programming
    Replies: 15
    Last Post: 09-21-2007, 05:41 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