Utilities/crypto/py3AES.py

changeset 5604
b047181a4a33
parent 5389
9b1c800daff3
child 5664
9b318fcb1ee2
equal deleted inserted replaced
5603:4f2dd0850803 5604:b047181a4a33
606 "OFB": 0, 606 "OFB": 0,
607 "CFB": 1, 607 "CFB": 1,
608 "CBC": 2, 608 "CBC": 2,
609 } 609 }
610 610
611 def __extractBytes(self, input, start, end, mode): 611 def __extractBytes(self, inputData, start, end, mode):
612 """ 612 """
613 Private method to extract a range of bytes from the input. 613 Private method to extract a range of bytes from the input.
614 614
615 @param input input data (bytes) 615 @param inputData input data (bytes)
616 @param start start index (integer) 616 @param start start index (integer)
617 @param end end index (integer) 617 @param end end index (integer)
618 @param mode mode of operation (0, 1, 2) 618 @param mode mode of operation (0, 1, 2)
619 @return extracted bytes (bytearray) 619 @return extracted bytes (bytearray)
620 """ 620 """
628 i = start 628 i = start
629 j = 0 629 j = 0
630 while len(ar) < end - start: 630 while len(ar) < end - start:
631 ar.append(0) 631 ar.append(0)
632 while i < end: 632 while i < end:
633 ar[j] = input[i] 633 ar[j] = inputData[i]
634 j += 1 634 j += 1
635 i += 1 635 i += 1
636 return ar 636 return ar
637 637
638 def encrypt(self, input, mode, key, size, IV): 638 def encrypt(self, inputData, mode, key, size, IV):
639 """ 639 """
640 Public method to perform the encryption operation. 640 Public method to perform the encryption operation.
641 641
642 @param input data to be encrypted (bytes) 642 @param inputData data to be encrypted (bytes)
643 @param mode mode of operation (0, 1 or 2) 643 @param mode mode of operation (0, 1 or 2)
644 @param key key to be used (bytes) 644 @param key key to be used (bytes)
645 @param size length of the key (16, 24 or 32) 645 @param size length of the key (16, 24 or 32)
646 @param IV initialisation vector (bytearray) 646 @param IV initialisation vector (bytearray)
647 @return tuple with mode of operation, length of the input and 647 @return tuple with mode of operation, length of the input data and
648 the encrypted data (integer, integer, bytes) 648 the encrypted data (integer, integer, bytes)
649 @exception ValueError key size is invalid or decrypted data is invalid 649 @exception ValueError key size is invalid or decrypted data is invalid
650 """ 650 """
651 if len(key) % size: 651 if len(key) % size:
652 raise ValueError("Illegal size ({0}) for key '{1}'.".format( 652 raise ValueError("Illegal size ({0}) for key '{1}'.".format(
659 ciphertext = bytearray(16) 659 ciphertext = bytearray(16)
660 # the output cipher string 660 # the output cipher string
661 cipherOut = bytearray() 661 cipherOut = bytearray()
662 # char firstRound 662 # char firstRound
663 firstRound = True 663 firstRound = True
664 if input: 664 if inputData:
665 for j in range(int(math.ceil(float(len(input)) / 16))): 665 for j in range(int(math.ceil(float(len(inputData)) / 16))):
666 start = j * 16 666 start = j * 16
667 end = j * 16 + 16 667 end = j * 16 + 16
668 if end > len(input): 668 if end > len(inputData):
669 end = len(input) 669 end = len(inputData)
670 plaintext = self.__extractBytes(input, start, end, mode) 670 plaintext = self.__extractBytes(inputData, start, end, mode)
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:
713 firstRound = False 713 firstRound = False
714 ciphertext = self.aes.encrypt(iput, key, size) 714 ciphertext = self.aes.encrypt(iput, key, size)
715 # always 16 bytes because of the padding for CBC 715 # always 16 bytes because of the padding for CBC
716 for k in range(16): 716 for k in range(16):
717 cipherOut.append(ciphertext[k]) 717 cipherOut.append(ciphertext[k])
718 return mode, len(input), bytes(cipherOut) 718 return mode, len(inputData), bytes(cipherOut)
719 719
720 # Mode of Operation Decryption 720 # Mode of Operation Decryption
721 # cipherIn - Encrypted String 721 # cipherIn - Encrypted String
722 # originalsize - The unencrypted string length - required for CBC 722 # originalsize - The unencrypted string length - required for CBC
723 # mode - mode of type modeOfOperation 723 # mode - mode of type modeOfOperation

eric ide

mercurial