Week 1, programming assignment


In [1]:
MSGS = [
    "315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc41556bdb36bbca3e8774301fbcaa3b83b220809560987815f65286764703de0f3d524400a19b159610b11ef3e",
    "234c02ecbbfbafa3ed18510abd11fa724fcda2018a1a8342cf064bbde548b12b07df44ba7191d9606ef4081ffde5ad46a5069d9f7f543bedb9c861bf29c7e205132eda9382b0bc2c5c4b45f919cf3a9f1cb74151f6d551f4480c82b2cb24cc5b028aa76eb7b4ab24171ab3cdadb8356f",
    "32510ba9a7b2bba9b8005d43a304b5714cc0bb0c8a34884dd91304b8ad40b62b07df44ba6e9d8a2368e51d04e0e7b207b70b9b8261112bacb6c866a232dfe257527dc29398f5f3251a0d47e503c66e935de81230b59b7afb5f41afa8d661cb",
    "32510ba9aab2a8a4fd06414fb517b5605cc0aa0dc91a8908c2064ba8ad5ea06a029056f47a8ad3306ef5021eafe1ac01a81197847a5c68a1b78769a37bc8f4575432c198ccb4ef63590256e305cd3a9544ee4160ead45aef520489e7da7d835402bca670bda8eb775200b8dabbba246b130f040d8ec6447e2c767f3d30ed81ea2e4c1404e1315a1010e7229be6636aaa",
    "3f561ba9adb4b6ebec54424ba317b564418fac0dd35f8c08d31a1fe9e24fe56808c213f17c81d9607cee021dafe1e001b21ade877a5e68bea88d61b93ac5ee0d562e8e9582f5ef375f0a4ae20ed86e935de81230b59b73fb4302cd95d770c65b40aaa065f2a5e33a5a0bb5dcaba43722130f042f8ec85b7c2070",
    "32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd2061bbde24eb76a19d84aba34d8de287be84d07e7e9a30ee714979c7e1123a8bd9822a33ecaf512472e8e8f8db3f9635c1949e640c621854eba0d79eccf52ff111284b4cc61d11902aebc66f2b2e436434eacc0aba938220b084800c2ca4e693522643573b2c4ce35050b0cf774201f0fe52ac9f26d71b6cf61a711cc229f77ace7aa88a2f19983122b11be87a59c355d25f8e4",
    "32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd90f1fa6ea5ba47b01c909ba7696cf606ef40c04afe1ac0aa8148dd066592ded9f8774b529c7ea125d298e8883f5e9305f4b44f915cb2bd05af51373fd9b4af511039fa2d96f83414aaaf261bda2e97b170fb5cce2a53e675c154c0d9681596934777e2275b381ce2e40582afe67650b13e72287ff2270abcf73bb028932836fbdecfecee0a3b894473c1bbeb6b4913a536ce4f9b13f1efff71ea313c8661dd9a4ce",
    "315c4eeaa8b5f8bffd11155ea506b56041c6a00c8a08854dd21a4bbde54ce56801d943ba708b8a3574f40c00fff9e00fa1439fd0654327a3bfc860b92f89ee04132ecb9298f5fd2d5e4b45e40ecc3b9d59e9417df7c95bba410e9aa2ca24c5474da2f276baa3ac325918b2daada43d6712150441c2e04f6565517f317da9d3",
    "271946f9bbb2aeadec111841a81abc300ecaa01bd8069d5cc91005e9fe4aad6e04d513e96d99de2569bc5e50eeeca709b50a8a987f4264edb6896fb537d0a716132ddc938fb0f836480e06ed0fcd6e9759f40462f9cf57f4564186a2c1778f1543efa270bda5e933421cbe88a4a52222190f471e9bd15f652b653b7071aec59a2705081ffe72651d08f822c9ed6d76e48b63ab15d0208573a7eef027",
    "466d06ece998b7a2fb1d464fed2ced7641ddaa3cc31c9941cf110abbf409ed39598005b3399ccfafb61d0315fca0a314be138a9f32503bedac8067f03adbf3575c3b8edc9ba7f537530541ab0f9f3cd04ff50d66f1d559ba520e89a2cb2a83"
]
DE = "32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904"

In [2]:
import sys

def strxor(a, b):     # xor two strings of different lengths
    #a = bytearray(a)
    #b = bytearray(b)
    if len(a) > len(b):
       return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])
       #return "".join([chr(x ^ y) for (x, y) in zip(a[:len(b)], b)])
       '''
        chr() return the string representing a character whose Unicode code point is the integer i.
        ord() Given a string representing one Unicode character, return an integer representing the Unicode code point of that character.
       '''
    else:
       return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])
       #return "".join([chr(x ^ y) for (x, y) in zip(a, b[:len(a)])])

def random(size=16):
    return open("/dev/urandom",encoding = "ISO-8859-1").read(size)

def encrypt(key, msg):
    c = strxor(key, msg)
    print
    #print (c.encode('hex'))
    #print(c.encode("ISO-8859-1"))
    #print (codecs.encode(c, "ISO-8859-1"))
    return c

def main():
    key = random(1024)
    ciphertexts = [(key, msg) for msg in MSGS]

In [3]:
#" " ascii = 0010 0000
a = "afdsfasdi"
b = "  h f g "
print("a ^ b = ",strxor(b,a),"\n")
print("a ^ b ^ b = ",strxor(strxor(b,a),b))


a ^ b =  AFSAD 

a ^ b ^ b =  afdsfasd

In [4]:
m1 = " I am a boy"
m2 = "You are pig"
key = random(1024)
c1 = encrypt(key,m1)
c2 = encrypt(key,m2)
c1
c2
encrypt(c1,c2)


Out[4]:
'y&UA\x0cR\x04\x00\x12\x06\x1e'

In [5]:
import codecs
M = [" "]*len(MSGS)
for i in range(len(MSGS)):
    M[i] = [str(chr(k)) for k in codecs.decode(MSGS[i],'hex')]

In [6]:
ciphertexts = [encrypt(M[6], msg) for msg in M]

l = len(ciphertexts)

guess = []*len(ciphertexts)
for m in range(l):
    ll = len(ciphertexts[m])
    gu = []
    for i in range(ll):
        if(ciphertexts[m][i].isalpha()):
            gu.append(ciphertexts[m][i])
            #guess.insert(i,ciphertexts[m][i])
            #guess[i] = ciphertexts[m][i]
        else:
            gu.append("_")
            #guess.remove(i)
            #guess.insert(i," ")
            #if(~guess[i].isalpha()):
            #guess[i] = " "
    guess.append(gu)
six = []
for le in range(len(guess[6])):
    temp = []
    for i in guess:
        if(len(i)> le):
            if( (i[le].isalpha())):
                temp.append(i[le])
    temp.sort()
    print(temp)
    if len(temp) > 1:
        tt = 0
        maxx = 0
        for m in range(len(temp)):
            if m==0:
                ttext = temp[m]
                tt = 1
                maxx = 1
            else:
                if temp[m] == temp[m-1]:
                    tt = tt + 1
                    if(tt > maxx):
                        ttext = temp[m]
                        maxx = tt
                else:
                    tt = 1
        six.append(ttext)
    elif len(temp) ==1 :
        six.append(temp[0])
    else:
        six.append("_")
print(six)


['t']
['H']
['E', 'E', 'M']
['R', 'R', 'R']
['E']
['I', 'I', 'I', 'N', 'N', 'O', 'c']
['A', 'A']
['U']
['E']
['C', 'E', 'E', 'I', 'L', 'R', 'T']
['T', 'Y']
['W']
['O']
['E', 'G', 'R', 'T', 'T', 'Y', 'o']
['T', 'T', 'T', 'T']
['Y']
['P']
['E', 'E']
[]
['C', 'T', 'T', 'U', 'U', 'U', 'Y', 'd']
['O', 'O', 'O']
['F']
['A', 'D', 'E', 'E', 'H', 'N', 'P', 'T']
['C', 'C']
['Y']
['A']
['A', 'T', 'T', 'T']
['O', 'O', 'O']
['G', 'G']
['R']
['A', 'A', 'I']
['B', 'P', 'P']
['H', 'X']
['I', 'Y']
['C', 'J', 'M', 'M', 'O']
['A', 'K', 'N', 'S']
['B', 'O']
['N']
['E', 'E']
['C', 'E', 'H', 'M', 'P', 'U', 'Ï']
['T', 'Ø']
['H', 'é']
['A', 'R']
['T']
['A', 'H', 'O', 'P', 'P', 'R', 'S']
['A']
['L', 'L']
['L']
['O']
['W']
['S']
['H', 'L', 'O', 'O', 'R', 'T', 'W']
['T']
['H', 'H']
['E', 'E', 'E', 'I']
['A', 'C', 'E', 'L', 'N', 'S']
[]
['O', 'O', 'O']
['V', 'V']
['E']
['R']
['N']
['M']
['E', 'E', 'E', 'E']
['N', 'N', 'N']
['T']
['C', 'E', 'L', 'O', 'R', 'T']
['T']
['O']
['A', 'E', 'E', 'F', 'R', 'R']
['U', 'U']
['S', 'S']
['E']
['A', 'E', 'E', 'F', 'I', 'N', 'R']
['B', 'B']
['R']
['U']
['T']
['E', 'E', 'E']
['C', 'C', 'E', 'E', 'G', 'M', 'O', 'U']
['F']
['B', 'O', 'O']
['C', 'R', 'R', 'R']
['C', 'C', 'V']
['E', 'H', 'H']
['N', 'N', 'O', 'R', 'T', 'T', 'W']
[]
['O', 'O']
['C', 'C', 'G', 'H', 'N', 'P', 'R', 'Y']
['B', 'B', 'B']
['R']
['E', 'E']
[]
['E', 'K', 'K']
['E', 'F', 'G', 'H', 'O', 'R']
['T', 'T', 'X']
['H', 'H', 'H']
['E']
['A', 'N', 'P', 'R', 'T', 'U']
[]
['O', 'O']
[]
['B', 'E']
['A', 'H', 'I', 'M']
['E', 'M', 'N', 'T', 'T', 'U']
['A']
['N']
['D']
['A', 'F', 'I', 'I', 'O', 'O', 'Y']
[]
[]
['E', 'E', 'E', 'E']
['E', 'N', 'O', 'O', 'T', 'W']
[]
['H', 'H', 'H']
['L']
['T', 'T']
['G', 'I', 'K', 'P', 'a']
[]
[]
['Q']
['U']
['E']
['R', 'R']
['E', 'H']
['S']
['D', 'E', 'R', 'r']
['T']
[]
['E', 'E']
['L', 'P', 'R', 'S']
[]
['O']
['V']
['E']
[]
[]
[]
[]
['N', 'N']
[]
['A', 'O', 'O']
[]
['O']
['D']
[]
[]
[]
['E', 'Y']
[]
[]
[]
[]
[]
['T']
['F', 'é']
['B']
['R']
[]
[]
['U']
[]
[]
[]
[]
[]
[]
[]
[]
['I']
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
['t', 'H', 'E', 'R', 'E', 'I', 'A', 'U', 'E', 'E', 'T', 'W', 'O', 'T', 'T', 'Y', 'P', 'E', '_', 'U', 'O', 'F', 'E', 'C', 'Y', 'A', 'T', 'O', 'G', 'R', 'A', 'P', 'H', 'I', 'M', 'A', 'B', 'N', 'E', 'C', 'T', 'H', 'A', 'T', 'P', 'A', 'L', 'L', 'O', 'W', 'S', 'O', 'T', 'H', 'E', 'A', '_', 'O', 'V', 'E', 'R', 'N', 'M', 'E', 'N', 'T', 'C', 'T', 'O', 'E', 'U', 'S', 'E', 'E', 'B', 'R', 'U', 'T', 'E', 'C', 'F', 'O', 'R', 'C', 'H', 'N', '_', 'O', 'C', 'B', 'R', 'E', '_', 'K', 'E', 'T', 'H', 'E', 'A', '_', 'O', '_', 'B', 'A', 'T', 'A', 'N', 'D', 'I', '_', '_', 'E', 'O', '_', 'H', 'L', 'T', 'G', '_', '_', 'Q', 'U', 'E', 'R', 'E', 'S', 'D', 'T', '_', 'E', 'L', '_', 'O', 'V', 'E', '_', '_', '_', '_', 'N', '_', 'O', '_', 'O', 'D', '_', '_', '_', 'E', '_', '_', '_', '_', '_', 'T', 'F', 'B', 'R', '_', '_', 'U', '_', '_', '_', '_', '_', '_', '_', '_', 'I', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_']

We guess text6 is "There are two types of cyptography: one that allows the Government to use brute force "

  • 't', 'H', 'E', 'R', 'E', 'I', 'A', 'U', 'E', 'E', 'T', 'W', 'O', 'T', 'T', 'Y', 'P', 'E', '', 'U', 'O', 'F', 'E', 'C', 'Y', 'A', 'T', 'O', 'G', 'R', 'A', 'P', 'H', 'I', 'M', 'A', 'B', 'N', 'E', 'C', 'T', 'H', 'A', 'T', 'P', 'A', 'L', 'L', 'O', 'W', 'S', 'O', 'T', 'H', 'E', 'A', '', 'O', 'V', 'E', 'R', 'N', 'M', 'E', 'N', 'T', 'C', 'T', 'O', 'E', 'U', 'S', 'E', 'E', 'B', 'R', 'U', 'T', 'E', 'C', 'F', 'O', 'R', 'C', 'H'
  • There aue two types of cyatographi? bne that allows the _overnment to use brute forch
  • We can also try another text to get exact key

In [7]:
import codecs
n6 = codecs.decode(MSGS[6],'hex')
n60 = []
n60 = n60 + [str(chr(i)) for i in codecs.decode(MSGS[6],'hex')]
crib ="There are two types of cyptography: one that allows the Government to use brute force "
key = encrypt(n60,crib)
n9 = codecs.decode(DE,'hex')
n90 = []
n90 = n90 + [str(chr(i)) for i in n9]
encrypt(key,n90)


Out[7]:
'The secret message is: When using a stream cipher, never use the key more than once'

In [8]:
a = 80
a = a+1
str(chr(a))


Out[8]:
'Q'