eric6/Utilities/crypto/py3AES.py

changeset 7628
f904d0eef264
parent 7360
9190402e4505
child 7781
607a6098cb44
equal deleted inserted replaced
7626:7f643d41464e 7628:f904d0eef264
238 # For 256-bit keys, we add an extra sbox to the calculation 238 # For 256-bit keys, we add an extra sbox to the calculation
239 if ( 239 if (
240 size == self.KeySize["SIZE_256"] and 240 size == self.KeySize["SIZE_256"] and
241 ((currentSize % size) == 16) 241 ((currentSize % size) == 16)
242 ): 242 ):
243 for l in range(4): 243 for ll in range(4):
244 t[l] = self.__getSBoxValue(t[l]) 244 t[ll] = self.__getSBoxValue(t[ll])
245 245
246 # We XOR t with the four-byte block 16, 24, 32 bytes before the new 246 # We XOR t with the four-byte block 16, 24, 32 bytes before the new
247 # expanded key. This becomes the next four bytes in the expanded 247 # expanded key. This becomes the next four bytes in the expanded
248 # key. 248 # key.
249 for m in range(4): 249 for m in range(4):
545 545
546 # encrypt the block using the expandedKey 546 # encrypt the block using the expandedKey
547 block = self.__aes_main(block, expandedKey, nbrRounds) 547 block = self.__aes_main(block, expandedKey, nbrRounds)
548 548
549 # unmap the block again into the output 549 # unmap the block again into the output
550 for k in range(4): 550 for kk in range(4):
551 # iterate over the rows 551 # iterate over the rows
552 for l in range(4): 552 for ll in range(4):
553 output[k * 4 + l] = block[k + l * 4] 553 output[kk * 4 + ll] = block[kk + ll * 4]
554 return bytes(output) 554 return bytes(output)
555 555
556 # decrypts a 128 bit input block against the given key of size specified 556 # decrypts a 128 bit input block against the given key of size specified
557 def decrypt(self, iput, key, size): 557 def decrypt(self, iput, key, size):
558 """ 558 """
598 # expand the key into an 176, 208, 240 bytes key 598 # expand the key into an 176, 208, 240 bytes key
599 expandedKey = self.__expandKey(key, size, expandedKeySize) 599 expandedKey = self.__expandKey(key, size, expandedKeySize)
600 # decrypt the block using the expandedKey 600 # decrypt the block using the expandedKey
601 block = self.__aes_invMain(block, expandedKey, nbrRounds) 601 block = self.__aes_invMain(block, expandedKey, nbrRounds)
602 # unmap the block again into the output 602 # unmap the block again into the output
603 for k in range(4): 603 for kk in range(4):
604 # iterate over the rows 604 # iterate over the rows
605 for l in range(4): 605 for ll in range(4):
606 output[k * 4 + l] = block[k + l * 4] 606 output[kk * 4 + ll] = block[kk + ll * 4]
607 return output 607 return output
608 608
609 609
610 class AESModeOfOperation(object): 610 class AESModeOfOperation(object):
611 """ 611 """
831 831
832 @param key key to be used for encryption (bytes) 832 @param key key to be used for encryption (bytes)
833 @param data data to be encrypted (bytes) 833 @param data data to be encrypted (bytes)
834 @param mode mode of operations (0, 1 or 2) 834 @param mode mode of operations (0, 1 or 2)
835 @return encrypted data prepended with the initialization vector (bytes) 835 @return encrypted data prepended with the initialization vector (bytes)
836 @exception ValueError raised to indicate an invalid key size
836 """ 837 """
837 key = bytearray(key) 838 key = bytearray(key)
838 if mode == AESModeOfOperation.ModeOfOperation["CBC"]: 839 if mode == AESModeOfOperation.ModeOfOperation["CBC"]:
839 data = append_PKCS7_padding(data) 840 data = append_PKCS7_padding(data)
840 keysize = len(key) 841 keysize = len(key)
841 assert keysize in AES.KeySize.values(), \ 842 if keysize not in AES.KeySize.values():
842 'invalid key size: {0}'.format(keysize) 843 raise ValueError('invalid key size: {0}'.format(keysize))
843 # create a new iv using random data 844 # create a new iv using random data
844 iv = bytearray([i for i in os.urandom(16)]) 845 iv = bytearray([i for i in os.urandom(16)])
845 moo = AESModeOfOperation() 846 moo = AESModeOfOperation()
846 mode, length, ciph = moo.encrypt(data, mode, key, keysize, iv) 847 mode, length, ciph = moo.encrypt(data, mode, key, keysize, iv)
847 # With padding, the original length does not need to be known. It's a bad 848 # With padding, the original length does not need to be known. It's a bad
857 @param key key to be used for decryption (bytes) 858 @param key key to be used for decryption (bytes)
858 @param data data to be decrypted (with initialization vector prepended) 859 @param data data to be decrypted (with initialization vector prepended)
859 (bytes) 860 (bytes)
860 @param mode mode of operations (0, 1 or 2) 861 @param mode mode of operations (0, 1 or 2)
861 @return decrypted data (bytes) 862 @return decrypted data (bytes)
863 @exception ValueError raised to indicate an invalid key size
862 """ 864 """
863 key = bytearray(key) 865 key = bytearray(key)
864 keysize = len(key) 866 keysize = len(key)
865 assert keysize in AES.KeySize.values(), \ 867 if keysize not in AES.KeySize.values():
866 'invalid key size: {0}'.format(keysize) 868 raise ValueError('invalid key size: {0}'.format(keysize))
867 # iv is first 16 bytes 869 # iv is first 16 bytes
868 iv = bytearray(data[:16]) 870 iv = bytearray(data[:16])
869 data = bytearray(data[16:]) 871 data = bytearray(data[16:])
870 moo = AESModeOfOperation() 872 moo = AESModeOfOperation()
871 decr = moo.decrypt(data, None, mode, key, keysize, iv) 873 decr = moo.decrypt(data, None, mode, key, keysize, iv)

eric ide

mercurial