Facebook
From Buff Owl, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 118
  1.  
  2. from sys import argv
  3.  
  4. key1 = eval(argv[1])  # list of function selectors aka the key1
  5. key2 = eval(argv[2])  # list of function selectors aka the key2
  6. r = len(key1)  # nmbr rounds implied by keys
  7. v = eval(argv[3])  # moving vector
  8. bo = int(argv[4])  # nmbr of bits out
  9. pt = int(argv[5])  # the plaintext
  10.  
  11.  
  12. def round(s, key1, key2, bo):
  13.     s = s ^ key1
  14.     s = key2 * s % 2 ** bo
  15.     return s
  16.  
  17. def encrypt(pt, key1, key2, r, bo):
  18.     ct = pt
  19.     for i in range(r):
  20.         ct = round(ct, key1[i], key2[i], bo)
  21.         ct = bin(ct)
  22.         ct = ct[2:]
  23.         if len(ct) < bo:
  24.             d = 2 ** (bo - len(ct))
  25.             d = bin(d)
  26.             d = d[3:]
  27.             ct = d + ct
  28.         b = ct[v[i]:]
  29.         a = ct[:v[i]]
  30.         ct = b + a
  31.         ct = int(ct, 2)
  32.  
  33.     return ct
  34.  
  35. print encrypt(pt, key1, key2, r, bo)
  36.  
  37.