views:

242

answers:

4

I am trying to find the pseudocode for the XOR encryption algorithm. However I've had no luck so far. Anybody know where I can find it?

EDIT: XOR 32 if that helps

EDIT 2: For Passwords

+2  A: 

The most basic "xor encryption algorithm" is probably one that just XOR's the plaintext with the key, like so:

for each bit of the plaintext:
    ciphertext = bit of plaintext XOR bit of key

where the key just wraps around when it reaches the end.

Since XOR is its own inverse, XORing the ciphertext with the key again in the same fashion will reveal the plaintext.

Amber
Yes, though perhaps make it clear that bit x of the input is XORed with bit (x%k) of the key (if it has length k). And it works just as well if you do it per byte rather than bit.
redtuna
+2  A: 
Michael Dorgan
+2  A: 

For C:

void crypt(char key, char *msg, size_t l)
{
  int i;
  for(i=0; i<l; i++)
  msg[i]^=key;
}

void decrypt(char key, char *msg, size_t l)
{
  crypt(key, msg, l);
}
Pascal Cuoq
+3  A: 

Assuming you mean a Vernam cipher, it's just:

for i = 0 to length of input
    output[i] = input[i] xor key[i mod key_length]

Note that this is quite weak unless the key-stream is at least as long as the input, and is never re-used.

Jerry Coffin
Nice for the 32-bit version. I was puzzling over what to do for messages with an uneven length.
Pascal Cuoq
... in which case it is called "one-time pad" and is the strongest symmetric encryption we have, assuming you know where to get good keys :)
Pascal Cuoq
@Pascal:yes, it's rather an oddity: if you do things just right, it's the strongest cipher around (at least in some ways). If you slip up and do the *least* thing wrong, it degenerates directly from extremely secure to *completely* broken.
Jerry Coffin
'quite weak' is a bit of an understatement.
Nick Johnson