def EncryptAES(message, key): if len(key) != 16: raise ValueError( "Key must be 16 bytes (for AES-128, I didn't implement others)") # get round keys round_keys = MakeRoundKeys(exp_key=KeyExpansion(key, Nk=4), rounds=11) # pad message and convert to byte blocks message = pad(message, 16) msg_blocks = [message[i:i + 16] for i in range(0, len(message), 16)] cipher_blocks = [None] * len(msg_blocks) for m in range(len(cipher_blocks)): state = msg_blocks[m] # round 0 state = AddRoundKey(state, round_keys[0]) # rounds 1-10 for r in range(1, 11): state = SubBytes(state) state = ShiftRows(state) # mix columns omitted for last round if (r != 10): state = MixColumns(state) state = AddRoundKey(state, round_keys[r]) cipher_blocks[m] = state return b''.join(cipher_blocks)
def encrypt(self, plaintext): # handle bytes or string input if isinstance(plaintext, str): plaintext = plaintext.encode('ascii') elif isinstance(plaintext, bytes): None else: raise Exception("plaintext input is not str or bytes object") # break plaintext into chunks plaintext = pad(plaintext, 16) msg_blocks = [ plaintext[i:i + 16] for i in range(0, len(plaintext), 16) ] # initialize encrypted blocks cipher_blocks = [None] * len(msg_blocks) for m in range(0, len(msg_blocks)): # xor prev ciphertext block if m == 0: xor_block = xor(msg_blocks[m], self._iv) else: xor_block = xor(msg_blocks[m], cipher_blocks[m - 1]) # use block cipher cipher_blocks[m] = self._aes_cipher.encrypt(xor_block) return b''.join(cipher_blocks)
def encrypt(self, insert): return self._aes_cipher.encrypt(pad(self.make_text(insert), 16))
def make_text(self, insert): return pad( self._prefix + insert.encode('ascii') + b64decode(self._b64_target), 16)
def encrypt(self, plaintext): """encrypt using that key""" return self._aes_cipher.encrypt(pad(plaintext.encode('ascii'), 16))