Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random, os, re
- class Pad(object):
- def __init__(self):
- super(Pad, self).__init__()
- def getRandomCypher(self, data):
- """Generates random key and returns dict with key and cyphered data."""
- key = os.urandom(len(data))
- pairs = [[ord(a) for a in i] for i in zip(data, key)]
- retval = [chr(i[0]^i[1]) for i in pairs]
- return {"KEY":key, "CRYPT":str().join(retval)}
- def decypher(self, dict):
- """Decyphers with a dict. Takes dict with keys 'KEY' and 'CRYPT'."""
- if list(sorted(dict.keys())) != ["CRYPT","KEY"]:
- raise SyntaxError, "Invalid argument dict"
- pairs = [[ord(a) for a in i] for i in zip(dict["KEY"], dict["CRYPT"])]
- retval = [chr(i[0]^i[1]) for i in pairs]
- return str().join(retval)
- def cypherFile(self, path):
- kp = self.getRandomCypher(open(path, "rb").read())
- with open("outfile.bin", "wb") as of:
- of.write(kp["CRYPT"])
- with open("cryptkey.KEY", "wb") as ck:
- ckdat = """START?{d}?END\nFILE_NAME:'{fn}'"""\
- .format(d=kp["KEY"].encode("base64"), fn=os.path.split(path)[1])
- ck.write(ckdat)
- def decypherFile(self, keyFile, cypherFile):
- keyDat = open(keyFile, "rb").read()
- cypherDat = open(cypherFile, "rb").read()
- keyRead = re.findall("START\?(.+)\?END", keyDat, re.DOTALL)[0]
- FName = re.findall("FILE_NAME:'(.+)'", keyDat, re.DOTALL)[0]
- Decyphered = self.decypher({"KEY":keyRead.decode("base64"), "CRYPT":cypherDat})
- with open(FName, "wb") as file:
- file.write(Decyphered)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement