Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This program allows the user to implement a
- // substitution cipher. That is, at runtime, the
- // user enters a substitution key, and a string
- // of plaintext to convert to ciphertext.
- // includes
- #include <stdio.h>
- #include <cs50.h>
- #include <string.h>
- #include <ctype.h>
- // function prototypes
- int validateKey(int args, string argument);
- string encrypt(string key, string plain);
- // MAIN FUNCTION
- int main(int argc, string argv[])
- {
- string keyInput = argv[1]; // user's key input
- int errorCode = validateKey(argc, argv[1]); // key validation
- // if the error code is 1, throw an exception, and exit
- if (errorCode == 1)
- {
- return errorCode;
- }
- // get some plain text from the user
- string plainText = get_string("plaintext: ");
- // printf("\n");
- // store encrypted output in a string
- string encrypted = encrypt(keyInput, plainText);
- // print encrypted text
- printf("ciphertext: %s\n", encrypted);
- // end program
- return 0;
- }
- // ENCRYPTOR
- string encrypt(string key, string plain)
- {
- char alphabet[26] = "abcdefghijklmnopqrstuvwxyz"; // alphabet reference
- // iterate through the plaintext
- for (int i = 0, len = strlen(plain); i < len; i++)
- {
- bool alphabetic = false; // set up a variable to record if current char is alphabetic
- // iterate through the alphabet
- for (int j = 0, length = strlen(alphabet); j < length; j++)
- {
- // check if current char is lower-case letter
- if (plain[i] == alphabet[j])
- {
- // if the key is in upper-case, convert to lower-case
- if (isupper(key[j]))
- {
- plain[i] = tolower(key[j]);
- }
- else
- {
- plain[i] = key[j];
- }
- alphabetic = true; // record that it's an alphabetic char
- j = strlen(alphabet); // skip the rest of the alphabet scan
- }
- else if (plain[i] == toupper(alphabet[j]))
- {
- plain[i] = toupper(key[j]);
- alphabetic = true; // record that it's an alphabetic char
- j = strlen(alphabet); // skip the rest of the alphabet scan
- }
- }
- // if not alphabetic
- if (alphabetic == false)
- {
- plain[i] = plain[i];
- }
- }
- // returned the ciphertext, now stored in the original plain variable
- return plain;
- }
- // KEY VALIDATOR
- int validateKey(int args, string key)
- {
- // if not enough arguments
- if (args < 2)
- {
- printf("Try typing in a cipher key after ./substitution.\n");
- return 1;
- }
- // if too many arguments
- else if (args > 2)
- {
- printf("Try typing only one argument after ./substitution.\n");
- return 1;
- }
- // if right number of arguments
- else
- {
- int length = strlen(key);
- // but wrong length
- if (length < 26 || length > 26)
- {
- printf("Try using *exactly 26* upper-case or lower-case letters in your key.\n");
- return 1;
- }
- // if right length
- else
- {
- // iterate through the key
- for (int i = 0; i < length; i++)
- {
- // if it contains non-alphabetic characters
- if ((key[i] < 65 || key[i] > 90) && (key[i] < 97 || key[i] > 122))
- {
- printf("Try using exactly 26 upper-case or lower-case LETTERS in your key.\n");
- return 1;
- }
- }
- // declare a variable to keep track of the current character
- char currentChar;
- // iterate through the key
- for (int i = 0; i < length; i++)
- {
- // set currentChar value to currently selected character in key
- currentChar = key[i];
- // initialize a counter variable
- int counter = 0;
- // scan through the key
- for (int j = 0; j < length; j++)
- {
- // if the currently selected character matches any in the key, increment counter
- if (currentChar == key[j])
- {
- counter++;
- }
- // if the counter is greater than 1, return an error with a console warning
- if (counter > 1)
- {
- printf("Try not repeating any letters in the key.\n");
- return 1;
- }
- }
- }
- // if the key is valid by all the above criteria
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment