- Code: Select all
/*--------------------------------------------------------------------------------
===== Copyright =====
Copyright (c) 2010 Fabian Heredia
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
===== Explanation =====
The XOR cipher is based uppon boolean logic XOR. The cipher uses the 2
strings[plain & key] to safely encrypt the first(bit by bit).
Table:
---------------
| A | B | XOR |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
---------------
If you have acces to at least two of the three [plain, key, ciphered]
the remaining one is no longer protected. It is recommended to use a key
at most once since an attacker could start seeing patterns.
--------------------------------------------------------------------------------*/
#include <iostream>
#include <fstream>
#include <string>
#include <limits.h>
using namespace std;
// Cipher explained avobe.
string cipher(string toCrypt, string key){
string crypted = toCrypt;
unsigned int iK = key.length(), iC = toCrypt.length();
for(unsigned int i = 0, j = 0; i < iC; i++){
crypted[i] = toCrypt[i] ^ key[j]; // ^ is the operator for Bitwise XOR
if(j++ == iK){j = 0;}
}
return crypted;
}
// Function for writing into a file.
int write(string toWrite, string file)
{
ofstream output(file.c_str(), ios::binary);
if(output.is_open())
{
output << toWrite;
output.close();
}
else { return 1;}
return 0;
}
int main(int argc, char *argv[]){
// If we recieve exactly 3 arguments.[Plain, Key, ,and Output file]
if(argc == 4)
{
if(write(cipher(argv[1], argv[2]), argv[3]))
{
cout << "FAILED";
}
}
// If we recieve exactly 2 arguments.[File and Key]
else if(argc == 3)
{
// TODO: File Manipulation
}
else if(argc > 1)
{
cout << argv[0] << endl;
cout << "Usage:" << endl;
cout << "\t1) 0 arguments - Utility for ciphering a file/text." << endl;
cout << "\t2) 2 Arguments - [File to cipher] and [key]" << endl;
cout << "\t3) 3 Arguments - [Plain], [key], and [output file]." << endl;
}
// If we recieve no arguments
else
{
string plain, key, output;
cout << "File: ";
cin >> output;
cin.ignore(INT_MAX, '\n');
ifstream file(output.c_str(), ios::binary);
if(file.is_open())
{
cout << "Key: ";
cin >> key;
cin.ignore(INT_MAX, '\n');
// Loading file contents to memory.
unsigned int size = file.tellg();
char * mem = new char [size];
file.seekg (0, ios::beg);
file.read(mem, size);
file.close();
// Writing and cleaning up.
write(cipher(mem, key), output.c_str());
delete[] mem;
}
else
{
cout << "Plain: ";
cin >> plain;
cin.ignore(INT_MAX, '\n');
cout << "Key: ";
cin >> key;
if(write(cipher(plain, key), output))
{
cout << "FAILED!";
}
}
}
return 0;
}
cipher() works great. However, when I write into a file it start losing data and it doesn't come back as the plaintext. Any idea what I am doing wrong?