One Time Pad
Receives a message and a key, both strings, and returns the corresponding ciphertext
# Implementation of the popular stream cipher, the One-Time Pad. # It is considered to be the most secure encryption method when used # correctly. def OneTimePad(strMsg, strKey): arrCipher =  try: # Verifies if the message and the key have the same length if len(strMsg) != len(strKey): raise # XORs every single character of the message with every # single character of the key to obtain the ciphertext for x in range(len(strMsg)): arrCipher.append(chr(ord(strMsg[x]) ^ ord(strKey[x]))) # Returns None when the message and the key have different lengths except: print("Message and key have different sizes") return None return ''.join(arrCipher)
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Download this code in plain text format here