Its been 2 hours. Just wait
THIS POST NO LONGER APPLIES. READ THIS ONE
Okay I've got a program that encrypts text with a key:
Just a simple XOR routine.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; }
The key is: "key"
So now I have the plain text, and the cipher text:
Originally Posted by plaintextOk. So now it would make sense to me that XORing the plaintext with the ciphertext would reveal the key.Originally Posted by ciphertext
for instance:
0000 1111 - plain
^1111 0000 - cipher
1111 1111 - key
See what I mean? So I made this program to do just that:
Simple as pie...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; }
Except instead of getting the key. I get this mess!:
might I ask? Wtf?! my head is smitten by new mistress and perplexity is her name.Originally Posted by mess
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:
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?HEllo, my namE is Shaun..)
EDIT:
Using this scheme...
I almost fixed it....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; }
but the E is still capitalized....
Why is this so fkin complicated D: ArGGGHH! It's supposed to be easy!HEllo my name is Shaun.
Last edited by why06; 12-11-2009 at 11:15 PM.
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
Its been 2 hours. Just wait
What compiler are you using why?
My MSVC++ 2003 complains about 'while(plainfile)'
Ah we-a blaze the fyah, make it bun dem!
Yeh well Im making it too complicated. Here this one i simple enough that anyone can analyze it quickly:
Code:#include <iostream> using namespace std; int main() { char p,c,k; p = 'p';//plain c = 'c';//cipher k = 'k';//key cout<<p<<endl; //prints plain character to screen c = p^k; cout<<c<<endl; //prints cipher character to screen k = c^p; cout<<k<<endl; //prints key to screen //Time for strings... these are more error prone// char* pp = new char[50]; char* cp = new char[50]; char* kp = new char[50]; strcpy(pp,"plaintext plaintext plaintext"); strcpy(cp,"cipher"); strcpy(kp, "key"); cout<<pp<<endl; //XORs plaintext with key for(int i = 0; pp[i]; i++) { if(!*kp)kp = "key"; cp[i] = pp[i] ^ *(kp++); } cout << cp <<endl; // prints ciphertext to screen //XORs plaintext with cipertext. This should reveal key char* t = new char[50]; for(int i = 0; pp[i]; i++) { t[i] = pp[i] ^ cp[i]; } cout<< t <<endl; //prints keytext to screen system("pause"); return 1; }
See the key is revealed, but there are also some errors tacked on to the end.(SORS=2) Now it seems the longer the text gets the worse the errors. So I need to nip this out. I know I have played out this XOR thing, but it's important that I get it exactly right! If there are errors in my own algorithms, especially one this simple... how can I hope to crack others encryption?Originally Posted by Output
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
u defined key as char and char is ' ' not " ". unless you convert it with sZ.
u need to convert char to string.
https://www.linuxquestions.org/questi...r-in-c-423771/
Last edited by Matrix_NEO006; 12-11-2009 at 10:51 PM.
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
usually char should be like this
if(!*kp)kp = 'key';
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
-sigh, this shit is WAY over mah head.
why06 (12-11-2009)
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower
Uhm mister why, this works fine for me:
Using numbers that works fine, try converting the char they enter to int first see if that works.Code:#include <windows.h> #include <iostream> #include <fstream> using namespace std; void XOREncrypt(int num, int key); int main() { int num=00001101; int key=10100000; while(1) { cout<<"Enter a number: "; cin >> num; if(num==9001) { break; } cout<<"Encryption Key: "; cin >> key; XOREncrypt(num, key); } return 1; } void XOREncrypt(int num, int key) { cout<<"XOR Encryption finished. Here is the result: "<<(num ^ key) << endl; }
Ah we-a blaze the fyah, make it bun dem!
"Every gun that is made, every warship launched, every rocket fired signifies, in the final sense, a theft from those who hunger and are not fed, those who are cold and are not clothed. This world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children. The cost of one modern heavy bomber is this: a modern brick school in more than 30 cities. It is two electric power plants, each serving a town of 60,000 population. It is two fine, fully equipped hospitals. It is some fifty miles of concrete pavement. We pay for a single fighter plane with a half million bushels of wheat. We pay for a single destroyer with new homes that could have housed more than 8,000 people. This is, I repeat, the best way of life to be found on the road the world has been taking. This is not a way of life at all, in any true sense. Under the cloud of threatening war, it is humanity hanging from a cross of iron."- Dwight D. Eisenhower