Created
November 14, 2018 07:48
-
-
Save usernaamee/5203c2040c99ada9561c8355ed431aa0 to your computer and use it in GitHub Desktop.
One time pad - pure python implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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