master
1import base64
2import string
3import collections
4import csv
5import random
6from math import sqrt
7import itertools
8import simple_crypto as sc# helper functions
9
10stl = 20 # str_tips length
11do_4 = False
12
13# 1. Convert hex to base64 and back.
14print "\n---- Problem 1: Convert hex to base64 and back ----"
15p1_hex = "49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"
16p1_b64 = "SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t"
17p1_hex_dec = p1_hex.decode ('hex')
18p1_b64_enc = base64.b64encode (p1_hex_dec)
19p1_b64_dec = base64.b64decode (p1_b64_enc)
20p1_hex_enc = p1_b64_dec.encode ('hex')
21print "hex (G) :", sc.str_tips (p1_hex, stl)
22print "b64 (G) :", sc.str_tips (p1_b64, stl)
23print "hex_dec :", p1_hex_dec
24print "b64_enc :", sc.str_tips (p1_b64_enc, stl)
25print "b64_dec :", p1_b64_dec
26print "hex_enc :", sc.str_tips (p1_hex_enc, stl)
27assert p1_b64_enc == p1_b64, 'Hex -> B64 failed'
28assert p1_b64_dec == p1_hex_dec, 'Decoding mismatch (dec b64 != dec hex)'
29assert p1_hex_enc == p1_hex, 'b64 -> hex failed'
30
31
32# 2. Fixed XOR
33print "\n---- Problem 2: Fixed XOR ----"
34p2_a = "1c0111001f010100061a024b53535009181c"
35p2_b = "686974207468652062756c6c277320657965"
36p2_r = "746865206b696420646f6e277420706c6179"
37p2_a_dec = p2_a.decode ('hex')
38p2_b_dec = p2_b.decode ('hex')
39p2_r_dec = p2_r.decode ('hex')
40p2_o_dec = sc.xor_string(p2_a_dec, p2_b_dec)
41print "a_dec : ", p2_a_dec
42print "b_dec : ", p2_b_dec
43print "a^b (G) : ", p2_r_dec
44print "a^b : ", p2_o_dec
45assert p2_o_dec.encode ('hex') == p2_r, 'a^b failed'
46
47
48# 3. Single-character XOR Cipher
49print "\n---- Problem 3: Single-character XOR Cipher ----"
50p3_hex = "1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736"
51p3_hex_dec = p3_hex.decode ('hex')
52f = sc.freq_array_load()
53key, value = sc.ngram_freq_cos_sim (p3_hex_dec, f)
54p3_xor_dec = sc.xor_char (p3_hex_dec, key)
55p3_xor_enc = sc.xor_char (p3_xor_dec, key)
56p3_hex_enc = p3_xor_enc.encode ('hex')
57print "p3 dec : ", key
58print "p3 dec : ", p3_xor_dec
59assert p3_hex == p3_hex_enc, 'dec/enc failed'
60
61# 4. Detect single-character XOR
62if do_4:
63 print "\n---- Problem 4: Detect single-character XOR ----"
64 lines = [line.strip() for line in open('gistfile_p4.txt')]
65 best = (0,'',"")
66 f = sc.freq_array_load()
67 for x in lines:
68 x_raw = x.decode('hex')
69 key, value = sc.ngram_freq_cos_sim (x_raw, f)
70 if value > best[0]:
71 best = (value, key, x)
72 print "p4 key : ", best[1]
73 print "p4 dec : ", sc.xor_str (best[2].decode('hex'),best[1]).strip()
74
75
76# 5. Repeating-key XOR Cipher
77print "\n---- Problem 5: Repeating-key XOR Cipher ----"
78p5_str = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal"
79p5_key = "ICE"
80p5_enc_hex = "0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f"
81p5_enc = p5_enc_hex.decode('hex')
82p5_str_enc = sc.xor_str(p5_str,p5_key)
83p5_str_enc_hex = sc.xor_str(p5_str,p5_key).encode('hex')
84p5_dec = sc.xor_str(p5_enc, p5_key)
85print "p5 str (G) :", sc.str_tips (p5_str, stl)
86print "p5 enc hex (G) :", sc.str_tips (p5_enc_hex, stl)
87print "p5 str enc hex :", sc.str_tips (p5_str_enc_hex, stl)
88print "p5 dec :", sc.str_tips (p5_dec, stl)
89assert p5_enc == p5_str_enc, "p5 encryption failed"
90assert p5_dec == p5_str, "p5 decryption failed"
91
92# 5b. Encrypt a bunch of stuff using your repeating-key XOR function
93keys = 10
94sa = [] # string array
95sa.append ("Please! This is supposed to be a happy occasion.")
96sa.append ("Let's not bicker and argue over who killed who.")
97sa.append ("Go and boil your bottoms, you sons of silly persons!")
98sa.append ("Listen, strange women lyin' in ponds distributin' swords is no basis for a system of government.")
99sa.append ("Supreme executive power derives from a mandate from the masses, not from some farcical aquatic ceremony.")
100for s in sa:
101 for k in range(keys):
102 key_len = random.randint (3,30)
103 key = ''.join (random.choice (string.ascii_letters) for r in range (key_len))
104 assert sc.test_xor (s,key)
105print "Repeating-key XOR tested successfully on:", "\n\t", \
106 len(sa), "strings", "\n\t", \
107 len(sa)*keys, "unique keys"
108
109
110# 6. Break repeating-key XOR
111print "\n---- Problem 6: Break repeating-key XOR ----"
112p6_a = "this is a test"
113p6_b = "wokka wokka!!!"
114p6_hd_ab = sc.hamming_dist (p6_a, p6_b)
115assert p6_hd_ab == 37 # Given
116print "Hamming Distance [", p6_a,",", p6_b,"] =", p6_hd_ab
117p6_str_b64 = ''.join([line.strip() for line in open('gistfile_p6.txt')])
118p6_str = base64.b64decode (p6_str_b64)
119p6_key_len = sc.key_len_finder (p6_str)
120print "Possible p6_key_len :", p6_key_len[0][0],",", p6_key_len[1][0],",", p6_key_len[2][0]
121
122
123
124s = "DFJKSDJFKsjlakfjdsfkd"
125key_len = 5
126
127
128print sc.group_and_trans (s,key_len)
129