someone explain this x-or encryption

This is a discussion on someone explain this x-or encryption within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <iomanip> #include <string.h> using namespace::std; void encrypt(char string[], char key[]); void decrypt(char string[], char key[]); int ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    27

    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;
    }

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    http://www.codeproject.com/cpp/bitba...asp?print=true
    Go there to learn how the operator works, but basically..
    Code:
    encrypt: a ^ b = c   // a is the original and c is the result
    decrypt: c ^ b = a   // perform the same operation with result you get the original
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Does this method really works? After the ^ you lost all the information about the original string.
    Nothing more to tell about me...
    Happy day =)

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    27
    when I run it

    the first line is the string I entered
    the second line is a bunch of goop
    the third line is the string I entered again

  5. #5
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Originally posted by alphaoide
    http://www.codeproject.com/cpp/bitba...asp?print=true
    Go there to learn how the operator works, but basically..
    Code:
    encrypt: a ^ b = c   // a is the original and c is the result
    decrypt: c ^ b = a   // perform the same operation with result you get the original
    Code:
    replace a and b with 0 or 1 and get the c based on this table
    
    1   ^   1   ==   0
    1   ^   0   ==   1
    0   ^   1   ==   1
    0   ^   0   ==   0
    
    then do decrypting operation and you'll get the original a
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    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.)
    Last edited by DougDbug; 01-14-2004 at 08:05 PM.

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by student2005
    when I run it

    the first line is the string I entered
    the second line is a bunch of goop
    the third line is the string I entered again
    That's a cipher, buddy.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by gustavosserra
    Does this method really works? After the ^ you lost all the information about the original string.
    No, you lose none of the information about it, assuming you know what the key is.

    For example, look at this snippet from alphaoide's post:

    1 ^ 1 == 0
    1 ^ 0 == 1
    0 ^ 1 == 1
    0 ^ 0 == 0
    If you only know the key and the final value of a bit, then you have this:

    ? ^ 1 == 0
    ? ^ 0 == 1
    ? ^ 1 == 1
    ? ^ 0 == 0
    And if you look at this, in each of the 4 possible cases, you can reconstruct the original value of the bit.

    It's a reversible mathematical operation, and as long as you know the result and ONE of the operands, you know the other.

    A mathematical example would be to assume I was going to "encrypt" a vector of numbers by adding 42 to each one. If I told you the "encrypted" vector was [183, 25, 47.2], surely you could tell me what the original was? You lose no information about the numbers.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    It all stems from

    K ^ K = 0

    C ^ 0 = C

    Since encryption followed by decryption is
    ( C ^ K ) ^ K

    Exoring anything with the same value twice gets you back to where you started.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    16
    Im a student of programming too.

    Can someone explain this in plain english. I've never seen this before. I've typed in it...how does it encrypt to things like *&^%$#.
    Thanks for anyones help..

  11. #11
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    digy,

    Like I said above, first you have to understand ASCII, Character Array Strings, Binary Numbers, and Bitwise Operations. Explaining this stuff "in plain english" won't help unless you have the background. Once you have the background, the above posts will be easy to understand.

    Programming is a difficult subject. You have to learn step-by-step... You can't learn it all at once!

    So, you'll have to research and study-up on those topics, or wait till you run-across them in your normal programming-studies.
    how does it encrypt to things like *&^%$#.
    Everything in the computer is stored as a binary number. So, you can XOR encrypt anything. I doesn't even have to be a character... you can encrypt an mp3 file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. help needed with edit control & encryption
    By willc0de4food in forum Windows Programming
    Replies: 2
    Last Post: 03-16-2006, 07:21 PM
  3. abt encryption algorithm
    By purIn in forum C Programming
    Replies: 9
    Last Post: 12-22-2003, 09:16 PM
  4. What's wrong with my Stream Cipher Encryption?
    By Davros in forum C++ Programming
    Replies: 3
    Last Post: 04-18-2002, 09:51 PM
  5. File Encryption & Read/Write in Binary Mode
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 05:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21