ritch
ritch

Reputation: 1808

Need XOR Encryption Algorithm Pseudocode

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

Upvotes: 0

Views: 4082

Answers (4)

Jerry Coffin
Jerry Coffin

Reputation: 490623

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.

Upvotes: 4

Pascal Cuoq
Pascal Cuoq

Reputation: 80335

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

Upvotes: 2

Michael Dorgan
Michael Dorgan

Reputation: 12515

Do you mean something like?


unsigned char key = 0x7F;  // or any 8-bit value.
//encrypt
for(int i=0; i < strlen(input); i++) { input[i] ^= key; }
//decrypt
for(int i=0; i < strlen(input); i++) { input[i] ^= key; }

Upvotes: 2

Amber
Amber

Reputation: 527328

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.

Upvotes: 2

Related Questions