Created
February 2, 2018 19:22
-
-
Save 110Percent/2e72ecea9e65d070917147012ca89c70 to your computer and use it in GitHub Desktop.
ICS4U Encryption Assignment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Curtis Davies - ICS4U Encryption Assignment | |
Thursday, 1 February, 2018 | |
This program takes a data file "encode.dat" as input and encrypts any | |
alphabetical characters after the first line,using the first line as a key. | |
The encrypted text is outputted to the screen afterwards. | |
*/ | |
#include <iostream> | |
#include <fstream> | |
#include <cstring> | |
int main(int argc, char *argv[]) { | |
// Declare variables | |
char key[64], feed[64], toenc, enchar; | |
int keylen = 0, kid = -1; | |
// Whitespace to make the program look pretty! | |
std::cout << std::endl; | |
// Check if data file exists, throw error if not found. | |
std::ifstream fin("encode.dat"); | |
if (!fin) { | |
std::cout << "encode.dat not found" << std::endl; | |
return 1; | |
} else { | |
std::cout << "encode.dat found! Encrypting string..." << std::endl << std::endl; | |
} | |
// Read key into memory and save its length for processing | |
fin >> key; | |
keylen = std::strlen(key) - 1; | |
// Print the key to the screen and prepare the console for the encrypted string | |
std::cout << "Key: " << key << std::endl << std::endl << "Output: "; | |
// Keep feeding text into the encryptor until the end of the file is reached | |
// 'feed' represents the current word | |
while (fin >> feed) { | |
// Repeat this loop for each letter in the current word | |
for (int i = 0; i < strlen(feed); i++) { | |
// If the character is not an uppercase letter, skip it | |
if (feed[i] > 'Z' || feed[i] < 'A') continue; | |
// Proceed to the next letter in the key. When the end of the key is reached, loop to the beginning again | |
kid++; | |
if (kid > keylen) { | |
kid = 0; | |
} | |
// If the character of the key is not an uppercase letter, skip it | |
if (key[kid] > 'Z' || key[kid] < 'A') continue; | |
// If the code exceeds Z, loop back to ASCII code 65 (A) | |
if ((key[kid] - 65) + feed[i] > 'Z') { | |
enchar = key[kid] - 91 + feed[i]; | |
} else { | |
enchar = key[kid] - 65 + feed[i]; | |
} | |
// Print the encrypted character to the screen | |
std::cout << enchar; | |
} | |
} | |
// After the text is outputted, print an endline | |
std::cout << std::endl; | |
// Program has finished properly, return 0 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment