Advertisement
Pietu1998

PietuCipher 1.0

Feb 9th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. # PietuCipher1
  2. # version 1.0
  3.  
  4. def _keyval(key):
  5.     total = 1
  6.     for pos1 in range(len(key)):
  7.         for pos2 in range(len(key)):
  8.             total = ((total + key[pos1]) ** key[pos2]) % 0xffffffff
  9.     return total
  10.  
  11. def _tables(cols, rows):
  12.     table = []
  13.     revtable = []
  14.     for row in rows:
  15.         items = [(pos, (row + col) % 256) for pos, col in enumerate(cols)]
  16.         poss = [0] * len(cols)
  17.         vals = [0] * len(cols)
  18.         for pos, val in items:
  19.             poss[val] = pos
  20.             vals[pos] = val
  21.         table += [vals]
  22.         revtable += [poss]
  23.     return table, revtable
  24.  
  25. def _gentables(key):
  26.     import random
  27.     val = _keyval(key)
  28.     rand = random.Random(val)
  29.     cols, rows = list(range(256)), list(range(256))
  30.     rand.shuffle(cols)
  31.     rand.shuffle(rows)
  32.     result = _tables(cols, rows)
  33.     return result
  34.  
  35. def encrypt(data, key):
  36.     table, revtable = _gentables(key)
  37.     prev = 0
  38.     out = []
  39.     for x in data:
  40.         y = table[prev][x]
  41.         out += [y]
  42.         prev = y
  43.     return bytes(out)
  44.  
  45. def decrypt(data, key):
  46.     table, revtable = _gentables(key)
  47.     prev = 0
  48.     out = []
  49.     for x in data:
  50.         out += [revtable[prev][x]]
  51.         prev = x
  52.     return bytes(out)
  53.  
  54. if __name__ == "__main__":
  55.     mode = ""
  56.     while mode not in ["E", "D"]: mode = input("[E]ncrypt or [D]ecrypt? ").upper()
  57.     key = bytes(input("Key: "), "utf-8")
  58.     data = input("Input file: ")
  59.     out = input("Output file: ")
  60.     infile = open(data, "rb")
  61.     indata = infile.read()
  62.     infile.close()
  63.     outdata = [encrypt, decrypt][mode == "D"](indata, key)
  64.     outfile = open(out, "wb")
  65.     outfile.write(outdata)
  66.     outfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement