Advertisement
DeaD_EyE

AES128 with Shamir

Apr 3rd, 2017
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. from Crypto.Cipher import AES
  2. from Crypto.Util import Counter
  3. from Crypto.Protocol.SecretSharing import Shamir
  4.  
  5. # https://legrandin.github.io/pycryptodome/Doc/3.4/Crypto.Protocol.SecretSharing.Shamir-class.html
  6. # https://legrandin.github.io/pycryptodome/Doc/3.4/Crypto.Cipher.AES-module.html
  7. # http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.80.8910&rep=rep1&type=pdf
  8. # https://pypi.python.org/pypi/secretsharing/ # Data-Len is not fixed to 16 byte
  9. # https://pypi.python.org/pypi/subrosa/0.1.0 # better
  10. # http://point-at-infinity.org/ssss/index.html
  11.  
  12. def make_keys(data=b'', k=2, n=3):
  13.     key = os.urandom(16)
  14.     counter = Counter.new(128)
  15.     aes = AES.new(key, mode=AES.MODE_CTR, counter=counter)
  16.     cipher = aes.encrypt(data)
  17.     keys = Shamir.split(k, n, key)
  18.     keys = [f'{n}-{base64.b64encode(k).decode()}' for n, k in keys]
  19.     return keys, cipher
  20.  
  21. def decode(keys, data):
  22.     keys = [k.split('-') for k in keys]
  23.     keys = [(int(n), base64.b64decode(k)) for n, k in keys]
  24.     key = Shamir.combine(keys)
  25.     aes = AES.new(key, mode=AES.MODE_CTR, counter=Counter.new(128))
  26.     cleartext = aes.decrypt(data).decode()
  27.     return cleartext
  28.  
  29. keys, encrypted = make_keys(b'Hello World', 5, 20)
  30. cleartext = decode(keys[5:11], encrypted)
  31. print(cleartext)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement