def main(): enc = tools.fromHex(STR) res = [[scoring.compute(crypto.xor(enc, i)),i,crypto.xor(enc, i)] for i in range(256)] res = sorted(res) for x in res[:-1]: if x[0]>0: tools.d(x) print(res[-1])
def main(): res = [] with open('4.txt','r') as f: for line in f.readlines(): enc = tools.fromHex(line.strip()) dec = [[scoring.compute(crypto.xor(enc, i)),i,crypto.xor(enc, i)] for i in range(256)] res.extend(dec) res = sorted(res) for x in res[-20:]: print(x)
def attackHeader(data): expectedText = HEADER[16:32] injected = b';admin=true;dat=' b1 = data[0:16] b2 = data[16:32] newB1 = crypto.xor(expectedText, injected) newB1 = crypto.xor(newB1, b1) return newB1 + data[16:]
def main(): res = [] with open('4.txt', 'r') as f: for line in f.readlines(): enc = tools.fromHex(line.strip()) dec = [[ scoring.compute(crypto.xor(enc, i)), i, crypto.xor(enc, i) ] for i in range(256)] res.extend(dec) res = sorted(res) for x in res[-20:]: print(x)
def test_guess_key_length(self): original_plain_text = "1111111111111111" original_key = "2222" ciphertext = crypto.xor(crypto.h2ba("1111111111111111"),crypto.h2ba("2122")) xor_decrypter = XorDecrypter(ciphertext, 2, 40) # we dont use the constructor params for this test output = 2 self.assertEqual(xor_decrypter.guess_key_length(), output) ciphertext2 = crypto.xor(crypto.h2ba("1111111111111111"),crypto.h2ba("21222324")) xor_decrypter2 = XorDecrypter(ciphertext, 2, 40) # we dont use the constructor params for this test output = 4 self.assertEqual(xor_decrypter.guess_key_length(), output)
def attackTail(data): # modify second-last block POS=len(data) - 2*16 print(data) expectedText = tools.addPadding(b'con') #expectedText = tools.addPadding(b'0of%20bacon') injected = tools.addPadding(b';admin=true;') b1 = data[POS:POS+16] newB1 = crypto.xor(expectedText, injected) newB1 = crypto.xor(newB1, b1) return data[:POS] + newB1 + data[POS+16:]
def decrypt(enc, blocks, hints=[]): key = [] for (idx, block) in enumerate(blocks): res = [[ scoring.compute(crypto.xor(block, i)), i, crypto.xor(block, i) ] for i in range(256)] res = sorted(res) # for x in res[:-1]: tools.d(x) print(res[-5:]) key.append(res[-1][1]) print(['key', key]) dec = crypto.xor(enc, key) #[:80] print(dec) return (scoring.compute(dec), dec, key)
def _unprotect(self, string): """ Base64 decode and XOR the given `string` with the next salsa. Returns an unprotected string. """ tmp = base64.b64decode(string) return unicode(str(xor(tmp, self._get_salsa(len(tmp)))))
def _protect(self, string): """ XORs the given `string` with the next salsa and base64 encodes it. Returns a protected string. """ tmp = str(xor(string, self._get_salsa(len(string)))) return base64.b64encode(tmp)
def bit_flipper_3(data_in): for bit_pos in reversed(range((len(data_in) * 8) - 2)): msg_hex_len = len(data_in) * 2 for pre_mask in [0b1, 0b101, 0b011, 0b111]: mask2 = to_bytes(pre_mask << bit_pos, msg_hex_len) data = xor(mask2, data_in) yield data
def _unprotect(self, string): """ Base64 decode and XOR the given `string` with the next salsa. Returns an unprotected string. """ tmp = base64.b64decode(string) return str(xor(tmp, self._get_salsa(len(tmp))))
def test_decrypt_repeating_xor(self): bytes1 = crypto.h2ba("1c0111001f010100061a024b53535009181c") xor_decrypter = XorDecrypter(bytes1, 1, 40) # we dont use the constructor params for this test output = crypto.h2ba("1d0010011e000001071b034a52525108191d") self.assertEqual(xor_decrypter.decrypt_repeating_xor(), output) original_plain_text = "1111111111111111" original_key = "2222" ciphertext = crypto.xor(crypto.h2ba("1111111111111111"),crypto.h2ba("2122")) xor_decrypter2 = XorDecrypter(ciphertext, 1, 40) # we dont use the constructor params for this test self.assertEqual(xor_decrypter2.decrypt_repeating_xor(), original_plain_text)
def main(): keyLength = len(KEY) for S in STR: rawInput = bytes(S, 'utf-8') rawKey = bytes(KEY, 'utf-8') xorEnc = crypto.xor(rawInput, rawKey) print("In: " + tools.toHex(rawInput, blockSize=keyLength)) print("Out: " + tools.toHex(xorEnc,0,False, blockSize=keyLength)) exp = tools.fromHex("0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f") print("Exp: " + tools.toHex(exp, blockSize=keyLength,upperCase=False)) print("Key: " + tools.toHex(rawKey*25, blockSize=keyLength))
def main(): if len(sys.argv) != 3: print("Usage: %s [key] [inputtext]" % sys.argv[0]) exit(1) (key, data) = (sys.argv[1], sys.argv[2]) rawInput = bytes(data, 'utf-8') rawKey = bytes(key, 'utf-8') xorEnc = crypto.xor(rawInput, rawKey) print("In: " + tools.toHex(rawInput)) print("Key: " + tools.toHex(rawKey*((len(data)+len(key)-1)//len(key)))) print("Out: " + tools.toHex(xorEnc,0,False)) print("B64: " + tools.toB64(xorEnc))
def main(): if len(sys.argv) != 3: print("Usage: %s [key] [inputtext]" % sys.argv[0]) exit(1) (key, data) = (sys.argv[1], sys.argv[2]) rawInput = bytes(data, 'utf-8') rawKey = bytes(key, 'utf-8') xorEnc = crypto.xor(rawInput, rawKey) print("In: " + tools.toHex(rawInput)) print("Key: " + tools.toHex(rawKey * ((len(data) + len(key) - 1) // len(key)))) print("Out: " + tools.toHex(xorEnc, 0, False)) print("B64: " + tools.toB64(xorEnc))
def main(): keyLength = len(KEY) for S in STR: rawInput = bytes(S, 'utf-8') rawKey = bytes(KEY, 'utf-8') xorEnc = crypto.xor(rawInput, rawKey) print("In: " + tools.toHex(rawInput, blockSize=keyLength)) print("Out: " + tools.toHex(xorEnc, 0, False, blockSize=keyLength)) exp = tools.fromHex( "0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f" ) print("Exp: " + tools.toHex(exp, blockSize=keyLength, upperCase=False)) print("Key: " + tools.toHex(rawKey * 25, blockSize=keyLength))
def main(): with open(F,'r') as f: b64 = f.read() enc = tools.fromB64(b64) encBlocks = tools.split(enc, 16, False) iv = IV result = b'' cipher=AES.new(KEY, AES.MODE_ECB) for block in encBlocks: decBlock = cipher.decrypt(block) dec = crypto.xor(decBlock, iv) iv = block result += dec print(tools.toStr(tools.stripPadding(result)))
def main(): with open(F, 'r') as f: b64 = f.read() enc = tools.fromB64(b64) encBlocks = tools.split(enc, 16, False) iv = IV result = b'' cipher = AES.new(KEY, AES.MODE_ECB) for block in encBlocks: decBlock = cipher.decrypt(block) dec = crypto.xor(decBlock, iv) iv = block result += dec print(tools.toStr(tools.stripPadding(result)))
import crypto import binascii infile = open("s1c4.txt") for line in infile: line = line.strip() if len(line) != 60: continue for byte in range(0, 256): xor_result = crypto.xor(bytearray(binascii.a2b_hex(line)), bytearray([byte])) score = crypto.score_plaintext(xor_result) if score > 1: print(line + " bytekey:" + str(byte) + " score:" + str(score)) print(bytes(xor_result).decode())
import crypto import sys import binascii in_string = sys.argv[1] for byte in range(0,256): xor_result = crypto.xor(bytearray(binascii.a2b_hex(in_string)), bytearray([byte])) score = crypto.score_plaintext(xor_result) if score > 1: print(in_string + " bytekey:" + str(byte) + " score:" + str(score)) print(bytes(xor_result).decode())
def extractKey(message): blocks = tools.split(message, 16) iv = crypto.xor(blocks[0], blocks[2]) return iv
def test_get_single_byte_xor_histogram_score(self): block1 = crypto.xor(crypto.h2ba("Hello pretty city!"),crypto.h2ba("12")) block2 = crypto.xor(crypto.h2ba("Purple dogs are really neat, and I like them a bunch!"),crypto.h2ba("22")) block3 = crypto.xor(crypto.h2ba("Frogs jump really high sometimes and then fall like marshmallows onto big fluffy pillows."),crypto.h2ba("23"))
import crypto import binascii import base64 in_string = "Burning 'em, if you ain't quick and nimble\nI go crazy when I hear a cymbal" key = "ICE" xor_result = crypto.xor(bytearray(in_string.encode()), bytearray(key.encode())) print(base64.b16encode(xor_result))
def test_xor(self): self.assertEqual("\x00", crypto.xor("\x00", "\x00")) self.assertEqual("\x01", crypto.xor("\x00", "\x01")) self.assertEqual("\x00", crypto.xor("\x01", "\x01")) self.assertEqual("\x04\n\x05", crypto.xor("\x0f\x00\x00", "\x0b\n\x05"))
def bit_flipper_1(data_in): for bit_pos in reversed(range((len(data_in) * 8) - 1)): msg_hex_len = len(data_in) * 2 mask2 = to_bytes(1 << bit_pos, msg_hex_len) data = xor(mask2, data_in) yield data
#!/usr/bin/env python import crypto INPUT1 = "1c0111001f010100061a024b53535009181c" INPUT2 = "686974207468652062756c6c277320657965" OUTPUT = "746865206b696420646f6e277420706c6179" xored = crypto.words_to_hex(crypto.xor(crypto.hex_to_words(INPUT1), crypto.hex_to_words(INPUT2))) print xored print OUTPUT print OUTPUT == xored
def xor_with_key(string, key): char_string = repeat_to_length(key, len(string)) char_bits = crypto.ascii_to_bits(char_string) string_bits = crypto.ascii_to_bits(string) xored = crypto.xor(string_bits, char_bits) return crypto.bits_to_hex(xored)
#!/usr/bin/env python import crypto INPUT1 = "1c0111001f010100061a024b53535009181c" INPUT2 = "686974207468652062756c6c277320657965" OUTPUT = "746865206b696420646f6e277420706c6179" xored = crypto.words_to_hex( crypto.xor(crypto.hex_to_words(INPUT1), crypto.hex_to_words(INPUT2))) print xored print OUTPUT print OUTPUT == xored
import crypto ## Problem 2 s1 = '1c0111001f010100061a024b53535009181c'.decode('hex') s2 = '686974207468652062756c6c277320657965'.decode('hex') print crypto.xor(s1, s2).encode('hex') ## Problem 3
def test_xor_different_length(self): bytes1 = crypto.h2ba("1c0111001f010100061a024b53535009181c") bytes2 = crypto.h2ba("01") output = crypto.h2ba("1d0010011e000001071b034a52525108191d") self.assertEqual(crypto.xor(bytes1, bytes2), output)
def test_xor_samelength(self): bytes1 = crypto.h2ba("1c0111001f010100061a024b53535009181c") bytes2 = crypto.h2ba("686974207468652062756c6c277320657965") output = crypto.h2ba("746865206b696420646f6e277420706c6179") self.assertEqual(crypto.xor(bytes1, bytes2), output)
import crypto import sys print(crypto.xor(sys.argv[1], sys.argv[2]))