def xorStr(str1, str2): str1 = encoding.hexDecode(str1) str2 = encoding.hexDecode(str2) str3 = '' for i in range(len(str1)): str3 += chr(asc(str1[i]).__xor__(asc(str2[i]))) return encoding.hexEncode(str3)
def main(): text = open('6.txt', 'r').read() text = encoding.base64Decode(text) print 'Text size:', len(text) print size = guessKeySize(text, 2, 40) print 'Provably key size:', size print part = sliceText(text, size) print 'Text splitted into %d blocks of %d bytes' % (len(part), len( part[0])) print tran = transposeBlocks(part, size) print 'Transposed blocks.' print 'Now we have %d blocks of %d bytes' % (len(tran), len(tran[0])) print key = '' for block in tran: key += chr(betterHistogram(block)) print 'The key appears to be:', key print print 'The original text would be:' print print encoding.hexDecode(set1_challenge5.xorCrypt(text, key))
def findXord(str): str = encoding.hexDecode(str) pos = [] for i in range(256): pos.append(0) tmp = unxor(str, i).lower() for wrd in english_commons: if wrd in tmp: pos[i] = pos[i] + 1 m = 0 for y in range(256): if pos[y] > pos[m]: m = y if (pos[m] == 0): return -1 return m
#!/usr/bin/env python2 import encoding asc = lambda c : ord(c) def xorCrypt(str, key): enc = '' for i in range(0, len(str), len(key)): for j in range(len(key)): if (i+j) < len(str): enc += chr(asc(str[i+j]).__xor__(asc(key[j]))) else: break return encoding.hexEncode(enc) if __name__ == '__main__': enc1 = xorCrypt('Burning \'em, if you ain\'t quick and nimble\n' + 'I go crazy when I hear a cymbal', 'ICE') dec1 = encoding.hexDecode(xorCrypt(encoding.hexDecode(enc1), 'ICE')) print dec1
out += chr(asc(str[j]).__xor__(c)) return out def findXord(str): str = encoding.hexDecode(str) pos = [] for i in range(256): pos.append(0) tmp = unxor(str, i).lower() for wrd in english_commons: if wrd in tmp: pos[i] = pos[i] + 1 m = 0 for y in range(256): if pos[y] > pos[m]: m = y if (pos[m] == 0): return -1 return m xor = '1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736' #XOR'd with char 88 (X) #Original text: "Cooking MC's like a pound of bacon" if (__name__ == '__main__'): c = findXord(xor) print c print unxor(encoding.hexDecode(xor), c)
#!/usr/bin/env python2 import encoding import set1_challenge3 asc = lambda c : ord(c) file = open('4.txt', 'r') n = 0 for line in file.readlines(): print "Line #%d: %s" %(n, line.rstrip()) c = set1_challenge3.findXord(line.rstrip()) if (c > -1): print "Encoded (%d): %s" %(c, set1_challenge3.unxor(encoding.hexDecode(line.rstrip()), c)) n += 1 #7b5a4215415d544115415d5015455447414c155c46155f4058455c5b523f #nOWTHATHEPARTYISJUMPING* (Xor'd with 21)