Skip to content

Instantly share code, notes, and snippets.

@usernaamee
Created November 14, 2018 07:48
Show Gist options
  • Save usernaamee/5203c2040c99ada9561c8355ed431aa0 to your computer and use it in GitHub Desktop.
Save usernaamee/5203c2040c99ada9561c8355ed431aa0 to your computer and use it in GitHub Desktop.
One time pad - pure python implementation
import os
import sys
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('infile')
parser.add_argument('padfile')
parser.add_argument('outfile')
chunksize = 8192
args = parser.parse_args()
infile = args.infile
padfile = args.padfile
outfile = args.outfile
if os.path.getsize(infile) > os.path.getsize(padfile):
print('Error: padding is smaller than input file')
sys.exit(1)
with open(infile, 'rb') as inf, open(padfile, 'rb') as pf, open(outfile, 'wb') as of:
while True:
inchunk = inf.read(chunksize)
padchunk = pf.read(chunksize)
if inchunk:
outchunk = ''.join([chr(ord(i) ^ ord(p)) for i, p in zip(inchunk, padchunk)])
of.write(outchunk)
else:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment