Skip to content

Instantly share code, notes, and snippets.

@divegeek
Created August 18, 2015 20:31
Show Gist options
  • Save divegeek/7e4e3fe9853ab3a6fee3 to your computer and use it in GitHub Desktop.
Save divegeek/7e4e3fe9853ab3a6fee3 to your computer and use it in GitHub Desktop.
Trivial one-time pad implementation
#!/usr/bin/python
import sys
if __name__ == "__main__":
if len(sys.argv) != 3:
print "Usage: otp.py <plaintext> <pad>"
exit(1)
plaintext = sys.argv[1].upper()
pad = sys.argv[2].upper()
if len(plaintext) != len(pad):
print "Pad and plaintext must be the same length"
exit(2)
if not plaintext.isalpha() or not pad.isalpha():
print "Pad and plaintext must be alphabetic"
exit(3)
ciphertext = ''
for i in range(0, len(plaintext)):
ciphertext += chr((ord(plaintext[i]) + ord(pad[i])) % 26 + ord('A'))
print ciphertext
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment