XOR encryption
Quick and messy XOR encryptor. Reads in a string of any length and outputs a binary string of it xored with the key hardcoded in the source. See the decrypter
#include <iostream> #include <string> #include <cmath> #include <vector> using namespace std; vector<int> to_binary(string binarify_me) { vector<int> return_me; for(int i=0; i<binarify_me.size(); i++) { vector<int> temp; int char_as_int = (int)binarify_me[i]; while(char_as_int != 0) { if(char_as_int % 2 == 0) temp.push_back(0); else temp.push_back(1); char_as_int /= 2; } while(temp.size() != 8) temp.push_back(0); for(int i=temp.size()-1; i>=0; --i) return_me.push_back(temp[i]); } return return_me; } vector<int> xor_encrypt(vector<int> encrypt_me, vector<int> with_me) { vector<int> return_me; for(int i=0; i<encrypt_me.size(); i++) { int key_index = i; while(key_index > with_me.size()) key_index = key_index-with_me.size(); int xored = encrypt_me[i] ^ with_me[key_index]; return_me.push_back(xored); } return return_me; } string from_binary(vector<int> stringify_me) { string return_me = ""; for(int i=7; i<stringify_me.size(); i+=8) { int char_as_int = 0; for(int j=0; j<8; j++) char_as_int += stringify_me[i-j] * pow(2, j); return_me += (char)char_as_int; } return return_me; } int main() { string encryption_key = "this is the key."; vector<int> binary_key = to_binary(encryption_key); string encrypt_me; getline(cin, encrypt_me); vector<int> binary_encrypt_me = to_binary(encrypt_me); vector<int> encrypted = xor_encrypt(binary_encrypt_me, binary_key); for(int i=0; i<encrypted.size(); i++) cout << encrypted[i]; cout << endl; return 0; }

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Download this code in plain text format here
Comments: