diff options
| author | Logan <[email protected]> | 2024-05-16 02:24:15 -0700 |
|---|---|---|
| committer | Logan <[email protected]> | 2024-05-16 02:24:15 -0700 |
| commit | 4ee0726c1d48691bb2cf1a25322336f622e4974a (patch) | |
| tree | 01e85c5b56468f72954dd62456d2c2949e84ecbe /CST 126/Homework 2/main.cpp | |
| parent | Added Homework 2 Project (diff) | |
| download | archived-homework-1-bobjoe64-4ee0726c1d48691bb2cf1a25322336f622e4974a.tar.xz archived-homework-1-bobjoe64-4ee0726c1d48691bb2cf1a25322336f622e4974a.zip | |
Added Menu with Both options. Encoding a file seems to have a padding issue.
Diffstat (limited to 'CST 126/Homework 2/main.cpp')
| -rw-r--r-- | CST 126/Homework 2/main.cpp | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/CST 126/Homework 2/main.cpp b/CST 126/Homework 2/main.cpp new file mode 100644 index 0000000..b68c1e2 --- /dev/null +++ b/CST 126/Homework 2/main.cpp @@ -0,0 +1,141 @@ +// Name: Logan Gillespie +// Class: CST 126 +// Date: 5/16/24 +// Assignment: Homework 2 + +#include <iostream> +#include <fstream> +#include <sstream> + +std::string encodeFileToBase64(const std::string& filename); +std::string decodeBase64(const std::string& encoded); +bool decodeBase64ToFile(const std::string& base64Data, const std::string& filename); + +int main() +{ + int choice = 0; + do { + std::cout << "Welcome to my program! Here are your options:" << std::endl; + std::cout << "0. Quit" << std::endl; + std::cout << "1. Encode File to Base64" << std::endl; + std::cout << "2. Decode Base64 to File" << std::endl; + std::cin >> choice; + switch (choice) { + case 1: + { + std::string filename = "encode.txt"; + std::string base64Data = encodeFileToBase64(filename); + std::cout << "Base64 Encoded Data:\n" << base64Data << std::endl; + break; + } + case 2: + { + std::string base64Data; + std::cout << "Enter Base64 encoded data: "; + std::cin >> base64Data; + + std::string filename; + std::cout << "Enter output filename: "; + std::cin >> filename; + + if (decodeBase64ToFile(base64Data, filename)) { + std::cout << "File successfully decoded and written: " << filename << std::endl; + } + else { + std::cerr << "Failed to decode and write file." << std::endl; + } + break; + } + } + } while (choice != 0); +} + +std::string encodeFileToBase64(const std::string& filename) { + std::ifstream file(filename, std::ios::binary | std::ios::ate); + if (!file.is_open()) { + std::cerr << "Error: Unable to open file " << filename << std::endl; + return ""; + } + + // Get file size + std::streamsize fileSize = file.tellg(); + file.seekg(0, std::ios::beg); + + // Read file into buffer + char* buffer = new char[fileSize]; + file.read(buffer, fileSize); + + // Base64 encoding table + const std::string base64_chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + + // Encode buffer to Base64 + std::stringstream encoded; + int val = 0, valb = -6; + for (int i = 0; i < fileSize; i++) { + unsigned char c = buffer[i]; + val = (val << 8) + c; + valb += 8; + while (valb >= 0) { + encoded << base64_chars[(val >> valb) & 0x3F]; + valb -= 6; + } + } + + // Padding with '=' + while (valb > -6) { + encoded << '='; + valb -= 6; + } + + delete[] buffer; + file.close(); + + return encoded.str(); +} + +std::string decodeBase64(const std::string& encoded) { + // Base64 decoding table + const std::string base64_chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/"; + + std::string decoded; + int val = 0, valb = -8; + for (char c : encoded) { + if (c == '=') break; + if (base64_chars.find(c) == std::string::npos) { + std::cerr << "Error: Invalid Base64 character detected!" << std::endl; + return ""; + } + val = (val << 6) + base64_chars.find(c); + valb += 6; + if (valb >= 0) { + decoded.push_back(char((val >> valb) & 0xFF)); + valb -= 8; + } + } + return decoded; +} + + +bool decodeBase64ToFile(const std::string& base64Data, const std::string& filename) { + std::ofstream file(filename, std::ios::binary); + if (!file.is_open()) { + std::cerr << "Error: Unable to create file " << filename << std::endl; + return false; + } + + std::string decodedData = decodeBase64(base64Data); + if (decodedData.empty()) { + std::cerr << "Error: Failed to decode Base64 data" << std::endl; + return false; + } + + file.write(decodedData.c_str(), decodedData.size()); + file.close(); + return true; +}
\ No newline at end of file |