someone explain this x-or encryption
Code:
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace::std;
void encrypt(char string[], char key[]);
void decrypt(char string[], char key[]);
int main()
{
int again;
char string[81];
char key[]="123456789ZBCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJK123456789";
do
{
cout << "Enter a word or sentence up to 80 characters: " << endl;
gets(string);
encrypt(string, key);
decrypt(string, key);
cout << "Want to enter another? (1 for yes, 2 for no)" << endl;
cin >> again;
}
while (again == 1);
return 0;
}
void encrypt(char string[], char key[])
{
for(int x = 0; x < strlen(string); x++)
{
string[x] = string[x]^key[x];
cout << string[x];
}
cout<<endl;
}
void decrypt(char string[], char key[])
{
for(int x=0; x < strlen(string); x++)
{
string[x] = string[x]^key[x];
cout << string[x];
}
cout<<endl;
}
I'm not sure what part(s) of the program you are having trouble with....
Today, I'm making alot of assumptions, so I'll assume it's the bitwise-binary stuff. :)
First, you have to understand binary numbers. Everything inside the computer is stored in binary (ones and zeros). Getting a C++ program to display in binary is a little tricky... not really that hard, but not as trivial as displaying in decimal, octal, or hexadecimal.
The built-in Windows calculator will do decimal-binary conversion:
Start/Programs/Accessories/Calculator/View/Scientific/Bin
You can look-up the ASCII value for the character in question. The first character in your key is a space which is stored as 32 decimal, or 0010 0000 binary (leading zeros and space added for clarity).
Say the first character in your string is 'A'. This converts to 0100 0001.
Next, you have to understand bitwise operators. There is more info in the Programming FAQ
You bitwise-exclusive-or the first character in the key-string with the first character of your string to encrypt it:
0010 0000 ASCII space (key for first character)
0100 0001 ASCII 'A' (The first character in your string)
------------- XOR
0110 0001 ASCII '1' = 49 decimal (An ecncrypted A)
Now to decrypt:
0010 0000 ASCII space (key for first character)
0110 0001 ASCII '1' (An ecncrypted A)
------------- XOR
0100 0001 ASCII 'A' (The decrypted A) Ta-Da!!!
(I hope I didn't screw that up...)
A couple of comments:
I would expect a beginner's version of this program to use a single-character key... every character in your string XORed with the same key-character.
If you don't have a lot of experience with binary numbers, and you are not a "math whiz", don't expect to understand this stuff right-away. It takes some study, thinking, and practice to get comfortable with it. And, you can do alot of programming without binary numbers or bitwise operations. I'd say it's an intermediate topic. If you are a beginner, don't get "bogged-down" with this stuff... move-on to the next topic and come back to it later.
Nobody uses actual binary. (Well, almost nobody.) We use hexadecimal because,
1- You can't put binary numbers directly in your C++ source code.
2- Binary numbers get difficult to read. More than 8-bits, and you have to start counting the digits.
3- With a little practice, you can convert between binary and hexadecimal in your head. (binary-dicimal is not so easy.)