Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ----------------------------------------------------------------------------------------------------------------------------
- //
- // compiles as :- gcc -O2 -o file_crypt file_crypt.cpp -lcrypto -Wall -lstdc++ on ubuntu with gcc v8.0
- // usage to encrypt :- ./file_crypt input_file_name.txt --> outputs to crypted_msg.txt
- // decrypt :- ./file_crypt crypted_msg.txt
- //
- // ----------------------------------------------------------------------------------------------------------------------------
- #include <iostream>
- #include <string>
- #include <cstdlib>
- #include <time.h>
- #include <locale>
- //#include <Windows.h>
- #include <fstream>
- #include <cstring>
- #include <iostream>
- #include <cctype>
- using namespace std;
- char* g_alphabetENG = new char[26]{ 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z' };
- char* input(char*& path, int& raw_length, int& msg_length) {
- int length = 0;
- ifstream is;
- is.open(path, ios::binary);
- is.seekg(0, ios::end);
- length = is.tellg();
- char* buffer = new char[length];
- is.seekg(0, ios::beg);
- is.read(buffer, length);
- is.close();
- int j = 0;
- for (int i = 0; i < length; ++i) {
- ++j;
- if ((int)buffer[i] != 32 && (int)buffer[i] != 10 && (int)buffer[i] != 13)
- msg_length++;
- if (isupper(buffer[i])) {
- buffer[i] = tolower(buffer[i]);
- }
- }
- raw_length = length;
- return buffer;
- }
- void output(char* buffer, const int& raw_length) {
- ofstream out;
- out.open("crypted_msg.txt", ios::binary);
- out.write(buffer, raw_length);
- out.close();
- }
- // caesar 's cipher
- void caesar(const int& change_Caesar, char*& encrypt, char*& alphabet, const int& len, const int& lenalp) {
- char help;
- for (int i = 0; i < len; ++i) {
- for (int j = 0; j < lenalp; ++j) {
- if (encrypt[i] == alphabet[j]) {
- if (j + change_Caesar >= lenalp)
- help = alphabet[change_Caesar + j - lenalp];
- else
- help = alphabet[j + change_Caesar];
- encrypt[i] = help;
- cout << help;
- break;
- }
- }
- }
- cout << " - " << "Encrypted with caesars cypher" << endl;
- }
- // atbash cipher
- void atbash(char*& encrypt, char*& alphabet, const int& len, const int& lenalp) {
- char help;
- for (int i = 0; i < len; ++i) {
- for (int j = 0; j < lenalp; ++j) {
- if (encrypt[i] == alphabet[j]) {
- help = alphabet[lenalp - j - 1];
- encrypt[i] = help;
- cout << help;
- break;
- }
- }
- }
- cout << " - " << "Encrypted with the atbash cipher" << endl;
- }
- // weiners cipher
- void weiner(char*& encrypt, char*& alphabet, const int& len, const string& key, const int& lenalp) {
- int* change = new int[len];
- int lenkey = key.length();
- char help;
- int help2;
- for (int i = 0, k = 0; i < len; ++i) {
- if (k + 1 != lenkey) {
- help = key[k];
- k++;
- }
- else {
- help = key[k];
- k = 0;
- }
- for (int j = 0; j < lenalp; ++j) {
- if (help == alphabet[j]) {
- help2 = j + 1;
- change[i] = help2;
- }
- }
- }
- for (int i = 0; i < len; ++i) {
- help2 = change[i];
- for (int j = 0; j < lenalp; ++j) {
- if (encrypt[i] == alphabet[j]) {
- if (j + help2 >= lenalp)
- help = alphabet[help2 + j - lenalp];
- else
- help = alphabet[j + help2];
- encrypt[i] = help;
- cout << help;
- break;
- }
- }
- }
- delete[] change;
- cout << " - " << "Encrypted with the weiner cypher" << endl;
- }
- // reverse order cipher
- void reverse(char*& encrypt, const int& len) {
- for (int i = 0; i < len / 2; ++i) {
- char rev = encrypt[i];
- encrypt[i] = encrypt[len - i - 1];
- encrypt[len - i - 1] = rev;
- }
- for (int i = 0; i < len; ++i)
- cout << encrypt[i];
- cout << " - " << "Encrypted with a reverse-order cipher" << endl;
- }
- // cipher a1z26
- void a1z26(char*& encrypt, char*& alphabet, const int& len, const int& lenalp, char*& fbuffer, const int& fbuffer_size, char*& sm_buffer, const int& change_Caesar) {
- int sm_count = 0, b_count = 0;
- for (int i = 0; i < (fbuffer_size - 5); ) {
- if ((int)encrypt[b_count] == 32 || (int)encrypt[b_count] == 10 || (int)encrypt[b_count] == 13) {
- fbuffer[i] = encrypt[b_count];
- b_count++;
- i++;
- }
- else {
- for (int j = 0; j < lenalp; ++j) {
- if (sm_buffer[sm_count] == alphabet[j]) {
- if ((j + 1) < 10) {
- fbuffer[i] = '0';
- fbuffer[i + 1] = (j + 1) + '0';
- fbuffer[i + 2] = '-';
- }
- else {
- fbuffer[i] = ((j + 1) / 10) + '0';
- fbuffer[i + 1] = ((j + 1) % 10) + '0';
- fbuffer[i + 2] = '-';
- }
- cout << fbuffer[i] << fbuffer[i + 1] << fbuffer[i+2];
- i += 3;
- break;
- }
- }
- b_count++;
- sm_count++;
- }
- }
- if (change_Caesar < 10) {
- fbuffer[(fbuffer_size - 1) - 1] = '0';
- fbuffer[(fbuffer_size - 1)] = change_Caesar + '0';
- }
- else {
- fbuffer[(fbuffer_size - 1) - 1] = (change_Caesar / 10) + '0';
- fbuffer[(fbuffer_size - 1)] = (change_Caesar % 10) + '0';
- }
- fbuffer[(fbuffer_size - 1) - 2] = '-';
- if (alphabet[1] == 'b') {
- fbuffer[(fbuffer_size - 1) - 3] = '2';
- fbuffer[(fbuffer_size - 1) - 4] = '5';
- }
- else {
- fbuffer[(fbuffer_size - 1) - 3] = '6';
- fbuffer[(fbuffer_size - 1) - 4] = '6';
- }
- cout << " - " << "Encrypted with a1z26" << endl << endl;
- }
- // rot13 cipher
- void rot13CipherEncode( char*& cipherText, char*& message, size_t raw_length )
- {
- char c;
- size_t i;
- for (i = 0; i < raw_length; i++)
- {
- c = cipherText[i];
- if (c >= 'a' && c <= 'z')
- {
- c = c + 13;
- if (c > 'z')
- {
- c = c - 26;
- }
- }
- else if (c >= 'A' && c <= 'Z')
- {
- c = c + 13;
- if (c > 'Z')
- {
- c = c - 26;
- }
- }
- message[i] = c;
- }
- message[raw_length] = '\0';
- }
- // rot13 de-cipher
- void rot13CipherDecode( char*& cipherText, char*& message, size_t raw_length )
- {
- char c;
- size_t i;
- for (i = 0; i < raw_length; i++)
- {
- c = message[i];
- if (c >= 'a' && c <= 'z')
- {
- c -= 13;
- if (c < 'a')
- {
- c += 26;
- }
- }
- else if (c >= 'A' && c <= 'Z')
- {
- c -= 13;
- if (c < 'A')
- {
- c += 26;
- }
- }
- cipherText[i] = c;
- }
- }
- void DEreverse(char*& encrypt, const int& len) {
- for (int i = 0; i < len / 2; ++i) {
- char rev = encrypt[i];
- encrypt[i] = encrypt[len - i - 1];
- encrypt[len - i - 1] = rev;
- }
- for (int i = 0; i < len; ++i)
- cout << encrypt[i];
- cout << " - " << "Decrypted by a reverse - order cipher" << endl;
- }
- void DEweiner(char*& crypt, char*& alphabet, const int& len, const string& key, const int& lenalp) {
- int lenkey = key.length();
- int* change = new int[len];
- char help;
- int help2;
- for (int i = 0, k = 0; i < len; ++i) {
- if (k + 1 != lenkey) {
- help = key[k];
- k++;
- }
- else {
- help = (char)(key[k]);
- k = 0;
- }
- for (int j = 0; j < lenalp; ++j) {
- if (help == alphabet[j]) {
- help2 = j + 1;
- change[i] = help2;
- }
- }
- }
- for (int i = 0; i < len; ++i) {
- help2 = change[i];
- for (int j = 0; j < lenalp; ++j) {
- if (crypt[i] == alphabet[j]) {
- if (j + (lenalp - help2) >= lenalp)
- help = alphabet[(lenalp - help2) + j - lenalp];
- else
- help = alphabet[j + (lenalp - help2)];
- crypt[i] = help;
- cout << help;
- break;
- }
- }
- }
- cout << " - " << "Deciphered by the weiner cipher" << endl;
- delete[] change;
- }
- // decipher using atbash
- void DEatbash(char*& crypt, char*& alphabet, const int& len, const int& lenalp) {
- char help;
- for (int i = 0; i < len; ++i) {
- for (int j = 0; j < lenalp; ++j) {
- if (crypt[i] == alphabet[j]) {
- help = alphabet[lenalp - j - 1];
- crypt[i] = help;
- cout << help;
- break;
- }
- }
- }
- cout << " - " << "Deciphered by the atbash cipher" << endl;
- }
- // decipher using caesar
- void DEcaesar(const int& DEchange_Caesar, char*& crypt, char*& alphabet, int& len, const int& lenalp, char*& buffer, int& raw_length) {
- char help;
- for (int i = 0; i < len; ++i) {
- for (int j = 0; j < lenalp; ++j) {
- if (crypt[i] == alphabet[j]) {
- if (j + (lenalp - DEchange_Caesar) >= lenalp)
- help = alphabet[(lenalp - DEchange_Caesar) + j - lenalp];
- else
- help = alphabet[j + (lenalp - DEchange_Caesar)];
- cout << help;
- crypt[i] = help;
- break;
- }
- }
- }
- cout << " - " << "Deciphered by Caesar 's cipher" << endl << endl;
- int sm_count = 0, b_count = 0;
- len = ((raw_length - 5) - len * 3) + len;
- char* fbuffer = new char[len];
- for (int i = 0; i < raw_length - 5; ) {
- if ((int)buffer[i] == 32 || (int)buffer[i] == 10 || (int)buffer[i] == 13) {
- fbuffer[sm_count] = buffer[i];
- sm_count++;
- i++;
- }
- else {
- fbuffer[sm_count] = crypt[b_count];
- b_count++;
- sm_count++;
- i += 3;
- }
- }
- crypt = fbuffer;
- }
- int setting_up(char first_letter, char*& lang, int& change_Caesar, bool& crypt, int& lenalp, char*& buffer, char*& n_buffer, int& raw_length, int& msg_length) {
- //SetConsoleCP(1251);
- //SetConsoleOutputCP(1251);
- //setlocale(LC_ALL, "Russian");
- srand(time(NULL));
- if (int(first_letter) >= 97 && int(first_letter) <= 122) {
- lang = g_alphabetENG;
- crypt = true;
- lenalp = 26;
- }
- else if (int(first_letter) >= 65 && int(first_letter) <= 90) {
- lang = g_alphabetENG;
- crypt = true;
- lenalp = 26;
- }
- else if (first_letter - '0' >= 0 && first_letter - '0' < 10) {
- crypt = false;
- if (buffer[(raw_length - 1) - 3] == '2') {
- lang = g_alphabetENG;
- lenalp = 26;
- }
- }
- else {
- cout << "error, wrong text language or crypted msg";
- system("pause");
- }
- if (crypt == true)
- change_Caesar = rand() % 25 + 1;
- else {
- if (buffer[(raw_length - 1) - 1] == '0')
- change_Caesar = buffer[(raw_length - 1)] - '0';
- else
- change_Caesar = ((buffer[(raw_length - 1) - 1] - '0') * 10) + (buffer[(raw_length - 1)] - '0');
- }
- int k = 0;
- char* g_sm_buffer;
- if (crypt == true) {
- g_sm_buffer = new char[msg_length];
- for (int i = 0; i < raw_length; ) {
- if ((int)buffer[i] != 32 && (int)buffer[i] != 10 && (int)buffer[i] != 13) {
- g_sm_buffer[k] = buffer[i];
- ++k;
- ++i;
- }
- else
- i++;
- }
- }
- else {
- msg_length = (msg_length - 5) / 3;
- g_sm_buffer = new char[msg_length];
- for (int i = 0; i < (raw_length - 5); ) {
- if ((int)buffer[i] != 32 && (int)buffer[i] != 10 && (int)buffer[i] != 13) {
- if (buffer[i] == '0') {
- g_sm_buffer[k] = lang[(buffer[i + 1] - '0') - 1];
- k++;
- i += 3;
- }
- else {
- g_sm_buffer[k] = lang[(((buffer[i] - '0') * 10) + (buffer[i + 1] - '0')) - 1];
- k++;
- i += 3;
- }
- }
- else
- i++;
- }
- }
- n_buffer = g_sm_buffer;
- return 1;
- }
- int main(int argc, char* argv[]) {
- int raw_length, change_Caesar, lenalp, msg_length = 0;
- char* lang;
- char* sm_buffer;
- bool crypt = true;
- char* buffer = input(argv[1], raw_length, msg_length);
- sm_buffer = new char[msg_length];
- try {
- if (setting_up(buffer[0], lang, change_Caesar, crypt, lenalp, buffer, sm_buffer, raw_length, msg_length) == 0)
- return 0;
- } catch (...) {
- cout << "error setting up" << endl;
- }
- string key;
- char* fbuffer;
- char* message;
- int fbuffer_size;
- cout << "Enter the cipher key: ";
- getline(cin, key);
- if (crypt == true) {
- // encrypt the message and write it to the file
- fbuffer_size = 3 * msg_length + (raw_length - msg_length) + 5;
- fbuffer = new char[fbuffer_size];
- caesar(change_Caesar, sm_buffer, lang, msg_length, lenalp);
- atbash(sm_buffer, lang, msg_length, lenalp);
- weiner(sm_buffer, lang, msg_length, key, lenalp);
- reverse(sm_buffer, msg_length);
- a1z26(buffer, lang, raw_length, lenalp, fbuffer, fbuffer_size, sm_buffer, change_Caesar);
- output(fbuffer, fbuffer_size);
- cout << "output is crypted_msg.txt" << endl;
- // just example of how to call rot13
- message = new char[msg_length];
- rot13CipherEncode( sm_buffer, message, static_cast<size_t>(msg_length) );
- cout << "with rot13 cipher (just printed here) " << message << " from the output of the reverse cipher " << sm_buffer << endl;
- delete[] fbuffer;
- delete[] message;
- }
- else {
- // decrypt the message and write it to the file
- DEreverse(sm_buffer, msg_length);
- DEweiner(sm_buffer, lang, msg_length, key, lenalp);
- DEatbash(sm_buffer, lang, msg_length, lenalp);
- DEcaesar(change_Caesar, sm_buffer, lang, msg_length, lenalp, buffer, raw_length);
- output(sm_buffer, msg_length);
- }
- delete[] buffer;
- delete[] sm_buffer;
- delete[] lang;
- return 0;
- }
Add Comment
Please, Sign In to add comment