Search code examples
encryptionxor

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?


Solution

  • 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.