dimanche 27 mars 2022

Changing this encryption application to handle multiple files

Currently trying to modify this code to handle multiple input files iteratively.

The current functionality takes a single file as input and outputs a single encrypted file, but I would like to improve on this program by allowing it to potentially loop through the current folder, and encrypting all files beginning with 'inputdatafile'

If the current folder has 'inputdatafile1', 'inputdatafile2', 'inputdatafile3'... and so on how could I handle this dynamically?

#include <cassert>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <chrono>
#include <ctime>
#include <time.h>
using namespace std;

/// <summary>
/// encrypt or decrypt a source string using the provided key
/// </summary>
/// <param name="source">input string to process</param>
/// <param name="key">key to use in encryption / decryption</param>
/// <returns>transformed string</returns>
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 < source_length; ++i)
    {// transform each character based on an xor of the key modded constrained to key length using a mod
        output[i] = source[i]^key[i % key_length];
    }

    // 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\nThis is my test string";

    // TODO: implement loading the file into a string
    //open file_text
    std::ifstream ifile(filename);
    //read the file entirely into string
    std::getline(ifile, file_text, '\0');
    //return text
    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('\n');
    // 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

    //file stream
    std::fstream myFile;

    //open file in write mde
    myFile.open("student.txt", ios::out);

    //time object to enter time
    time_t my_time = time(NULL);
    //auto start = std::chrono::system_clock::now();
    //auto legacyStart = std::chrono::system_clock::to_time_t(start);

    //in case file is opening get an error
    if (myFile.is_open()) {
        // get the date time
        auto t = std::time(nullptr);
        struct tm buf;
        auto tm = localtime_s(&buf, &t);

        // open the file
        std::ofstream stream(filename);

        // write student name
        stream << student_name << std::endl;

        // write timestamp
        stream << std::put_time(&buf, "%Y-%m-%d") << std::endl;

        // write key
        stream << key << std::endl;

        // write data
        stream << data << std::endl;

        // force all buffers to be written to disk
        stream.flush();

        // close the stream - we are done
        stream.close();
    }
    else {
        cout << "Error in opening the file";
    }
}

int main()
{
    std::cout << "Encyption Decryption Test!" << std::endl;

    // input file format
    // Line 1: <students name>
    // Line 2: <Lorem Ipsum Generator website used> 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+: <lorem ipsum generated with 3 paragraphs> 
    //  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 << "Read File: " << file_name << " - Encrypted To: " << encrypted_file_name << " - Decrypted To: " << decrypted_file_name << std::endl;

    // students submit input file, encrypted file, decrypted file, source code file, and key used
}

Aucun commentaire:

Enregistrer un commentaire