0

Based on Wikipedia, One-Time Pad is an encryption technique that cannot be cracked. How to implement One-Time Pad encryption in Python?

Farshid Ashouri
  • 16,143
  • 7
  • 52
  • 66
  • That's not completely true. It cannot be cracked if used correctly. If a one-time pad is used over and over then it can be broken using frequency analysis. – ramzeek Nov 21 '21 at 01:33
  • The name suggests it should be used ONE TIME, so this is the base assumption. – Farshid Ashouri Nov 21 '21 at 05:33

1 Answers1

0

Here is an example implementation

def encrypt(pad, secret):
    return ''.join([chr(ord(c) ^ ord(secret[i])) for i, c in enumerate(pad[:len(secret)])])

Now you can use it to encrypt your messages. Remember the pad size should be at least the size of the secret:

secret = 'i am a secret'
pad = '33116f14-9b6f-42c6-9636-3dbd31c0548d'
enc = encrypt(pad, secret)

And you can decipher it with the same function:

assert secret == encrypt(pad, enc)
Farshid Ashouri
  • 16,143
  • 7
  • 52
  • 66