def problem4(cipher1_b64, cipher2_b64, N1, N2): cipher1_hex = conversions.b64_to_hex(cipher1_b64) cipher2_hex = conversions.b64_to_hex(cipher2_b64) cipher1_b10 = int(cipher1_hex,16) cipher2_b10 = int(cipher2_hex,16) common_factor = euclid_etc.euclid(N1, N2) if (common_factor ==1): #Returning an error as requested if there is no common factor return "error: there is no common factor" else: p1 = N1 / common_factor #N1 = common_factor * p1 N1,N2 are the 2 different moduli p2 = N2 / common_factor #N2 = common_factor * p2 M1 = (p1 - 1) * (common_factor - 1) #definition of M as in the notes M2 = (p2 - 1) * (common_factor - 1) d1 = euclid_etc.extended_euclid(65537, M1) d2 = euclid_etc.extended_euclid(65537, M2) d1 = d1[1][0] #Need to access the desired part bc we get a tuple and then a tuple within it in the second (technically [1]) so i need to access the actual wanted part d2 = d2[1][0] + M2 #Added M2 because I was getting an error bc originally it was negative and we know that a = b(mod n) -> a+n = b(mod n) plaintext1_b10 = euclid_etc.repeated_squaring(cipher1_b10, d1, N1) #message = c^d(mod n), equation from class and from notes, plug and chug plaintext2_b10 = euclid_etc.repeated_squaring(cipher2_b10, d2, N2) plaintext1_hex = hex(plaintext1_b10) plaintext2_hex = hex(plaintext2_b10) plaintext1_hex = hex_string_to_hex(plaintext1_hex) plaintext2_hex = hex_string_to_hex(plaintext2_hex) plaintext1 = conversions.hex_to_as(plaintext1_hex) plaintext2 = conversions.hex_to_as(plaintext2_hex) return (plaintext1, plaintext2) #I returned a tuple of the plaintexts bc I decrypted both of them
def problem1(cipher_b64, N): cipher_hex = conversions.b64_to_hex(cipher_b64) cipher_b10 = int(cipher_hex,16) plaintext_b10 = kthroot(cipher_b10,3) plaintext_hex_string = hex(plaintext_b10) plaintext_hex = hex_string_to_hex(plaintext_hex_string) plaintext_as = conversions.hex_to_as(plaintext_hex) return plaintext_as
def reduce_git(hashb64, column): hashhex = conversions.b64_to_hex(hashb64) hashV = hashhex[:10] results = [] chars = 'abcdefghijklmnopqrstuvwxyz' # Cast hash from str to int then decompose into bytes byteArray = getBytes(hashV) for i in range(4): index = byteArray[(i + column) % len(byteArray)] newChar = chars[index % len(chars)] results.append(newChar) return "".join(results)
def problem2(cipher1_b64, cipher2_b64, cipher3_b64, N1, N2, N3): a1_hex = conversions.b64_to_hex(cipher1_b64) a2_hex = conversions.b64_to_hex(cipher2_b64) a3_hex = conversions.b64_to_hex(cipher3_b64) a1_b10 = int(a1_hex,16) a2_b10 = int(a2_hex,16) a3_b10 = int(a3_hex,16) N2N3inv = euclid_etc.extended_euclid(N2*N3,N1) N2N3inv = N2N3inv[1][0] #This is all just plug and chug N1N3inv = euclid_etc.extended_euclid(N1*N3,N2) #into the CRT equation in the N1N3inv = N1N3inv[1][0] #Lecture notes #7. x = ... see notes for full equation N1N2inv = euclid_etc.extended_euclid(N1*N2,N3) N1N2inv = N1N2inv[1][0] parta = a1_b10 * ((N2N3inv) % N1) * (N3*N2) partb = a2_b10 * ((N1N3inv) % N2) * (N1*N3) partc = a3_b10 * ((N1N2inv) % N3) * (N1*N2) x = (parta + partb + partc) % (N1*N2*N3) cubeRootx = kthroot(x,3) x_hex = hex(cubeRootx) x_hex = hex_string_to_hex(x_hex) plain = conversions.hex_to_as(x_hex) return plain
def reduce_git1(hashb64, column): hashhex = conversions.b64_to_hex(hashb64) hashV = hashhex z = b26_to_num('z') results = [] chars = 'abcdefghijklmnopqrstuvwxyz' # Cast hash from str to int then decompose into bytes byteArray = getBytes(hashV) for i in range(6): index = byteArray[(i + column) % len(byteArray)] newChar = chars[index % len(chars)] results.append(newChar) "".join(results) results[3] = num_to_b26((b26_to_num(results[3]) + column) % z, 1) pword = "".join(results) return pword
def reduce_git1(hashb64, column): hashhex = conversions.b64_to_hex(hashb64) hashV = hashhex results = [] chars = 'abcdefghijklmnopqrstuvwxyz' # Cast hash from str to int then decompose into bytes byteArray = getBytes(hashV) for i in range(4): index = byteArray[(i + column) % len(byteArray)] newChar = chars[index % len(chars)] results.append(newChar) letter4 = results[3] letter4num = ((b26_to_num(letter4)) + column) % 26 newletter4 = num_to_b26(letter4num, 1) # if column > 100: # pword = "".join(results) return "".join(results)
def b64_to_bits(b64text): text = conversions.b64_to_hex(b64text) bits = int(text, 16) return bits