Alexander
Alexander

Reputation: 355

XOR encryption/decryption when the key is more than one byte long?

Suppose that the character 'b' is used as a key for XOR encryption. In that case, encrypting a plain text is done by XOR-ing each byte (character) of the text by the ascii code of 'b'. Conversely, the plain text can be obtained from the ciphered text by XOR-ing by 'b's ascii code again. This is understood.

However, how exactly does one encrypt when the key (password) is a string of characters? Suppose that the encrypting password is 'adg'. In that case, is the plain text ciphered via XOR-ing each of its bytes with the value of a XOR d XOR g? If not, then how?

Upvotes: 2

Views: 5444

Answers (2)

Ilmari Karonen
Ilmari Karonen

Reputation: 50338

There are many ways to implement "XOR encryption", so if you're trying to decode some existing data, you'll first need to figure out which kind it's encrypted with.

The most common scheme I've seen works basically like the classic Vigenère cipher; e.g. for the three-byte key abc, the first byte of plaintext is XORed with a, the second with b, the third with c; the fourth byte is then again XORed with a, the fifth with b, and so on, like this:

Plaintext: THIS IS SOME SECRET TEXT...
Key:       abcabcabcabcabcabcabcabcabc
--------------------------------------
XOR:       5**2B*2B0./&A1&"0&5B7$:7OLM

One way to recognize this kind of repeating-key cipher (and also find out the key length) is to compute the index of coincidence between pairs of bytes N positions apart in the ciphertext. If the key length is L, then plotting the index of coincidence as a function of N should reveal a regular array of peaks at the values of N that are divisible by L. (Of course, this only works if the plaintext is something like normal text or code that has a biased byte frequency distribution; if it's completely random data, then this won't help.)

Or you could just use hellman's xortool, which will automate all this for you. For example, running it on the ciphertext 5**2B*2B0./&A1&"0&5B7$:7OLM above, it says:

The most probable key lengths:
   1:   17.3%
   3:   40.7%
   6:   21.5%
   8:   6.5%
  12:   5.4%
  15:   4.6%
  18:   4.0%
Key-length can be 3*n

If you have enough ciphertext, and can guess the most common byte in the plaintext, it will even spit out the key for you.

Upvotes: 5

kerem
kerem

Reputation: 2710

A way is to repeat the key to cover plain text.

e.g. key = RTTI, plaintext = "how exactly does one"

Text: how exactly does one
Key:  RTTIRTTIRTTIRTTIRTTI

Each character in the plain text will be XOR'd with the corresponding key character below it.

Upvotes: 5

Related Questions