Computer Architectures have predefined ASCII values & Binary forms for all printable characters, which allows us to operate bit-wise logic like XOR and most encryption/decryption algorithms depend on. The Key is XOR-operated on the plain text to produce the encrypted text.
Only Parameters required to encrypt a plain text using this technique:
- Plain text (text which has to be encrypted).
- Key (unique byte of text which is used to encrypt, can be of any length).
- Finding the lengths of “Plain text” and “Key”.
- Breaking the plain text into pieces of length equivalent to the length of Key.
- XOR-ring the pieces of Plain text with the Key in respective order individually.
- Store the above XOR operated outcome in an array.
- When the looping is done, the array contains the whole encrypted text.
Note: Sometimes you may have to pad the plain text in case it is not welly aligned with the general block size. Here is how to do it https://www.geeksforgeeks.org/retaining-the-padded-bytes-of-structural-padding-in-python/
Below is the Code to encrypt Repeated-key XOR;
Plain text: b”Burning ’em, if you ain’t quick and nimble\nI go crazy when I hear a cymbal”
Encrypted as: 0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.