Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # PietuCipher1
- # version 1.0
- def _keyval(key):
- total = 1
- for pos1 in range(len(key)):
- for pos2 in range(len(key)):
- total = ((total + key[pos1]) ** key[pos2]) % 0xffffffff
- return total
- def _tables(cols, rows):
- table = []
- revtable = []
- for row in rows:
- items = [(pos, (row + col) % 256) for pos, col in enumerate(cols)]
- poss = [0] * len(cols)
- vals = [0] * len(cols)
- for pos, val in items:
- poss[val] = pos
- vals[pos] = val
- table += [vals]
- revtable += [poss]
- return table, revtable
- def _gentables(key):
- import random
- val = _keyval(key)
- rand = random.Random(val)
- cols, rows = list(range(256)), list(range(256))
- rand.shuffle(cols)
- rand.shuffle(rows)
- result = _tables(cols, rows)
- return result
- def encrypt(data, key):
- table, revtable = _gentables(key)
- prev = 0
- out = []
- for x in data:
- y = table[prev][x]
- out += [y]
- prev = y
- return bytes(out)
- def decrypt(data, key):
- table, revtable = _gentables(key)
- prev = 0
- out = []
- for x in data:
- out += [revtable[prev][x]]
- prev = x
- return bytes(out)
- if __name__ == "__main__":
- mode = ""
- while mode not in ["E", "D"]: mode = input("[E]ncrypt or [D]ecrypt? ").upper()
- key = bytes(input("Key: "), "utf-8")
- data = input("Input file: ")
- out = input("Output file: ")
- infile = open(data, "rb")
- indata = infile.read()
- infile.close()
- outdata = [encrypt, decrypt][mode == "D"](indata, key)
- outfile = open(out, "wb")
- outfile.write(outdata)
- outfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement