0

ok this is what I have now it's almost right but won't loop around the text also it messes up if the key has an uppercase :-/ any hints or clues would be amazing I don't understand size_of_the_key bcz I've never seen that all I've seen is strlen

 #define LMIN 97
#define LMAX 122
#define UMIN 65
#define UMAX 90
#define ALPHANUM 25
int main(int argc, string argv[])
{   
int i, j, n;

 string pt = GetString();
 string key = argv[1];

for(i = 0, j = 0, n = strlen(pt); i < n; i++, j++)
{

if(pt[i]  >= LMIN||pt[i]  == LMAX){

pt[i] = ((pt[i] - LMIN) + (key[j] - LMIN)) % 26;

pt[i] += LMIN;

printf("%c", pt[i]);
}

else if (pt[i] >= UMIN||pt[i] == UMAX){

pt[i] = ((pt[i] - UMIN) + (key[j] - UMIN)) % 26;

pt[i] += UMIN;

printf("%c",pt[i]);

} else{
printf("%c", pt[i]);
}        
}
}   
1
  • Looks like a normal variable to me? But actually this question is impossible to answer without having the complete source (like you probably have). You simply have to search through the source for the definition of size_of_the_key. Feb 20, 2014 at 10:06

2 Answers 2

0

I assume you're familiar with how the Vigenère cipher works. When the key phrase is shorter than the plaintext, its letters are repeated until it forms a string of the same length, allowing the Caesar shift to be calculated for each plaintext letter.

In your C code, key[k % size_of_the_key] uses the incrementing variable k to access each part of the key phrase in turn. The modulus operator (%) ensures that when k increases beyond the length of the key phrase, the letters are looped around as many times as necessary.

If you're building your own program, you can use a simple call to strlen() to obtain the length of your key phrase. However, in the code you posted, the key phrase will then have to be converted into an array of values from 0 to 25 (key[]).

You should also try making your code work with upper and lower case letters; it looks like it will only convert upper case letters in its present form.

0

Well,

  • the length of the text is almost always longer then the length of the key, which is size_of_the_key. This is valid for most ciphers.

  • key[k % size_of_the_key] ensures you keep looping the values on the key, so if the text is long 6 and the key is long 4, you will have that k % size_of_the_key has values 0,1,2,3,0,1 in the loop

As a comment,

  • k=j in every loop. You can as well get rid of the variable k and use directly j.

  • do not use strlen() to evaluate the size of the key, as the key can easily contain the null character.

2
  • how do I go through each character along with the text if I don't use strlen? if text[i] + k[j] % 26 I am starting to get it correctly but still don't understand how to wrap the key around the text using mod% would it be text % argv[1][i] if i=0; then have like int c = argv[1][i]; then c++; all of this within a for loop Feb 27, 2014 at 3:19
  • Please, can you edit back your changes and ask another question? This post is getting very messy..
    – gg349
    Feb 27, 2014 at 8:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.