def my_encryption_oracle(plaintext):
   return cipher.encrypt(ca.pkcs7_pad(prefix + plaintext + suffix, AES.block_size))
from Crypto import Random
from Crypto.Cipher import AES
import cryptanalib as ca
from zlib import compress

plaintext = 'I am the very model of a modern major-general, I\'ve information vegetable, animal and mineral, I know the kings of England and I quote the fights historical, from Marathon to Waterloo in order categoricalAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'

plaintext2 = 'I am the very model of a modern major-general, I\'m covered in bees and I have information vegetable, animal and mineral, I know the kings of England and I quote the fights historical, from Marathon to Waterloo in order categoricalAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'

key = b'YELLOW SUBMARINE'
iv = Random.new().read(AES.block_size)

two_time_pad_key = Random.new().read(len(plaintext2))

ecb_cipher = AES.new(key, AES.MODE_ECB, iv)
ecb_ciphertexts = [ecb_cipher.encrypt(ca.pkcs7_pad(plaintext, AES.block_size))]
ecb_cipher = AES.new(key, AES.MODE_ECB, iv)
ecb_ciphertexts.append(ecb_cipher.encrypt(ca.pkcs7_pad(plaintext2,AES.block_size)))
print 'ECB ciphertexts are:'
print "\n".join([ct.encode('hex') for ct in ecb_ciphertexts])

cbc_cipher = AES.new(key, AES.MODE_CBC, iv)
cbc_ciphertexts = [cbc_cipher.encrypt(ca.pkcs7_pad(plaintext,AES.block_size))]
cbc_cipher = AES.new(key, AES.MODE_CBC, iv)
cbc_ciphertexts.append(cbc_cipher.encrypt(ca.pkcs7_pad(plaintext2,AES.block_size)))
print 'CBC fixed IV ciphertexts are:'
print "\n".join([ct.encode('hex') for ct in cbc_ciphertexts])

mb_xor_ciphertexts = [ca.sxor(plaintext,"\xfa\x4e\x77\x01\x43"*len(plaintext))]
mb_xor_ciphertexts.append(ca.sxor(plaintext2,"\xfa\x4e\x77\x01\x43"*len(plaintext2)))
print 'Multi-byte XOR ciphertexts are:'
Example #3
0
import cryptanalib as ca
from Crypto.Cipher import AES
from Crypto import Random

key = iv = Random.new().read(AES.block_size)

cipher = AES.new(key, mode=AES.MODE_CBC, IV=iv)
second_cipher_because_yolo = AES.new(key, mode=AES.MODE_CBC, IV=iv)

ciphertext = cipher.encrypt(ca.pkcs7_pad('Check out the mic while the DJ revolves it (ICE ICE BABY)',AES.block_size))

def decryption_oracle(ciphertext):
   return second_cipher_because_yolo.decrypt(ciphertext)

print 'Key and IV are %s and %s' % (key.encode('hex'), iv.encode('hex'))
retrieved_iv = ca.retrieve_iv(decryption_oracle, ciphertext, AES.block_size)
print 'Ciphertext is %s' % ciphertext.encode('hex')
plaintext = decryption_oracle(ciphertext)
print 'Produced plaintext is %s' % plaintext.encode('hex')
print 'First block of produced plaintext is %s' % plaintext[:AES.block_size].encode('hex')
print 'Second block of produced plaintext is %s' % plaintext[AES.block_size:AES.block_size*2].encode('hex')
print 'Retrieved IV is %s' % retrieved_iv.encode('hex')

if iv != retrieved_iv:
   raise Exception('Decryption oracle IV retrieval is broken')

from Crypto.Cipher import AES
from Crypto import Random
import cryptanalib as ca
from time import sleep

plaintext = 'I am the very model of a modern major-general'
plaintext = ca.pkcs7_pad(plaintext, AES.block_size)
print "Plaintext is " + plaintext

key = b'YELLOW SUBMARINE' #<3 matasano crypto challenges
iv = Random.new().read(AES.block_size)

def my_padding_oracle(ciphertext):
   dat_cipher = AES.new(key,AES.MODE_CBC,iv)
   if ca.pkcs7_padding_remove(dat_cipher.decrypt(ciphertext),AES.block_size) == False:
      return False
   else:
      return True

cipher = AES.new(key,AES.MODE_CBC,iv)
ciphertext = cipher.encrypt(plaintext)

print 'Running the attack with known IV:'
result = ca.padding_oracle_decrypt(my_padding_oracle, ciphertext, block_size=AES.block_size, verbose=True, iv=iv)
print result
if result != plaintext:
   raise Exception('Vaudenay\'s padding oracle attack with IV knowledge is broken.')
print ''
print 'Running the attack without knowledge of the IV:'
result = ca.padding_oracle_decrypt(my_padding_oracle, ciphertext, block_size=AES.block_size, verbose=True)
print result
def my_encryption_oracle(plaintext):
    return cipher.encrypt(
        ca.pkcs7_pad(
            'A' * random.randint(1, AES.block_size) + plaintext + suffix,
            AES.block_size))
Example #6
0
def my_encryption_oracle(plaintext):
    return cipher.encrypt(
        ca.pkcs7_pad(prefix + plaintext + suffix, AES.block_size))
def my_encryption_oracle(plaintext):
   return cipher.encrypt(ca.pkcs7_pad('A'*random.randint(1,AES.block_size) + plaintext + suffix, AES.block_size))