Simple XOR Crypter
February 13th, 2008
Exclusive or is a type of logical disjunction on two operands that results in a value of âtrueâ if and only if exactly one of the operands has a value of âtrueâ.
The truth table is as follows:
a | b | a XOR b
—-+—-+———
0 | 0 | 1
0 | 1 | 0
1 | 0 | 0
1 | 1 | 1
- 1 xor 1 = 0
- 1 xor 0 = 1
- 1110 xor 1001 = 0111
XOR can be used to swap two numeric variables, using the XOR swap algorithm; however it’s regarded as more of a curiosity and not encouraged in practice.
As you know ^ declares XOR in C/C++.
void XorSwap(int* x, int* y) { if (x != y) { *x ^= *y; // the right-most expression *y ^= *x; // the middle expression *x ^= *y; // the left-most expression } }
A reson why you’d better avoid excessive use of XOR is that on modern CPUs, the XOR technique is considerably slower than using a temporary variable to do swapping. One reason is that modern CPUs strive to execute commands in parallel. In the XOR technique, the inputs to each operation depend on the results of the previous operation, so they must be executed in strictly sequential order.
XOR can simply be used for an easy to understand encryption algorithm.
In an XOR encryption program each letter of the key and the input(e.g text) gets converted to ascii, then to binary (0’s and 1’s). The 0 or 1 from the key is xored against the 0 or 1 of the text.
Consider that function:
string XOR(string value,string key) { string retval(value); short unsigned int klen=key.length(); short unsigned int vlen=value.length(); short unsigned int k=0; short unsigned int v=0; for(v;v<vlen;v++) { retval[v] = value[v] ^ key[k]; k=(++k < klen ? k : 0); } return retval; }
It can be used like this:
string value("Hello World"); string key("key"); cout << "Plain text: " << value << "\n\n"; value = XOR(value,key); cout << "Cipher text: " << value << "\n\n"; value = XOR(value,key); cout << "Decrypted text: " <<value << std::endl;
XORing is used in many cryptographic algorithms to produce a ciphertext from the key and plaintext - especially in so-called “stream ciphers”.
You might have noticed that this encryption is similar to Vigenère Cipher method and basically an enhanced version of it. XOR encryption takes the result of XORing the ascii number of the value and the number of the key value to produce a crypted value. Vigenère Cipher takes the result of adding (modulo 255) the ascii number of the value to the ascii number of value to produce a crypted value. That’s the reason why XOR encryption is more attractive than the addition function XOR is a symmetric operation, whereas addition is not.
If you’d like to use XOR encryption I would therefore advice you that function, cause it’s valid C++ and uses a template. You don’t have to care about datatypes you like to crypt/encrypt.
template<typename> void BetterXor(const T& value, const T& key, T& result) { typedef unsigned char byte; const byte* pvalue = reinterpret_cast<const>(&value); const byte* pkey = reinterpret_cast<const>(&key); byte* pResult = reinterpret_cast[byte*](&result); for( size_t i = 0; i < sizeof(value) && i < sizeof(key); ++i ) pResult[i] = pvalue[i] ^ pkey[i]; }
Nevertheless if you need strong encryption, DO NOT USE XOR algorithms. If true security is an issue then you will want to use something else.





