THIS POST NO LONGER APPLIES. READ THIS ONE
Okay I've got a program that encrypts text with a key:
Code:
#include <iostream>
#include <fstream>
using namespace std;
void XOREncrypt(char* key, char* plaintext);
int main()
{
char plaintext[50];
char key[50];
for(;;)
{
cout<<"Plaintext file name: ";
cin >> plaintext;
cout<<"Encryption Key: ";
cin >> key;
XOREncrypt(key, plaintext);
}
return 1;
}
void XOREncrypt(char* key, char* plaintext)
{
fstream plainfile(plaintext, ios::in | ios::out | ios::binary);
if(!plainfile)
{
cout <<"Error opening plaintext.\n";
return;
}
fstream cipherfile("ciphertext.txt", ios::in | ios::out | ios::binary | ios::trunc);
if (!cipherfile)
{
cout << "Error opening chiphertext.txt.\n";
return;
}
int i = 0;
char ch;
while(plainfile)
{
plainfile.get(ch);
if(plainfile)
{
cipherfile.put(ch ^ key[i]);
i++;
if(!key[i]) i = 0;
}
}
cout<< "XOR Encryption finished. Check ciphertext.txt" << endl;
return;
}
Just a simple XOR routine.
The key is: "key"
So now I have the plain text, and the cipher text:
![Quote](mpghv2/misc/quote_icon.png)
Originally Posted by
plaintext
Hello my name is Shaun.
![Quote](mpghv2/misc/quote_icon.png)
Originally Posted by
ciphertext
#
YYEE*K
Ok. So now it would make sense to me that XORing the plaintext with the ciphertext would reveal the key.
for instance:
0000 1111 - plain
^1111 0000 - cipher
1111 1111 - key
See what I mean? So I made this program to do just that:
Code:
#include <iostream>
#include <fstream>
using namespace std;
void XOREncrypt(char* ciphertext, char* plaintext);
int main()
{
char plaintext[50];
char ciphertext[50];
for(;;)
{
cout<<"Plaintext file name: ";
cin >> plaintext;
cout<<"Ciphertext filename: ";
cin >> ciphertext;
XOREncrypt(ciphertext, plaintext);
}
return 1;
}
void XOREncrypt(char* ciphertext, char* plaintext)
{
fstream plainfile(plaintext, ios::in | ios::out | ios::binary);
if(!plainfile)
{
cout <<"Error opening plaintext.\n";
return;
}
fstream cipherfile(ciphertext, ios::in | ios::out | ios::binary);
if (!cipherfile)
{
cout << "Error opening chiphertext.\n";
return;
}
fstream keyfile("key.txt", ios::in | ios::out | ios::binary | ios::trunc);
if(!keyfile)
{
cout << "Error opening key.txt\n";
return;
}
int i = 0;
char ch1, ch2;
while(plainfile && cipherfile)
{
plainfile.get(ch1);
cipherfile.get(ch2);
if(plainfile && cipherfile)
{
keyfile.put(ch1 ^ ch2);
}
}
cout<< "XOR Encryption finished. Check ciphertext.txt" << endl;
return;
}
Simple as pie...
Except instead of getting the key. I get this mess!:
![Quote](mpghv2/misc/quote_icon.png)
Originally Posted by
mess
·›OlO xy'nkm< os<S1apn*
might I ask? Wtf?! my head is smitten by new mistress and perplexity is her name. ![Confused](images/emotions/emo8.png)
Hmmm.. I should have checked the encryptor after I edited it. It seems like every little thing I do to it messes up the code immensely.
The closest thing I have got to fixing it is this:
HEllo, my namE is Shaun..)
and it keeps throwing in random capitalization and shit... I don't get it o_O... Is it so much to ask for something to just work?
EDIT:
Using this scheme...
Code:
#include <iostream>
#include <fstream>
using namespace std;
void XOREncrypt(char* key, char* plaintext);
int main()
{
char* plaintext = new char[50];
char* key = new char[50];
for(;;)
{
plaintext = new char[50];
key = new char[50];
cout<<"Plaintext file name: ";
cin >> plaintext;
cout<<"Encryption Key: ";
cin >> key;
XOREncrypt(key, plaintext);
}
return 1;
}
void XOREncrypt(char* key, char* plaintext)
{
fstream plainfile(plaintext, ios::in | ios::out | ios::binary);
if(!plainfile)
{
cout <<"Error opening plaintext.\n";
return;
}
fstream cipherfile("ciphertext.txt", ios::in | ios::out | ios::binary | ios::trunc);
if (!cipherfile)
{
cout << "Error opening chiphertext.txt.\n";
return;
}
int i = 0;
char ch;
while(plainfile)
{
plainfile.get(ch);
if(!plainfile)break;
if(ch)
{
cipherfile.put(ch ^ key[i]);
i++;
if(!key[i]) i = 0;
}
}
cout<< "XOR Encryption finished. Check ciphertext.txt" << endl;
return;
}
I almost fixed it....
but the E is still capitalized.... ![Frown](images/emotions/emo4.png)
Why is this so fkin complicated D: ArGGGHH! It's supposed to be easy!