Advertisement
Guest User

Untitled

a guest
Oct 7th, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <limits.h>
  4. #include <fstream>
  5.  
  6.  
  7.  
  8. void CheckAsciiPos(short& pos) {
  9.     if (std::numeric_limits<char>::is_signed) {
  10.         if (pos > 127) //if it leaves ascii table :)
  11.             pos -= 256;
  12.         if (pos < -128)
  13.             pos += 256;
  14.     }
  15.     else {
  16.         if (pos > 255) //if it leaves ascii table :)
  17.             pos -= 256;
  18.         if (pos < 0)
  19.             pos += 256;
  20.     }
  21. }
  22.  
  23.  
  24.  
  25. //((P XOR((K + KC) XOR K)) + ((K + KC) XOR K))
  26. std::string encrypt(std::string plaintext, std::string password) {
  27.  
  28.     int passPos = 0;
  29.     short asciiPosCipher;
  30.     short passCounter = 1;
  31.     short asciiPosPass;
  32.     std::string cipher;
  33.  
  34.     for (unsigned int i = 0; i < plaintext.length(); i++) { //do for every character in string
  35.  
  36.         if (passPos >= password.length()) //after password ends start
  37.             passPos = 0; //start from the beginning
  38.         if (passCounter > 255)
  39.             passCounter = 0;
  40.         asciiPosPass = (password[passPos] + passCounter); //shift password char around passCounter
  41.         CheckAsciiPos(asciiPosPass); //checks and corrects position pos in asciitable
  42.         asciiPosPass ^= password[passPos]; //xor shifted password char with original password char
  43.         asciiPosCipher = plaintext[i] ^ asciiPosPass; //xor this with plaintext char
  44.         asciiPosCipher += asciiPosPass; //shift both to result
  45.         CheckAsciiPos(asciiPosCipher);
  46.         cipher += static_cast<char>(asciiPosCipher); //cast them to char an add to cipher-text
  47.         passPos++; //next char in password
  48.         passCounter++;
  49.     }
  50.  
  51.     return cipher;
  52. }
  53.  
  54.  
  55. //((C - ((K + KC) XOR K)) XOR ((K + KC) XOR K))
  56. std::string decrypt(std::string cipher, std::string password) {
  57.  
  58.     int passPos = 0;
  59.     short asciiPosResult;
  60.     short passCounter = 1;
  61.     short asciiPosPass;
  62.     std::string result;
  63.  
  64.     for (unsigned int i = 0; i < cipher.length(); i++) {
  65.  
  66.         if (passPos >= password.length())
  67.             passPos = 0;
  68.         if (passCounter > 255)
  69.             passCounter = 0;
  70.         asciiPosPass = (password[passPos] + passCounter);
  71.         CheckAsciiPos(asciiPosPass);
  72.         asciiPosPass ^= password[passPos];
  73.         asciiPosResult = cipher[i] - asciiPosPass;
  74.         asciiPosResult ^= asciiPosPass;
  75.         CheckAsciiPos(asciiPosResult);
  76.         result += static_cast<char>(asciiPosResult);
  77.         passPos++;
  78.         passCounter++;
  79.     }
  80.  
  81.     return result;
  82. }
  83.  
  84.  
  85.  
  86. int main() {
  87.  
  88.     std::string fileName = "test.exe";
  89.     std::ifstream input(fileName, std::ios::binary);
  90.    
  91.     std::string plaintext;
  92.  
  93.     char byte;
  94.     while (input.get(byte)) {
  95.         plaintext += byte;
  96.     }
  97.  
  98.  
  99.     std::string password = "testpass";
  100.     std::string cipher;
  101.     std::string result;
  102.  
  103.     cipher = encrypt(plaintext, password);
  104.  
  105.     std::ofstream enc("test_enc.exe", std::ios::app | std::ios::binary);
  106.     enc << cipher;
  107.     enc.close();
  108.  
  109.     result = decrypt(cipher, password);
  110.  
  111.     std::ofstream dec("test_dec.exe", std::ios::app | std::ios::binary);
  112.     dec << result;
  113.     dec.close();
  114.  
  115.  
  116.     std::cin.get();
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement