Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that asks the user if they want to encrypt or decrypt, to supply

ID: 3605053 • Letter: W

Question

Write a program that asks the user if they want to encrypt or decrypt, to supply a key, and either the plaintext or ciphertext depending on their choice to encrypt or decrypt. The program will store the user supplied string in a character vector. After which it will send the choice to encrypt/decrypt, the key, and the vector to a function that will encrypt or decrypt appropriately. Only one function will be needed for this program. It’s functionality will change depending on the first parameter.

#include<iostream>

#include<string>

#include<vector>

using namespace std;

int main()

{

vector<string> text;

string w;

int key;

char choice;

cout << "Welcome to the ceaser salad cipher machine" << endl;

cout << "Would you like to (E)ncrypt or (D)ecrypt: ";

cin >> choice;

if (choice == 'e')

{

cout << " Enter key: ";

cin >> key;

cin.clear();

cin.ignore();

cout << " Enter your plaintext: ";

getline(cin, w);

text.push_back(w);

}

else

{

if (choice == 'd')

{

cout << " Enter key: ";

cin >> key;

cin.clear();

cin.ignore();

cout << "Enter your ciphertext";

getline (cin, w);

text.push_back(w);

}

}

return 0;

}

void cipher(vector<string> t, int &a, char &b)

{

if (b == 'e')

{

for (int i = 0, n = t.size; i < n; i++)

{

if (isalpha(t.[i]))

{

//if character is a letter, assume it is an upper case

//ASCI 65 = 'A'

int offset = 65;

//if character is a lowercase letter, offset it by 97

//ASCI 97 = 'a'

if (islower(t.[i]))

offset = 97;

//formula to encrypt the letter by the key

int cipheredLetter = (((int)t.[i] - offset + a) % 26) + offset;

//print out the encrypted character

cout << (char)cipheredLetter;

}

else //if character is not a-z or A-Z, just output it

cout << t.[i];

}

}

if (b == 'd')

{

}

}

This is all I have so far. The problem is i have tried many different ways in doing the cipher but everytime i try i get stopped by the vector.

Explanation / Answer

#include #include #include #include #include #include #include using std::string; using std::vector; using std::cout; using std::endl; void aes_init() { static int init=0; if (init==0) { EVP_CIPHER_CTX e_ctx, d_ctx; //initialize openssl ciphers OpenSSL_add_all_ciphers(); //initialize random number generator (for IVs) int rv = RAND_load_file("/dev/urandom", 32); } } std::vector aes_128_gcm_encrypt(std::string plaintext, std::string key) { aes_init(); size_t enc_length = plaintext.length()*3; std::vector output; output.resize(enc_length,''); unsigned char tag[AES_BLOCK_SIZE]; unsigned char iv[AES_BLOCK_SIZE]; RAND_bytes(iv, sizeof(iv)); std::copy( iv, iv+16, output.begin()+16); int actual_size=0, final_size=0; EVP_CIPHER_CTX* e_ctx = EVP_CIPHER_CTX_new(); //EVP_CIPHER_CTX_ctrl(e_ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL); EVP_EncryptInit(e_ctx, EVP_aes_128_gcm(), (const unsigned char*)key.c_str(), iv); EVP_EncryptUpdate(e_ctx, &output[32], &actual_size, (const unsigned char*)plaintext.data(), plaintext.length() ); EVP_EncryptFinal(e_ctx, &output[32+actual_size], &final_size); EVP_CIPHER_CTX_ctrl(e_ctx, EVP_CTRL_GCM_GET_TAG, 16, tag); std::copy( tag, tag+16, output.begin() ); std::copy( iv, iv+16, output.begin()+16); output.resize(32 + actual_size+final_size); EVP_CIPHER_CTX_free(e_ctx); return output; } std::string aes_128_gcm_decrypt(std::vector ciphertext, std::string key) { aes_init(); unsigned char tag[AES_BLOCK_SIZE]; unsigned char iv[AES_BLOCK_SIZE]; std::copy( ciphertext.begin(), ciphertext.begin()+16, tag); std::copy( ciphertext.begin()+16, ciphertext.begin()+32, iv); std::vector plaintext; plaintext.resize(ciphertext.size(), ''); int actual_size=0, final_size=0; EVP_CIPHER_CTX *d_ctx = EVP_CIPHER_CTX_new(); EVP_DecryptInit(d_ctx, EVP_aes_128_gcm(), (const unsigned char*)key.c_str(), iv); EVP_DecryptUpdate(d_ctx, &plaintext[0], &actual_size, &ciphertext[32], ciphertext.size()-32 ); EVP_CIPHER_CTX_ctrl(d_ctx, EVP_CTRL_GCM_SET_TAG, 16, tag); EVP_DecryptFinal(d_ctx, &plaintext[actual_size], &final_size); EVP_CIPHER_CTX_free(d_ctx); plaintext.resize(actual_size + final_size, ''); return string(plaintext.begin(),plaintext.end()); } int main(int argc, char **argv) { aes_init(); //create a sample key unsigned char key_bytes[16]; RAND_bytes(key_bytes, sizeof(key_bytes)); string key = string((char *)key_bytes, sizeof(key_bytes)); //text to encrypt string plaintext= "elephants in space"; cout
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote