Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Encryption.cpp : This file contains the 'main' function. Program execution begins and ends there.
- #include "pch.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <cstdint>
- #define DELTA 0x9e3779b9
- #define MX (((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (key[(p&3)^e] ^ z)))
- void btea(uint32_t *v, int n, uint32_t const key[4]) {
- uint32_t y, z, sum;
- unsigned p, rounds, e;
- if (n > 1) { /* Coding Part */
- rounds = 6 + 52 / n;
- sum = 0;
- z = v[n - 1];
- do {
- sum += DELTA;
- e = (sum >> 2) & 3;
- for (p = 0; p < n - 1; p++) {
- y = v[p + 1];
- z = v[p] += MX;
- }
- y = v[0];
- z = v[n - 1] += MX;
- } while (--rounds);
- }
- else if (n < -1) { /* Decoding Part */
- n = -n;
- rounds = 6 + 52 / n;
- sum = rounds * DELTA;
- y = v[0];
- do {
- e = (sum >> 2) & 3;
- for (p = n - 1; p > 0; p--) {
- z = v[p - 1];
- y = v[p] -= MX;
- }
- z = v[n - 1];
- y = v[0] -= MX;
- sum -= DELTA;
- } while (--rounds);
- }
- }
- int main(int argc, char** argv)
- {
- std::cout << "you have entered" << argc << "arguments: " << "\n";
- for (auto i = 0; i < argc; ++i)
- {
- std::cout << argv[i] << "\n\n";
- }
- //uint32_t key[4];
- //key[3] = argv[2][3] - 48;
- //key[2] = argv[2][2] - 48;
- //key[1] = argv[2][1] - 48;
- //key[0] = argv[2][0] - 48;
- //const int n = 128 / sizeof(uint32_t);
- //char TestStr[128] = "1234567";
- //std::cout << "\nTest: \n";
- //printf("%s\n\n", TestStr);
- //btea(reinterpret_cast<uint32_t*>(TestStr), n, key);
- //std::cout << "Encrypted: \n";
- //printf("%s\n\n", TestStr);
- //btea(reinterpret_cast<uint32_t*>(TestStr), -n, key);
- //std::cout << "Decrypted: \n";
- //printf("%s\n\n", TestStr);
- //ENCRYPT OR DECRYPT COMMAND
- const auto opType = argv[1];
- std::vector<char> toProcess;
- //READING INPUT FILE
- const auto inputfile = argv[3];
- std::ifstream ifstreamFile;
- ifstreamFile.open(inputfile);
- if (ifstreamFile.is_open())
- {
- if (static_cast<std::string>(opType) == "Encrypt" || static_cast<std::string>(opType) == "encrypt")
- {
- std::cout << "Start encryption process >> \n";
- while (!ifstreamFile.eof())
- {
- toProcess.push_back(ifstreamFile.get());
- }
- }
- else if (static_cast<std::string>(opType) == "Decrypt" || static_cast<std::string>(opType) == "decrypt")
- {
- std::cout << "Start decryption process >> \n";
- while (!ifstreamFile.eof())
- {
- toProcess.push_back(ifstreamFile.get());
- }
- }
- else
- std::cout << "!!cmd argument 1 >> Operation type Invalid input!!";
- std::cout << "Input: \n";
- for (auto cha : toProcess)
- {
- std::cout << cha;
- }
- std::cout << '\n';
- }
- else
- std::cout << "Source File >> " << inputfile << " >> FAILED TO OPEN\n";
- ifstreamFile.close();
- //GETTING PASSWORD
- uint32_t key[4];
- key[3] = argv[2][3] - 48;
- key[2] = argv[2][2] - 48;
- key[1] = argv[2][1] - 48;
- key[0] = argv[2][0] - 48;
- //HANDLING PRE-ENCRYPTION
- const auto initSize = toProcess.size();
- const unsigned int c = 40;
- const int paddingsize = 4 - (toProcess.size() % 4);
- const auto cc = toProcess.size() + paddingsize;
- int nCounter = cc / sizeof(uint32_t);
- std::cout << "\nTo process>> \n";
- if (static_cast<std::string>(opType) == "Encrypt" || static_cast<std::string>(opType) == "encrypt")
- {
- toProcess.pop_back();
- for (unsigned int i = 0; i < cc; ++i)
- {
- if(i >= initSize - 1 && i < cc - 1)
- {
- toProcess.push_back('l');
- }
- else if (i == cc - 1)
- {
- toProcess.push_back(static_cast<char>(paddingsize));
- }
- //if (i >= ii - 1 && i < c -1)
- //{
- // toProcess.push_back('l');
- //}
- //else if (i == c)
- //{
- // toProcess.push_back(0);
- //}
- //else
- //{
- // //toProcess[i] = inputChars[i];
- //}
- std::cout << toProcess[i];
- }
- }
- else if (static_cast<std::string>(opType) == "Decrypt" || static_cast<std::string>(opType) == "decrypt")
- {
- toProcess.pop_back();
- for (unsigned int i = 0; i < toProcess.size() - 1; ++i)
- {
- std::cout << toProcess[i];
- }
- nCounter = toProcess.size() / sizeof(uint32_t);
- }
- std::cout << '\n' << "InitSize: " << initSize << " :: Size: " << toProcess.size() << " :: nCounter: " << nCounter << '\n';
- //PROCESSING
- std::cout << "\nOutput>> \n";
- if (static_cast<std::string>(opType) == "Encrypt" || static_cast<std::string>(opType) == "encrypt")
- {
- btea(reinterpret_cast<uint32_t*>(toProcess.data()), nCounter, key);
- std::cout << toProcess.data() << std::endl;
- std::cout << '\n';
- for (auto i = 0; i < toProcess.size(); i++)
- {
- std::cout << toProcess[i];
- }
- std::cout << '\n';
- }
- else if (static_cast<std::string>(opType) == "Decrypt" || static_cast<std::string>(opType) == "decrypt")
- {
- btea(reinterpret_cast<uint32_t*>(&toProcess[0]), -nCounter, key);
- std::cout << &toProcess[0] << std::endl;
- /* for (auto& toProces : toProcess)
- {
- if (toProces == 'l')
- {
- toProces = 0;
- }
- }*/
- std::cout <<"\n\nUn-Padded>>\n" << &toProcess[0] << std::endl;
- }
- //if (static_cast<std::string>(opType) == "Decrypt" || static_cast<std::string>(opType) == "decrypt")
- //{
- // //HANDLING POST-DECRYPTION
- // for (auto i = 0; i < 5; ++i)
- // {
- // toProcess.pop_back();
- // }
- // std::cout << "\n\nUn-Padded>>\n" << toProcess.data() << std::endl;
- //}
- std::cout << '\n' << "InitSize: " << initSize << " :: Size: " << toProcess.size() << " :: nCounter: " << nCounter << '\n';
- //WRITING OUTPUT FILE
- const auto outputfile = argv[4];
- std::ofstream ofstreamFile;
- ofstreamFile.open(outputfile);
- if (ofstreamFile.is_open())
- {
- //streamFile << &toProcess[0];
- for (char toProces : toProcess)
- {
- ofstreamFile << toProces;
- std::cout << toProces;
- }
- }
- else
- std::cout << "Source File >> " << outputfile << " >> FAILED TO OPEN\n";
- ofstreamFile.close();
- std::cout << '\n';
- std::cin.ignore();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement