def guess(list_encrypted): string_index= int(raw_input("Which string do you want to guess from?")) string= list_encrypted[string_index] start_offset= int(raw_input("Enter start offset of guess:")) end_offset = int(raw_input("Enter end offset of guess:")) enc= string[start_offset:end_offset] guess_len= end_offset-start_offset dec = raw_input("Guessed plain text:") #Check so you enter the right number of characters while you actually guess stuff and not head scratch where you're going wrong :) if len(dec) != guess_len: print "Guess length incorrect.Exiting" print sys.exit(0) #CT xor PT = key based on stream cipher property. Get the key for one string and re-use the key to try and decrypt the rest of the encrypted strings. key= block.xor(enc, dec) t4= [] for count,s1 in enumerate(list_encrypted): t3= s1[start_offset:end_offset] t4.append(block.xor(key[0:len(t3)],t3)) return t4
def hmac(self, p1): key = 'teddy' blocksize = 64 if len(key) > blocksize: key = sha1(key).hexdigest() if len(key) < blocksize: diff = blocksize - len(key) key = key + '\x00' * diff o_key_pad = block.xor('\x5c' * blocksize, key) i_key_pad = block.xor('\x36' * blocksize, key) m1 = i_key_pad + p1 h_m1 = sha1(m1).hexdigest() m2 = o_key_pad + h_m1 h_m2 = sha1(m2).hexdigest() return h_m2
def get_key(decrypted): t1 = [decrypted[x:x + 16] for x in range(0, len(decrypted), 16)] s1 = t1[0] s2 = t1[2] k1 = [] for i in range(0, 16): k1.append(block.xor(s1[i], s2[i])) key = ''.join(k1) #This is for sanity checking only. Did we recover the key correctly? key_orig = '0x71e6efcfb44e362b6e14f7abbecf5503' key_in_binary_orig = binascii.a2b_hex(key_orig[2:]) if key == key_in_binary_orig: return key else: return None
#Importing common crypto module import block import common def ctr_counter_fixed_nonce_encrypt(decoded_input_str, key, nonce, counter): t1 = nonce + str(counter).zfill(8) t2 = block.openssl_ecb_encrypt(t1, key) keystream.append(t2) return keystream if __name__ == "__main__": input_str = 'L77na/nrFsKvynd6HzOoG7GHTLXsTVu9qvY/2syLXzhPweyyMTJULu/6/kXX0KSvoOLSFQ==' #input_str= 'MDAwMDAwTm93IHRoYXQgdGhlIHBhcnR5IGlzIGp1bXBpbmc=' decoded_input_str = input_str.decode("base64") blocklen = 16 counter = len(decoded_input_str) / blocklen + 1 key = '71e6efcfb44e362b6e14f7abbecf5503' nonce = '0' * 8 keystream = [] for i in range(0, counter, 1): t3 = ctr_counter_fixed_nonce_encrypt(decoded_input_str, key, nonce, i) keystream = ''.join(t3) encrypted = block.xor(decoded_input_str, keystream) decrypted = block.xor(encrypted, keystream) print decrypted