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).
Encryption Processing:
- 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;
Python3
def repeated_key_xor(plain_text, key): # returns plain text by repeatedly xoring it with key pt = plain_text len_key = len (key) encoded = [] for i in range ( 0 , len (pt)): encoded.append(pt[i] ^ key[i % len_key]) return bytes(encoded) # Driver Code def main(): plain_text = b 'Burning \'em, if you ain\'t quick and nimble\nI go crazy when I hear a cymbal' key = b 'ICE' print ( "Plain text: " , plain_text) print ( "Encrypted as: " , repeated_key_xor(plain_text, key). hex ()) if __name__ = = '__main__' : main() |
Output:
Plain text: b”Burning ’em, if you ain’t quick and nimble\nI go crazy when I hear a cymbal”
Encrypted as: 0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f
Complexity Analysis: The time complexity of the repeated_key_xor function is O(n), where n is the length of the plain text. The space complexity of the function is also O(n), as it creates a list of encoded bytes that is the same size as the plain text.