Utilities/crypto/py3AES.py

changeset 4544
5145cf800bb0
parent 4021
195a471c327b
child 4631
5c1a96925da4
equal deleted inserted replaced
4543:2e6a880670e9 4544:5145cf800bb0
1 #!/usr/bin/python3 1 # -*- coding: utf-8 -*-
2
2 # 3 #
3 # aes.py: implements AES - Advanced Encryption Standard 4 # aes.py: implements AES - Advanced Encryption Standard
4 # from the SlowAES project, http://code.google.com/p/slowaes/ 5 # from the SlowAES project, http://code.google.com/p/slowaes/
5 # 6 #
6 # Copyright (c) 2008 Josh Davis ( http://www.josh-davis.org ), 7 # Copyright (c) 2008 Josh Davis ( http://www.josh-davis.org ),
665 start = j * 16 666 start = j * 16
666 end = j * 16 + 16 667 end = j * 16 + 16
667 if end > len(input): 668 if end > len(input):
668 end = len(input) 669 end = len(input)
669 plaintext = self.__extractBytes(input, start, end, mode) 670 plaintext = self.__extractBytes(input, start, end, mode)
670 # print 'PT@%s:%s' % (j, plaintext)
671 if mode == self.ModeOfOperation["CFB"]: 671 if mode == self.ModeOfOperation["CFB"]:
672 if firstRound: 672 if firstRound:
673 output = self.aes.encrypt(IV, key, size) 673 output = self.aes.encrypt(IV, key, size)
674 firstRound = False 674 firstRound = False
675 else: 675 else:
708 for i in range(16): 708 for i in range(16):
709 if firstRound: 709 if firstRound:
710 iput[i] = plaintext[i] ^ IV[i] 710 iput[i] = plaintext[i] ^ IV[i]
711 else: 711 else:
712 iput[i] = plaintext[i] ^ ciphertext[i] 712 iput[i] = plaintext[i] ^ ciphertext[i]
713 # print 'IP@%s:%s' % (j, iput)
714 firstRound = False 713 firstRound = False
715 ciphertext = self.aes.encrypt(iput, key, size) 714 ciphertext = self.aes.encrypt(iput, key, size)
716 # always 16 bytes because of the padding for CBC 715 # always 16 bytes because of the padding for CBC
717 for k in range(16): 716 for k in range(16):
718 cipherOut.append(ciphertext[k]) 717 cipherOut.append(ciphertext[k])
849 @param mode mode of operations (0, 1 or 2) 848 @param mode mode of operations (0, 1 or 2)
850 @return decrypted data (bytes) 849 @return decrypted data (bytes)
851 """ 850 """
852 key = bytearray(key) 851 key = bytearray(key)
853 keysize = len(key) 852 keysize = len(key)
854 assert keysize in AES.KeySize.values(), 'invalid key size: %s' % keysize 853 assert keysize in AES.KeySize.values(), \
854 'invalid key size: {0}'.format(keysize)
855 # iv is first 16 bytes 855 # iv is first 16 bytes
856 iv = bytearray(data[:16]) 856 iv = bytearray(data[:16])
857 data = bytearray(data[16:]) 857 data = bytearray(data[16:])
858 moo = AESModeOfOperation() 858 moo = AESModeOfOperation()
859 decr = moo.decrypt(data, None, mode, key, keysize, iv) 859 decr = moo.decrypt(data, None, mode, key, keysize, iv)

eric ide

mercurial