In [1]:
import basic

In [3]:
print basic.ascii2hexstring("YELLOW SUBMARINE")


59454c4c4f57205355424d4152494e45

In [39]:
def pad(s,blocklength):
    num=blocklength-(len(s)%blocklength)
    if len(s)%blocklength==0:
        return s
    else:
        return s + "".join([chr(num)]*(num))

In [8]:
print basic.ascii2hexstring(chr(0))


00

In [27]:
print basic.ascii2hexstring(pad("YELLOW SUBMARINE",4))
print pad("YELLOW SUBMARINE",20)


59454c4c4f57205355424d4152494e45
YELLOW SUBMARINE

In [2]:
print basic.splitblocks("YELLOW SUBMARINE",8)


['YELLOW S', 'UBMARINE']

In [1]:
from basic import *

In [2]:
def decryptCBCAES(k,s,iv):
    plaintext=""
    blocks=splitblocks(s,16)
    for block in blocks:
        temp=decryptECBAES(k,block)
        plaintext+=xorstrings(temp,iv)
        iv=block
    return plaintext

In [3]:
ct=""
f=open("10.txt","r")
for line in f:
    ct+=line.strip()
f.close()
ct=b642ascii(ct)

In [4]:
print decryptCBCAES("YELLOW SUBMARINE", ct, "".join([chr(0)]*16))


I'm back and I'm ringin' the bell 
A rockin' on the mike while the fly girls yell 
In ecstasy in the back of me 
Well that's my DJ Deshay cuttin' all them Z's 
Hittin' hard and the girlies goin' crazy 
Vanilla's on the mike, man I'm not lazy. 

I'm lettin' my drug kick in 
It controls my mouth and I begin 
To just let it flow, let my concepts go 
My posse's to the side yellin', Go Vanilla Go! 

Smooth 'cause that's the way I will be 
And if you don't give a damn, then 
Why you starin' at me 
So get off 'cause I control the stage 
There's no dissin' allowed 
I'm in my own phase 
The girlies sa y they love me and that is ok 
And I can dance better than any kid n' play 

Stage 2 -- Yea the one ya' wanna listen to 
It's off my head so let the beat play through 
So I can funk it up and make it sound good 
1-2-3 Yo -- Knock on some wood 
For good luck, I like my rhymes atrocious 
Supercalafragilisticexpialidocious 
I'm an effect and that you can bet 
I can take a fly girl and make her wet. 

I'm like Samson -- Samson to Delilah 
There's no denyin', You can try to hang 
But you'll keep tryin' to get my style 
Over and over, practice makes perfect 
But not if you're a loafer. 

You'll get nowhere, no place, no time, no girls 
Soon -- Oh my God, homebody, you probably eat 
Spaghetti with a spoon! Come on and say it! 

VIP. Vanilla Ice yep, yep, I'm comin' hard like a rhino 
Intoxicating so you stagger like a wino 
So punks stop trying and girl stop cryin' 
Vanilla Ice is sellin' and you people are buyin' 
'Cause why the freaks are jockin' like Crazy Glue 
Movin' and groovin' trying to sing along 
All through the ghetto groovin' this here song 
Now you're amazed by the VIP posse. 

Steppin' so hard like a German Nazi 
Startled by the bases hittin' ground 
There's no trippin' on mine, I'm just gettin' down 
Sparkamatic, I'm hangin' tight like a fanatic 
You trapped me once and I thought that 
You might have it 
So step down and lend me your ear 
'89 in my time! You, '90 is my year. 

You're weakenin' fast, YO! and I can tell it 
Your body's gettin' hot, so, so I can smell it 
So don't be mad and don't be sad 
'Cause the lyrics belong to ICE, You can call me Dad 
You're pitchin' a fit, so step back and endure 
Let the witch doctor, Ice, do the dance to cure 
So come up close and don't be square 
You wanna battle me -- Anytime, anywhere 

You thought that I was weak, Boy, you're dead wrong 
So come on, everybody and sing this song 

Say -- Play that funky music Say, go white boy, go white boy go 
play that funky music Go white boy, go white boy, go 
Lay down and boogie and play that funky music till you die. 

Play that funky music Come on, Come on, let me hear 
Play that funky music white boy you say it, say it 
Play that funky music A little louder now 
Play that funky music, white boy Come on, Come on, Come on 
Play that funky music 


In [5]:
news=decryptCBCAES("YELLOW SUBMARINE", ct, "".join([chr(0)]*16))

In [15]:
def encryptCBCAES(k,s,iv):
    ctext=""
    blocks=splitblocks(s,16)
    for block in blocks:
        t=xorstrings(block,iv)
        temp=encryptECBAES(k,t)
        ctext+=temp
        iv=temp
    return ctext

In [16]:
crypt=encryptCBCAES("YELLOW SUBMARINE", news, "".join([chr(0)]*16))

In [17]:
ct==crypt


Out[17]:
True

In [11]:
print ascii2hexstring(ct[:32])
print ascii2hexstring(crypt[:32])


091230aade3eb330dbaa4358f88d2a6cd5cf8355cb6823397ad43906df434455
091230aade3eb330dbaa4358f88d2a6c37b72d0cf4c22c344aec4142d00ce530

In [14]:
print news[:32]


I'm back and I'm ringin' the bel

In [20]:
print splitblocks(ascii2hexstring(encryptCBCAES("YELLOW SUBMARINE","".join([chr(0)]*64) , "".join([chr(0)]*16))),32)


['76d1cb4bafa246e2e3af035d6c13c372', 'c0d80fdd2e2a09d5bb60dec1df226e51', 'c9cfdc946ad38f61a2c50c9133f02724', '70e326324c3c9f8471cbecf6cbf7baef']

In [21]:
print splitblocks(ascii2hexstring(encryptECBAES("YELLOW SUBMARINE","".join([chr(0)]*64) )),32)


['76d1cb4bafa246e2e3af035d6c13c372', '76d1cb4bafa246e2e3af035d6c13c372', '76d1cb4bafa246e2e3af035d6c13c372', '76d1cb4bafa246e2e3af035d6c13c372']

In [22]:
from random import randint

In [24]:
def randomkey(keysize):
    return "".join([chr(randint(0,255)) for x in xrange(keysize)])

In [25]:
x=randomkey(16)

In [26]:
print len(x)


16

In [27]:
print ascii2hexstring(x)


f14f177a7b105bd67ca4f843d2b1d00f

In [34]:
print randint(0,1)


1

In [52]:
def randomencrypt(s):
    key=randomkey(16)
    cbefore=randint(5,10)
    cafter=randint(5,10)
    news=randomkey(cbefore)+s+randomkey(cafter)
    news=pad(news,16)
    x=randint(0,1)
    if x==0:
        out=encryptECBAES(key,news)
        print "Chose ECB"
    else:
        out=encryptCBCAES(key,news , randomkey(16))
        print "Chose CBC"
    return out

In [51]:
print splitblocks(ascii2hexstring(randomencrypt("".join([chr(0)]*64))),32)


['5adb240efd82e07cad1ce2a67144a2cd', 'ab705a1ba1df19226efa701471b7d0ea', 'ab705a1ba1df19226efa701471b7d0ea', 'ab705a1ba1df19226efa701471b7d0ea', '73dd6b59c8c9e65fce0423f5403bbb23']

In [53]:
def ECBorCBCoracle(blackbox):
    lenblock=16
    samecount=0
    c=blackbox("".join([chr(0)]*128))
    for i in xrange(len(c)/lenblock):
           for j in xrange(i+1,(len(c)/lenblock)):
                if c[i*lenblock:(i+1)*lenblock] == c[j*lenblock:(j+1)*lenblock]:
                    samecount+=1
    if samecount>0:
        return "ECB"
    else:
        return "CBC"

In [ ]:
lenblock=32
    simd={}
    for z in xrange(len(cps)):
        c=cps[z]
        count=0
        for i in xrange(len(c)/lenblock):
           for j in xrange(i+1,(len(c)/lenblock)):
                if c[i*lenblock:(i+1)*lenblock] == c[j*lenblock:(j+1)*lenblock]:
                    count+=1

In [61]:
print ECBorCBCoracle(randomencrypt)


Chose ECB
ECB

In [76]:
def repeatECB(key,s):
    appends=b642ascii("Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK")
    s=s+appends
    s=pad(s,16)
    out=encryptECBAES(key,s)
    return out

In [67]:


In [65]:
globalkey=randomkey(16)

In [85]:
print ascii2hexstring(repeatECB(globalkey,"A"*16))


3909a5a0044e6bd0c362731e9c8f61b85579becc894499529e2aaf93d8d0b609a4bc33f5d235100d785dc9b21f9d6054c6c5f80d0b34f158e1b1ad0b1226040d78a0442ea0f36fda8cafdb27b28b8d3f71c3f66d94d0be342652208abe8ebc80195e4d8a7b8db408cf7fbd0f7a763009ce2723d2d775a391f0cd78b5e076ea4ea5412d50f8e207075e60a57eb5e0b271bc399719bac3a1af8c61c34ad9968df7

In [100]:
def findblocksize(blackbox,key, limit):
    prevval=""
    for bs in xrange(2,limit):
        ct=blackbox(key,"A"*bs)
        if ct[:bs-1]==prevval:
            return bs-1
        else:
            prevval=ct[:bs]
    return None
            

findblocksize(repeatECB, globalkey,256)


Out[100]:
16

In [101]:
def newECBorCBCoracle(key,lenblock,blackbox):
    samecount=0
    c=blackbox(key,"".join([chr(0)]*128))
    for i in xrange(len(c)/lenblock):
           for j in xrange(i+1,(len(c)/lenblock)):
                if c[i*lenblock:(i+1)*lenblock] == c[j*lenblock:(j+1)*lenblock]:
                    samecount+=1
    if samecount>0:
        return "ECB"
    else:
        return "CBC"

In [89]:
print len("231ef99bbde7af1217b1fe24e4bfd32e")


32

In [102]:
newECBorCBCoracle(globalkey,16,repeatECB)


Out[102]:
'ECB'

In [103]:
onebyteshort=repeatECB(globalkey, "A"*15)

In [104]:
print onebyteshort


#
������$��.?
����֒
���Լ۴ʅ8�nVK�yCA�׉Q=NQP@�.n��p�A�N�[4�i�8N��wFR��ĺ(U���S�q^���
�}	��pJO.���@��-

�Q����,��r�i)*�w^��	H�%�٪_F@^FD�\s

In [179]:
def createdict(blackbox, key,blocksize,curblock=0,byte=1,knownletters=[]):
    d={}
    z=curblock*blocksize
    for a in xrange(0,128):
        d[blackbox(key,("A"*((blocksize-byte)))+"".join(knownletters)+chr(a))[z:z+blocksize]]=chr(a)
    #print ("A"*((blocksize-byte)))+"".join(knownletters)+chr(a)
    return d

In [111]:
d=createdict(repeatECB, globalkey,16)

In [113]:
d[onebyteshort[:16]]


Out[113]:
'R'

In [119]:
print len(repeatECB(globalkey,""))


144

In [116]:
print len(onebyteshort)


160

In [187]:
def padoracle(blackbox,blocksize,totalbytes,key):
    outs=[]
    knownletters=[]
    for b in xrange(totalbytes/blocksize):
        o=""
        for byte in xrange(1,blocksize+1):
            onebyteshort=blackbox(key, "A"*((blocksize-byte)))
            d=createdict(blackbox,key,blocksize,curblock=b,byte=byte,knownletters=knownletters)
            #print d
            try:
                knownletters.append(d[onebyteshort[b*blocksize:(b+1)*blocksize]])
                o+=d[onebyteshort[b*blocksize:(b+1)*blocksize]]
            except:
                pass
           # print o, b
        outs.append(o)
       #print outs,b
    return "".join(outs)

In [199]:
def unpad(s):
    n=s[-1:]
    i=ord(n)
    if all(map(lambda x: x==n, s[-i:])):
        return s[:-i]
    else:
        return s

In [189]:
print len(padoracle(repeatECB,16,144,globalkey))


139

In [160]:
print len("AAAAAAAAAAAAAAARollin' in my 5}")


31

In [161]:
print len("AAAAAAAAAAAAAAA")


15

In [163]:
print len("Rollin' in my 5")


15

In [175]:
print len("AAAAAAAAAAAAAARollin' in my 5.0}")


32

In [190]:
print len(b642ascii("Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK"))


138

In [197]:
print ascii2hexstring(chr(3))
print ord(chr(3))


03
3

In [200]:
unpad("Test")


Out[200]:
'Test'

In [201]:
out=padoracle(repeatECB,16,144,globalkey)

In [203]:
print ascii2hexstring(out)


526f6c6c696e2720696e206d7920352e300a57697468206d79207261672d746f7020646f776e20736f206d7920686169722063616e20626c6f770a546865206769726c696573206f6e207374616e64627920776176696e67206a75737420746f207361792068690a44696420796f752073746f703f204e6f2c2049206a7573742064726f76652062790a01

In [204]:
print ascii2hexstring(unpad(out))


526f6c6c696e2720696e206d7920352e300a57697468206d79207261672d746f7020646f776e20736f206d7920686169722063616e20626c6f770a546865206769726c696573206f6e207374616e64627920776176696e67206a75737420746f207361792068690a44696420796f752073746f703f204e6f2c2049206a7573742064726f76652062790a

In [205]:
print unpad(out)==b642ascii("Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkgaGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBqdXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUgYnkK")


True

In [ ]: