// Arup Guha
// 8/24/2020
// Shows how to encrypt/decrypt for shift cipher in code

#include <stdio.h>
#include <string.h>
int main(void) {

    // Get plaintext and key.
    char word[1000];
    int key;
    printf("Enter a word to encrypt all caps.\n");
    scanf("%s", word);
    printf("Enter a key, 0 to 25.\n");
    scanf("%d", &key);

    int len = strlen(word);
    for (int i=0; i<len; i++) {

        // this is 0 to 25 value...word[i]-'A'
        // add key to it then mod to get 0 to 25 ciphertext
        // then add 'A' to it to get back to the Ascii value.
        printf("%c",(word[i] - 'A' + key)%26 + 'A');
    }
    printf("\n");

    // To decrypt.
    printf("Enter a word to decrypt all caps.\n");
    scanf("%s", word);
    printf("Enter a key, 0 to 25.\n");
    scanf("%d", &key);
    len = strlen(word);

    for (int i=0; i<len; i++) {

        // this is 0 to 25 value...word[i]-'A'
        // subtract key to get ciphertext. Before we mod, we may
        // have to add 26 to map the value back to an equivalent
        // positive value due to how mod works in C and Java.

        // then add 'A' to it to get back to the Ascii value.
        printf("%c",(word[i] - 'A' - key + 26)%26 + 'A');
    }
    printf("\n");

    return 0;
}
