-4

I cannot figure out why this thing doesn't scramble correctly. I read some other posts on this cipher and as far as I can tell I'm using the exact same algorithm as they are...

The areas commented out are tests I tried to make sure everything was passing through correctly. I believe it all goes through correctly then fails in the algorithm.

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

string get_message(void);
string scramble(string key, string message);

int main(int argc, string argv[])
{
    if(argc == 2)
    {
        string key;
        string message;

        key = argv[1]; 
        //printf("KEY: %s<<",key); 
        message = get_message();
        scramble(key, message);
    }
    else
    {
        printf("Please enter 2 arguments.\n");
        return 1;
    }
}

string get_message(void)
{   
    string message = "";
    do
    {
        message = GetString();
    }
    while(strlen(message) < 1);
    return message;
}

string scramble(string key,string message)
{       
    for(int i = 0, len = strlen(message), key_len = strlen(key); i < len; i++)
    {   
        int letter = message[i];
        //int Tkey = atoi(key[i % key_len]); 
        //printf("KEY: %d<<\n",Tkey);

        if(islower(letter))
        {
            //printf("KEY(%d)",(key[i % key_len]-97));
            letter = (((letter - 97) + (key[i % key_len])) % 26 + 97);
            //printf("(%d)",(letter - 97) + (key[i % key_len])%26);
            printf("%c",letter);
        }       
        else if(isupper(letter))
        {
            //printf("(%d)", key[i]);
            //printf("(%c)",letter); WE HAVE CORRECT LETTER
            letter = (((letter - 65) + (key[i % key_len])) % 26 + 65);
            printf("%c",letter);
        }
    }
    printf("\n");
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Krill
  • 45
  • 5
  • 3
    What exactly is your question? What is your input, what do you expect as output? – nvoigt Mar 28 '15 at 16:17
  • output for : Meet me at the park at eleven am should be : Negh zf av huf pcfx bt gzrwep oz You'd have to look up what a vigenere cipher is to understand it it shifts letters. GetString() asks for use input returns a string of users input. Yes i can use strlen on string. – Krill Mar 28 '15 at 18:02
  • The program runs it just doesn't scramble correctly – Krill Mar 28 '15 at 18:04

1 Answers1

1

I think your calculation is wrong:

You currently have

encryptedLetter = (letter - firstLetterOffset) + key[position % keyLength] % 26 + firstLetterOffset

by check the C operator precedence table we notice that % is evaluated before - or +, meaning that your code actually mean :

encryptedLetter = (letter - firstLetterOffset) + ( key[position % keyLength] % 26 ) + firstLetterOffset

Where you wanted :

encryptedLetter = ( (letter - firstLetterOffset) + key[position % keyLength] ) % 26 + firstLetterOffset

Conclusion : you need to put more parenthese to specify in which order you which to evaluate your expression.


In addition you took the letter number for the text character but not for the key !

Correct expression

encryptedLetter = ( (letter - firstLetterOffset) + key[position % keyLength] - firstLetterOffset ) % 26 + firstLetterOffset

Demonstration in javascript

dvhh
  • 4,724
  • 27
  • 33
  • Thank you. I changed the parentheses but i still get the same output as before Key:bacon Message:Meet Encrypted:Gxza thank you. – Krill Mar 28 '15 at 18:11
  • I forgot but is seem that neither you nor me are taking space in account – dvhh Mar 28 '15 at 18:44
  • just noticed the other error you are taking the letter number for the text but not for the key !! updated the javascript [here](http://jsfiddle.net/gga4nb5z/2/) – dvhh Mar 28 '15 at 19:06
  • Your javascript code is correct. You can test it http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher lookup `key`, `plaintext`, and `ciphertext` for testing. – Barmak Shemirani Mar 28 '15 at 20:10