Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int check_enc_key(string encryption_key) {
- int length = encryption_key.length();
- const string ABC = "abcdefghijklmnopqrstuvwxyz";
- // check length
- if(encryption_key.length() != 26) {
- cout << "Error! The encryption key must contain 26 characters." << endl;
- return EXIT_FAILURE;
- }
- // check chars, loop ABC
- for(int i = 0; i < length; i++) {
- bool found = false;
- // check if ABC char i is found from enc key
- for(int j = 0; j < length; j++) {
- if(ABC[i] == encryption_key[j]) {
- found = true;
- // check for uppercase while at it
- if(j >= 'A' && j <= 'Z') {
- cout << "Error! The encryption key must contain only lower case characters." << endl;
- return EXIT_FAILURE;
- }
- }
- }
- // all ABC letters must be found from encryption_key
- if (found == false) {
- cout << "Error! The encryption key must contain all alphabets a-z." << endl;
- return EXIT_FAILURE;
- }
- }
- return 0;
- }
- int encrypt(string encryption_key, string plain_text) {
- int length = plain_text.length();
- string enc_text;
- cout << encryption_key << endl;
- /*
- * Edelleen ASCII-arvon selvittämisen jälkeen saat kirjaimen järjestysluvun aakkostossa,
- * kun vähennät kirjaimen ASCII-arvosta ‘a’-kirjaimen ASCII-arvon (97). Tämä järjestysluku sopii
- * indeksiksi, kun etsit salausavaimesta salattavan tekstin kirjaimia.
- */
- for(int i = 0; i < length; i++) {
- char encrypted_char = encryption_key[int(plain_text[i])-97];
- // cout << plain_text[i] << " == " << encrypted_char << endl;
- enc_text += encrypted_char;
- }
- cout << "Encrypted text: " << enc_text << endl;
- return 0;
- }
- int main()
- {
- string encryption_key, user_input;
- cout << "Enter the encryption key: ";
- getline (cin, encryption_key);
- check_enc_key(encryption_key);
- cout << "Enter the text to be encrypted: ";
- getline (cin, user_input);
- encrypt(encryption_key, user_input);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement