def solve_x(bars, bars_pos, s): #if len(s) == 4: if False: barsp = bars.prev() #print("a='{0}'".format(barsp[20:30])) if barsp[20:30] == ' ' * 10: print("bars={0} s={1}".format(str(), s)) return bars.prev() bars_pos -= 1 if bars_pos < 0: return for c in string.ascii_uppercase: new_bars = Bars(bars) new_bars_pos = bars_pos code = encode_morse(c) rcode = code[::-1] for i in rcode: new_bars_pos -= 1 if new_bars < 0: break new_bars.xor(new_bars_pos, i) #print("{0} c={1}".format(str(new_bars), code)) #print("bars={0} c={1}".format(str(new_bars), c)) if new_bars_pos <= 0: print("bars={0} s={1}".format(str(new_bars), s)) else: print("new_bars_pos={0} codelen={1}".format( new_bars_pos, len(code))) bp = str(new_bars)[new_bars_pos:new_bars_pos + len(code)] print("bars='{0}' bp='{1}' c={2}".format(str(new_bars), bp, c + s)) if bp == ' ' * (len(code)): solve_x(new_bars, new_bars_pos, c + s)
def handle_starttag(self, tag, attrs): attrs_dict = dict(attrs) if self.verifing > 0: self.verifing += 1 elif "data-sign" in attrs_dict: print("data-sign: " + attrs_dict["data-sign"]) self.verifing = 1 self.signature = int(attrs_dict["data-sign"]) self.bars = Bars(" " * 32) self.bars_pos = 0
def test_rule(self): self.assertEquals(str(Bars(" I ").next()), "iIi ") self.assertEquals(str(Bars(" ii ").next()), "iIIi") self.assertEquals(str(Bars(" Ii ").next()), "iTIi") self.assertEquals(str(Bars(" TI ").next()), " Ii") self.assertEquals(str(Bars(" II ").next()), "iTTi") self.assertEquals(str(Bars("iIi ").prev()), " I ") self.assertEquals(str(Bars("iIIi").prev()), " ii ") self.assertEquals(str(Bars("iTIi").prev()), " Ii ") self.assertEquals(str(Bars(" Ii").prev()), " TI ") self.assertEquals(str(Bars("iTTi").prev()), " II ")
def inverse(): pat = [] for c in list(s): pat.append(table[c]) for p in itertools.product(*pat): line = ''.join(p) bs = Bars(line) n = str(bs.next()) if s == n: return line
class SignatureParser(HTMLParser): def __init__(self): HTMLParser.__init__(self) self.verifing = 0 self.e = 65537 self.n = 47775743999999999999 # TODO: this bit length is too short for RSA! def handle_starttag(self, tag, attrs): attrs_dict = dict(attrs) if self.verifing > 0: self.verifing += 1 elif "data-sign" in attrs_dict: print("data-sign: " + attrs_dict["data-sign"]) self.verifing = 1 self.signature = int(attrs_dict["data-sign"]) self.bars = Bars(" " * 32) self.bars_pos = 0 def handle_data(self, data): if self.verifing > 0: for c in data: c = c.upper() assert (ord(' ') < ord(c) and ord(c) < 127) code = encode_morse(c) print('code=' + code) for i in code: self.bars.xor(self.bars_pos, i) self.bars_pos += 1 self.bars_pos %= len(self.bars) self.bars_pos += 1 self.bars_pos %= len(self.bars) print(self.bars) self.bars.next() print(self.bars) def handle_endtag(self, tag): if self.verifing > 0: self.verifing -= 1 if self.verifing == 0: hash_value = self.bars.num() if verify_RSA_signature(hash_value, self.e, self.n, self.signature): print("verification succeeded!") else: print("verification failed")
def test_loop(self): bs = Bars("Ti ") self.assertEquals(str(bs.next()), " Ti ") self.assertEquals(str(bs.next()), " Ti") self.assertEquals(str(bs.next()), "i T") self.assertEquals(str(bs.next()), "Ti ") bs = Bars(" iT") self.assertEquals(str(bs.next()), " iT ") self.assertEquals(str(bs.next()), "iT ") self.assertEquals(str(bs.next()), "T i") self.assertEquals(str(bs.next()), " iT")
def handle_starttag(self, tag, attrs): attrs_dict = dict(attrs) if self.verifing > 0: self.verifing += 1 elif "data-sign" in attrs_dict: print("data-sign: " + attrs_dict["data-sign"]) self.verifing = 1 self.signature = int(attrs_dict["data-sign"]) self.bars = Bars(" "*32) self.bars_pos = 0
class SignatureParser(HTMLParser): def __init__(self): HTMLParser.__init__(self) self.verifing = 0 self.e = 65537 self.n = 47775743999999999999 # TODO: this bit length is too short for RSA! def handle_starttag(self, tag, attrs): attrs_dict = dict(attrs) if self.verifing > 0: self.verifing += 1 elif "data-sign" in attrs_dict: print("data-sign: " + attrs_dict["data-sign"]) self.verifing = 1 self.signature = int(attrs_dict["data-sign"]) self.bars = Bars(" "*32) self.bars_pos = 0 def handle_data(self, data): if self.verifing > 0: for c in data: c = c.upper() assert(ord(' ') < ord(c) and ord(c) < 127) code = encode_morse(c) for i in code: self.bars.xor(self.bars_pos, i) self.bars_pos += 1; self.bars_pos %= len(self.bars) self.bars_pos += 1; self.bars_pos %= len(self.bars) print(self.bars) self.bars.next() print(self.bars) def handle_endtag(self, tag): if self.verifing > 0: self.verifing -= 1 if self.verifing == 0: hash_value = self.bars.num() if verify_RSA_signature(hash_value, self.e, self.n, self.signature): print("verification succeeded!") else: print("verification failed")
def solve(): orig = ' iT iTiI T TTiTIITITIITTi iTiIIT' bars = Bars(orig) bars_pos = len(orig) - 7 solve_x(bars, bars_pos, "") if False: bars.prev() print(str(bars)) for c in string.ascii_uppercase: new_bars = Bars(bars) bars_pos = len(orig) - 7 code = encode_morse(c) rcode = code[::-1] for i in rcode: new_bars.xor(bars_pos, i) bars_pos -= 1 bars_pos %= len(bars) print("new_bars={0} xor={1}".format(str(new_bars), code)) print("prv_bars={0}".format(str(new_bars.prev())))
def solve5(answer, depth): if depth == 0: print("anser={0}".format(''.join(answer))) return False for i in range(depth): for c in string.ascii_uppercase: tmp_answer = list(answer) tmp_answer.append(c) bars = Bars(tmp_answer) if simple_check(tmp_answer) == False: continue elif solve5(tmp_answer, depth - 1): return True return False
def solve_with_backtracking(answer, depth): print("solve_with_backtracking({0},{1})".format(''.join(answer), str(depth))) if depth == 1: for c in string.ascii_uppercase: new_answer = list(answer) new_answer.append(c) if check_answer(new_answer): print("answer=" + str(new_answer)) return True return False for i in range(depth): for c in string.ascii_uppercase: tmp_answer = list(answer) tmp_answer.append(c) bars = Bars(tmp_answer) if simple_check(tmp_answer) == False: continue elif solve_with_backtracking(tmp_answer, depth - 1): return True return False
def check_answer(answer): bars = Bars(" "*32) bars_pos = 0 for c in answer: c = c.upper() assert(ord(' ') < ord(c) and ord(c) < 127) code = encode_morse(c) for i in code: bars.xor(bars_pos, i) bars_pos += 1; bars_pos %= len(bars) bars_pos += 1; bars_pos %= len(bars) bars.next() hash_value = bars.num() if True: s = bars_num_to_str(hash_value) ans = "iT iTiI T" if 0 <= s.find(ans): print('anser='.join(answer)) return hash_value == 1758412232636122750
def simple_check(answer): chk_str = "iTiIIT" bars = Bars(" "*32) bars_pos = 0 count = 0 for c in answer: c = c.upper() assert(ord(' ') < ord(c) and ord(c) < 127) code = encode_morse(c) for i in code: bars.xor(bars_pos, i) bars_pos += 1; bars_pos %= len(bars) bars_pos += 1; bars_pos %= len(bars) bars.next() #print("answer={0} bars={1} bars_str={2}".format(''.join(answer), str(bars), str(bars)[len(bars)-len(answer):])) return chk_str[0:len(answer)] == str(bars)[len(bars)-len(answer):]
def solve3(): e = 65537 n = 47775743999999999999 # TODO: this bit length is too short for RSA! h = 1758412232636122750 signature = 26984024434151540355 bars = Bars(" "*32) bars_pos = 0 data = "EBMDKA" for c in data: c = c.upper() print("c=" + c) assert(ord(' ') < ord(c) and ord(c) < 127) code = encode_morse(c) for i in code: bars.xor(bars_pos, i) bars_pos += 1; bars_pos %= len(bars) bars_pos += 1; bars_pos %= len(bars) print(bars) bars.next() print(bars) hash_value = bars.num() return lets_take_tea_break(hash_value, e, n, signature)
def test_next(self): bs = Bars("I IT ii i I I i i I T") bs.next() self.assertEquals(str(bs), "Ii iI iIIiiT Ii iI Ti iTi iIi ") bs.next() self.assertEquals(str(bs), "TIiiIT IIITI iTI ITi T TiT IIIii")
#/usr/bin/env python from second_code import Bars from third_code import decode_morse def take_away_tea(s): return s.replace('T','') str1 = "ITT TI I T TIii" str2 = "T i I Iii TTT" s = str2 for i in range(1000): bs = Bars(s) if bs.next() == str1: print("answer: " + ''.join([take_away_tea(decode_morse(take_away_tea(x))) for x in [s, str1, str2]])) break s = str(bs)
#/usr/bin/env python from second_code import Bars from third_code import decode_morse bs = Bars("I IT ii i I I i i I T") for i in range(26): print(bs) bs.next() print(bs) print("answer: " + decode_morse(str(bs)))
#!/usr/bin/env python from second_code import Bars from third_code import decode_morse bs = Bars("ITT TI I T TIii") while True: prev = bs[:] bs.next() if ''.join(bs) == "ITT TI I T TIii": break print ''.join(prev) print decode_morse(''.join(prev))
from second_code import Bars from third_code import decode_morse val="ITT TI I T TIii" bs = Bars(val) i=0; while(True): before=val val=str(bs.next()) if (val == "ITT TI I T TIii"): break print decode_morse(before) #print("answer: " + decode_morse(str(bs)))
def test_simple_rule(self): self.assertEquals(str(Bars(" ").next()), " ") self.assertEquals(str(Bars(" i ").next()), " iTi ") self.assertEquals(str(Bars(" i i ").next()), "iT Ti") self.assertEquals(str(Bars(" T ").next()), " i ") self.assertEquals(str(Bars(" TiT ").next()), " T ") self.assertEquals(str(Bars(" iTi ").next()), "iTiTi") self.assertEquals(str(Bars(" TTT ").next()), " iii ") self.assertEquals(str(Bars(" ").prev()), " ") self.assertEquals(str(Bars(" iTi ").prev()), " i ") self.assertEquals(str(Bars("iT Ti").prev()), " i i ") self.assertEquals(str(Bars(" i ").prev()), " T ") self.assertEquals(str(Bars(" T ").prev()), " TiT ") self.assertEquals(str(Bars("iTiTi").prev()), " iTi ") self.assertEquals(str(Bars(" iii ").prev()), " TTT ")
def solve2(): h = 1758412232636122750 s = bars_num_to_str(h) print("bars.str:" + s) barsd = Bars(s) bars = Bars(' ' + str(barsd)) print("bars.num:" + str(bars.num())) bars_len = len(bars) print("bars.len=" + str(bars_len)) bars_pos = bars_len - 1 answer = [] while 0 <= bars_pos: found = False for c in string.ascii_uppercase: tmp_bars = Bars(bars) tmp_bars.prev() tmp_pos = bars_pos code = encode_morse(c) rcode = reverse_str(code) ok = 0 for i in rcode: tmp_bars.xor(tmp_pos, i) if tmp_bars[tmp_pos] != ' ': ok += 1 tmp_pos -= 1; tmp_pos %= bars_len if len(code) - 1 <= ok: found = True bars.prev() #print("c=" + c) answer.append(c) for i in rcode: bars.xor(bars_pos, i) bars_pos -= 1; bars_pos %= bars_len break if found == False: print("failed") break print("bars=" + str(bars))