Created
October 14, 2013 22:03
-
-
Save lifuzu/6982989 to your computer and use it in GitHub Desktop.
XOR in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <bitset> | |
static const std::string base64_chars = | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
"abcdefghijklmnopqrstuvwxyz" | |
"0123456789+/"; | |
std::string base64_encode(unsigned char const* , unsigned int len); | |
std::string base64_decode(std::string const& s); | |
std::string XOR(std::string value, std::string key); | |
static inline bool is_base64(unsigned char c) { | |
return (isalnum(c) || (c == '+') || (c == '/')); | |
} | |
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) { | |
std::string ret; | |
int i = 0; | |
int j = 0; | |
unsigned char char_array_3[3]; | |
unsigned char char_array_4[4]; | |
while (in_len--) { | |
char_array_3[i++] = *(bytes_to_encode++); | |
if (i == 3) { | |
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; | |
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); | |
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); | |
char_array_4[3] = char_array_3[2] & 0x3f; | |
for(i = 0; (i <4) ; i++) | |
ret += base64_chars[char_array_4[i]]; | |
i = 0; | |
} | |
} | |
if (i) | |
{ | |
for(j = i; j < 3; j++) | |
char_array_3[j] = '\0'; | |
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; | |
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); | |
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); | |
char_array_4[3] = char_array_3[2] & 0x3f; | |
for (j = 0; (j < i + 1); j++) | |
ret += base64_chars[char_array_4[j]]; | |
while((i++ < 3)) | |
ret += '='; | |
} | |
return ret; | |
} | |
std::string base64_decode(std::string const& encoded_string) { | |
int in_len = encoded_string.size(); | |
int i = 0; | |
int j = 0; | |
int in_ = 0; | |
unsigned char char_array_4[4], char_array_3[3]; | |
std::string ret; | |
while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { | |
char_array_4[i++] = encoded_string[in_]; in_++; | |
if (i ==4) { | |
for (i = 0; i <4; i++) | |
char_array_4[i] = base64_chars.find(char_array_4[i]); | |
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); | |
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); | |
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; | |
for (i = 0; (i < 3); i++) | |
ret += char_array_3[i]; | |
i = 0; | |
} | |
} | |
if (i) { | |
for (j = i; j <4; j++) | |
char_array_4[j] = 0; | |
for (j = 0; j <4; j++) | |
char_array_4[j] = base64_chars.find(char_array_4[j]); | |
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); | |
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); | |
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; | |
for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; | |
} | |
return ret; | |
} | |
std::string XOR(std::string value, std::string key) | |
{ | |
std::string retval(value); | |
long unsigned int klen = key.length(); | |
long unsigned int vlen = value.length(); | |
unsigned long int k = 0; | |
unsigned long int v = 0; | |
for (; v < vlen; v++) { | |
retval[v] = value[v] ^ key[k]; | |
k = (++k < klen ? k : 0); | |
} | |
return retval; | |
} | |
/* | |
// The following functions are untested | |
std::string AND(std::string value, std::string key) | |
{ | |
uint32_t klen = key.length(); | |
uint32_t vlen = value.length(); | |
uint32_t max_len = (klen > vlen) ? klen : vlen; | |
char * ret_str = (char *)calloc(max_len, sizeof(char)); | |
const char * v_str = value.c_str(); | |
const char * k_str = key.c_str(); | |
for (uint32_t i = 0; i < max_len; i++) | |
{ | |
if (i >= klen || i >= vlen) | |
ret_str[i] = 0x00; | |
else | |
ret_str[i] = v_str[i] & k_str[i]; | |
} | |
std::string to_return(ret_str); | |
free(ret_str); | |
return to_return; | |
} | |
std::string NOT(std::string value) | |
{ | |
const char v_str = value.c_str(); | |
char * ret_str = (char *)calloc(value.size(), sizeof(char)); | |
for (uint32_t i = 0; i < value.size(); i++) | |
{ | |
ret_str[i] = ~(v_str[i]); | |
} | |
string to_return(ret_str); | |
free(ret_str); | |
return to_return; | |
} | |
std::string OR(std::string value, std::string key) | |
{ | |
uint32_t klen = key.length(); | |
uint32_t vlen = value.length(); | |
uint32_t max_len = (klen > vlen) ? klen : vlen; | |
char * ret_str = (char *)calloc(max_len, sizeof(char)); | |
const char * v_str = value.c_str(); | |
const char * k_str = key.c_str(); | |
for (uint32_t i = 0; i < max_len; i++) | |
{ | |
if (i >= klen || i >= vlen) | |
{ | |
ret_str[i] = (klen > vlen) ? k_str[i] : v_str[i]; | |
} | |
else | |
{ | |
ret_str[i] = v_str[i] | k_str[i]; | |
} | |
} | |
std::string to_return(ret_str); | |
free(ret_str); | |
return to_return; | |
} | |
*/ | |
int main(void) { | |
const std::string a = "hello?"; | |
const std::string b = "world!"; | |
std::string c = XOR(a, b); | |
//std::cout << "original: " << std::hex << c << std::endl; | |
std::string encoded = base64_encode(reinterpret_cast<const unsigned char *>(c.c_str()), c.length()); | |
std::string decoded = base64_decode(encoded); | |
std::cout << "encoded: " << encoded << std::endl; | |
//std::cout << "decoded: " << std::hex << decoded << std::endl; | |
if(c.compare(decoded) == 0) { | |
std::cout << "same!" << std::endl; | |
} else { | |
std::cout << "different!" << std::endl; | |
} | |
std::string d = XOR(c, b); | |
std::cout << "original a: " << a << std::endl; | |
std::cout << "original d: " << d << std::endl; | |
const std::string e = "hello&world!"; | |
encoded = base64_encode(reinterpret_cast<const unsigned char *>(e.c_str()), e.length()); | |
decoded = base64_decode(encoded); | |
std::cout << "original: " << e << std::endl; | |
std::cout << "encoded: " << encoded << std::endl; | |
std::cout << "decoded: " << decoded << std::endl; | |
const std::string r("Official NBC site for primetime, daytime and late night television shows. Television (TV) network includes soap opera, reality shows and more. Find additional"); | |
std::string s(""); | |
s += r[9]; | |
s += r[19]; | |
s += r[0]; | |
s += r[96]; | |
std::cout << "output:" << r[9] << r[19] << r[0] << r[96] << std::endl; | |
std::cout << "output:" << s << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
g++ -Wall -o xor xor.cpp