This code comes from: http://code.activestate.com/recipes/577090-file-encryption-using-stream-cipher/
import sys
import random
if len(sys.argv) != 4:
print "Usage: encdec_.py longintkey [path]filename1 [path]filename2"
sys.exit()
random.seed(long(sys.argv[1])) # key
f1 = open( sys.argv[2], "rb")
bytearr = map (ord, f1.read () )
f2 = open( sys.argv[3], "wb" )
for i in range(len(bytearr)):
f2.write(chr(bytearr[i] ^ random.randint(0, 255)))
f1.close()
f2.close()
Can this code be parallelized using multiprocessing?
Normally you would break the file into sections and send each section to a separate process for multiprocessing, but that won’t work in this case because you need to generate the random numbers sequentially from the same seed.
If you were willing to change the encryption algorithm there are a number of alternative ways you could set it up to be able to handle multiple chunks. But a “stream cipher” specifically doesn’t lend itself to that sort of thing easily.
You could generate the random numbers first and then feed those generated numbers to multiple processes along with their position, but that is more likely to slow you down than to speed you up.
So I would say “no”, you can’t make it multiprocessing, at least not in a practical way.
(I’m assuming this is an experiment or a learning exercise and you’re not trying to use this for serious encryption. If you trying to use it for something serious, there are many full-featured encryption library functions to choose from.)