def encryptWithRandomKey(data): key = bytes2str(randomBytes(16)) if random.random() < 0.5: result = 'ECB' aes = AES.new(key, AES.MODE_ECB) else: result = 'CBC' iv = '\x00' * 16 aes = AES.new(key, AES.MODE_CBC, iv) numBytesBefore = random.randrange(5, 10) numBytesAfter = random.randrange(5, 10) # print numBytesBefore, numBytesAfter bytesBefore = bytes2str(randomBytes(numBytesBefore)) bytesAfter = bytes2str(randomBytes(numBytesAfter)) input = bytesBefore + data + bytesAfter input = pkcs7(input, 16) # input = data output = aes.encrypt(input) return output, result
def encrypt(self, input): fullInput = input + self._extraText output = self._aes.encrypt(pkcs7(fullInput, 16)) return output
#!/usr/bin/env python from util import pkcs7, str2bytes input = 'YELLOW SUBMARINE' padded = pkcs7(input, 20) print padded print str2bytes(padded)