Question
Could use some help, please! I am mostly stuck on the TODO: implement file saving. I have coded the others, but not sure I did
Could use some help, please! I am mostly stuck on the TODO: implement file saving. I have coded the others, but not sure I did them correctly!
// Encryption.cpp : This file contains the 'main' function. Program execution begins and ends there. //
#include #include #include #include #include #include
/// /// encrypt or decrypt a source string using the provided key /// /// input string to process /// key to use in encryption / decryption /// transformed string std::string encrypt_decrypt(const std::string& source, const std::string& key) { // get lengths now instead of calling the function every time. // this would have most likely been inlined by the compiler, but design for perfomance. const auto key_length = key.length(); const auto source_length = source.length();
// assert that our input data is good assert(key_length > 0); assert(source_length > 0);
std::string output = source;
// loop through the source string char by char for (size_t i = 0; i { // TODO: student need to change the next line from output[i] = source[i] // transform each character based on an xor of the key modded constrained to key length using a mod
static size_t XORCipher(std::string data, std::string key); { //not sure this should be static. int dataLen = source_length; int keyLen = key.size(); std::string output = source;
for (size_t i = 0; i output[i] = source[i] ^ key[i % keyLen]; }
return output; }
// output[i] = source[i]; // }
// our output length must equal our source length assert(output.length() == source_length);
// return the transformed string return output; }
std::string read_file(const std::string& filename) { std::string file_text = \"John Q. Smith This is my test string\";
// TODO: implement loading the file into a string
return file_text;
std::string encrypt(std::string file_text, std::string const& key) if (!key.size()) return msg;
for (std::string::size_type i = 0; i msg[i] ^= key[i % key.size()];
return file_text; }
}
std::string get_student_name(const std::string& string_data) { std::string student_name;
// find the first newline size_t pos = string_data.find(' '); // did we find a newline if (pos != std::string::npos) { // we did, so copy that substring as the student name student_name = string_data.substr(0, pos); }
return student_name; }
void save_data_file(const std::string& filename, const std::string& student_name, const std::string& key, const std::string& data) { // TODO: implement file saving // file format // Line 1: student name // Line 2: timestamp (yyyy-mm-dd) // Line 3: key used // Line 4+: data
}
int main() { std::cout
// input file format // Line 1: // Line 2: https://pirateipsum.me/ (could be https://www.lipsum.com/ or one of https://www.shopify.com/partners/blog/79940998-15-funny-lorem-ipsum-generators-to-shake-up-your-design-mockups) // Lines 3+: // Fire in the hole bowsprit Jack Tar gally holystone sloop grog heave to grapple Sea Legs. Gally hearties case shot crimp spirits pillage galleon chase guns skysail yo-ho-ho. Jury mast coxswain measured fer yer chains man-of-war Privateer yardarm aft handsomely Jolly Roger mutiny. // Hulk coffer doubloon Shiver me timbers long clothes skysail Nelsons folly reef sails Jack Tar Davy Jones' Locker. Splice the main brace ye fathom me bilge water walk the plank bowsprit gun Blimey wench. Parrel Gold Road clap of thunder Shiver me timbers hempen halter yardarm grapple wench bilged on her anchor American Main. // Brigantine coxswain interloper jolly boat heave down cutlass crow's nest wherry dance the hempen jig spirits. Interloper Sea Legs plunder shrouds knave sloop run a shot across the bow Jack Ketch mutiny barkadeer. Heave to gun matey Arr draft jolly boat marooned Cat o'nine tails topsail Blimey.
const std::string file_name = \"inputdatafile.txt\"; const std::string encrypted_file_name = \"encrypteddatafile.txt\"; const std::string decrypted_file_name = \"decrytpteddatafile.txt\"; const std::string source_string = read_file(file_name); const std::string key = \"password\";
// get the student name from the data file const std::string student_name = get_student_name(source_string);
// encrypt sourceString with key const std::string encrypted_string = encrypt_decrypt(source_string, key);
// save encrypted_string to file save_data_file(encrypted_file_name, student_name, key, encrypted_string);
// decrypt encryptedString with key const std::string decrypted_string = encrypt_decrypt(encrypted_string, key);
// save decrypted_string to file save_data_file(decrypted_file_name, student_name, key, decrypted_string);
std::cout
// students submit input file, encrypted file, decrypted file, source code file, and key used }
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started