Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Crypto.Cipher import AES
- from Crypto.Util import Counter
- from Crypto.Protocol.SecretSharing import Shamir
- # https://legrandin.github.io/pycryptodome/Doc/3.4/Crypto.Protocol.SecretSharing.Shamir-class.html
- # https://legrandin.github.io/pycryptodome/Doc/3.4/Crypto.Cipher.AES-module.html
- # http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.80.8910&rep=rep1&type=pdf
- # https://pypi.python.org/pypi/secretsharing/ # Data-Len is not fixed to 16 byte
- # https://pypi.python.org/pypi/subrosa/0.1.0 # better
- # http://point-at-infinity.org/ssss/index.html
- def make_keys(data=b'', k=2, n=3):
- key = os.urandom(16)
- counter = Counter.new(128)
- aes = AES.new(key, mode=AES.MODE_CTR, counter=counter)
- cipher = aes.encrypt(data)
- keys = Shamir.split(k, n, key)
- keys = [f'{n}-{base64.b64encode(k).decode()}' for n, k in keys]
- return keys, cipher
- def decode(keys, data):
- keys = [k.split('-') for k in keys]
- keys = [(int(n), base64.b64decode(k)) for n, k in keys]
- key = Shamir.combine(keys)
- aes = AES.new(key, mode=AES.MODE_CTR, counter=Counter.new(128))
- cleartext = aes.decrypt(data).decode()
- return cleartext
- keys, encrypted = make_keys(b'Hello World', 5, 20)
- cleartext = decode(keys[5:11], encrypted)
- print(cleartext)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement