I have the following formula:
ciphertext[i] = ((plaintext[i] -'a' + k) % 26) + 'a';
I'm trying to solve for plaintext[i] given ciphertext[i].
I'm using a vigenere cipher. If I convert plain text to cipher text shouldn't there be a way to convert the cipher text back to plain text?
Here's the entire code:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[]){
if (argc !=2){
printf("FATAL ERROR: All hail Caesar!");
return 1;
}
for(int i = 0, n=strlen(argv[1]);i < n;i++){
if(!isupper(argv[1][i]) && !islower(argv[1][i])){
printf("FATAL ERROR: All hail Mark Anthony!");
return 1;
}
}
int cipherlength = strlen(argv[1]),ciphercount=0;
char cipherkey[strlen(argv[1])];
for(int i = 0, n=strlen(argv[1]);i < n;i++){
cipherkey[i] = argv[1][i];
}
string plaintext = NULL;
int k;
do{
printf("plaintext: ");
plaintext = get_string();
printf("ciphertext: ");
}while(plaintext == NULL);
char ciphertext[strlen(plaintext)];
char xiphertext[strlen(plaintext)];
k = atoi(argv[1]);
for (int i = 0,n=strlen(plaintext);i < n; i++){
int index = ciphercount % cipherlength;
if(isupper(cipherkey[index])) k = cipherkey[index] - 'A';
else if(islower(cipherkey[index])) k = cipherkey[index] - 'a';
if ((int)plaintext[i] >= 65 && (int)plaintext[i] <= 90){
ciphertext[i] = ((plaintext[i] -'A' + k) % 26) + 'A';
xiphertext[i] = ((plaintext[i] -'A' - k) % 26) + 'A';
ciphercount++;
}else if ((int)plaintext[i] >= 97 && (int)plaintext[i] <= 122){
ciphertext[i] = ((plaintext[i] -'a' + k) % 26) + 'a';
xiphertext[i] = ((plaintext[i] -'a' - k) % 26) + 'a';
ciphercount++;
}else {
ciphertext[i] = plaintext[i];
xiphertext[i] = plaintext[i];
}
printf("%c %c %c /n",plaintext[i],ciphertext[i],xiphertext[i]);
}
printf("\n");
}
When I enter abcdefghijklmnopqrstuvwxyz using random as the key word I get:
rbpgsrxhvmyxdnbsedjthykjpz as ciphertext[]
but when I enter rbpgsrxhvmyxdnbsedjthykjpz again using random as the key word I get:
abcdefghijklSnUpWXYt[v]^_z as xiphertext[]
so it works up to the letter m