I've been posting a lot of cryptokey generation stuff for one time pads lately.
For example:
https://trogramming.com/blog/2022/03/24/...generator/
& https://hackforums.net/showthread.php?tid=6200705
So it only made sense to actually make a one-time pad generator.
While I'm confident enough in my python skills to keep the memory from bloating with larger files,
I still thought I'd have to port my algorithm to C++ to get anything meaningfully fast.
Luckily I was wrong, on my slow AF computer I can do about a 2 megabytes per second.
I imagine most people will get much faster results.
As it's an XOR encryption you can decrypt the ciphertext back into plaintext with the exact same key.
For security reasons it's important you never reuse the same key thus the name one-time pad.
XOR_Crypt is a standalone class, with no dependencies aside from the byte_xor function which could possibly be sped up.
Included at the bottom is a demo of it's use, be sure to replace the filenames with your own files.
Make duplicate USBs/SD cards with an offline computer and share the keys with people in person, or if long distance/international key exchange is required consider concealing a QR code.
Personally I use a deck of numbered barcodes that can be shuffled & resequenced then hashed into seeds for generating one time pads.
For example:
https://trogramming.com/blog/2022/03/24/...generator/
& https://hackforums.net/showthread.php?tid=6200705
So it only made sense to actually make a one-time pad generator.
While I'm confident enough in my python skills to keep the memory from bloating with larger files,
I still thought I'd have to port my algorithm to C++ to get anything meaningfully fast.
Luckily I was wrong, on my slow AF computer I can do about a 2 megabytes per second.
I imagine most people will get much faster results.
As it's an XOR encryption you can decrypt the ciphertext back into plaintext with the exact same key.
For security reasons it's important you never reuse the same key thus the name one-time pad.
XOR_Crypt is a standalone class, with no dependencies aside from the byte_xor function which could possibly be sped up.
Included at the bottom is a demo of it's use, be sure to replace the filenames with your own files.
Code
from time import time
def byte_xor(ba1, ba2):
return bytes([_a ^ _b for _a, _b in zip(ba1, ba2)])
class XOR_Crypt:
def __init__(self,keyfile):
self.keyfile=keyfile
def encrypt(self,bytestream):
CHUNKSIZE=1024
out=b""
file=open(self.keyfile,'rb')
while 1:
if len(bytestream)<CHUNKSIZE:
#last chunk
chunk=file.read(len(bytestream))
out+=byte_xor(chunk,bytestream)
return out
else:
chunk=file.read(CHUNKSIZE)
out+=byte_xor(chunk,bytestream)
def encryptfile(self,filename,outputname=None):
if outputname==None:
outputname=filename+"_encrypted"
##Key File must be larger than the Input File
CHUNKSIZE=1024
file=open(self.keyfile,'rb')
infile=open(filename,'rb')
outfile=open(outputname,'wb')
while 1:
#load file 1024 bytes at a time.
bytestream=infile.read(CHUNKSIZE)
if len(bytestream)<CHUNKSIZE:
#last chunk
chunk=file.read(len(bytestream))
outfile.write(byte_xor(chunk,bytestream))
outfile.flush()
break
else:
chunk=file.read(CHUNKSIZE)
outfile.write(byte_xor(chunk,bytestream))
outfile.flush()
#Initialise the crypter class with your key. It must be larger than what you're encrypting.
a=XOR_Crypt("C:/Users/User/Documents/Hackforums/Keys/K001.key")
#record the current time
t=time()
#encrypt a file with the initialised crypter class
a.encryptfile("C:/Users/User/Documents/Hackforums/banner.png","outcrypt")
#use the same crypter to decrypt the encrypted file, it will return your original
a.encryptfile("outcrypt","decrypted.png")
#record the time upon completion
t2=time()
#print the total ellapsed time.
print("Took %s seconds" %round(t2-t,4))Personally I use a deck of numbered barcodes that can be shuffled & resequenced then hashed into seeds for generating one time pads.


![[Image: zzzscreen.gif]](https://trogramming.com/blog/wp-content/uploads/2022/03/zzzscreen.gif)