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 problem7(p, g, gx_mod_p, firstComponent, first_secondComponent, m, second_secondComponent): m_hex = conversions.as_to_hex(m) m_b10 = int(m_hex,16) gxy = euclid_etc.extended_euclid(m_b10,p) gxy = gxy[1][0] gxy = (first_secondComponent * gxy) % p gxy_inverse = euclid_etc.extended_euclid(gxy, p) gxy_inverse = gxy_inverse[1][0] plaintext_b10 = (gxy_inverse * second_secondComponent) % p 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 problem3(ct, d, modulus): # modulus = 118655596447903224963869613053944974689306948978385022351313547236325408711597515968013120482999812623684614237188826586513936280546323473924322265897469916282911780567243884859836121621708694140679468147474220271417145483116948895892297437839699778114403528270801117190611396069302409336194794071895031049811 # d = 23731119289580644992773922610788994937861389795677004470262709447265081742319503193602624096599962524736922847437765317302787256109264694784864453179493978848181776281125630457673065339633910037899644296946114786385008468034547208885433370715974640426127989454209067113013826578800440508863151210816724921125 # ct = 'LbJFbZa7eoMEQE8uPICTjzxFqvtM2qLPf6OSNOR6bgIJ8IlSWMMIOL4s7qevd77GRtLTz6EgC38EVjbp+ZHKJ5inX582MlxjD6WYcqGEYOnvCFGBpeFJnkAvhFIWxNyIx1tk1xxQgTHU8wMHjq/rMCl6USuZcSH4L1bzUMmgfZE=' a = random.randint(1, modulus-1) e = 5 k = e * d - 1 while pow(a, k, modulus) == 1 and k % 2 == 0: k = k / 2 r = pow(a, k, modulus) if pow(r, 2, modulus) == 1: xy = euclid_etc.extended_euclid(r-1, modulus) p = xy[0] q = modulus/p m = (p-1)*(q-1) d_new = modinv(3, m) decrypt_me = kthroot(3, d_new) ct_as_bits = b64_to_bits(ct) convert_me = pow(ct_as_bits, d_new, modulus) h = hex(convert_me) h = h[2:-1] answer = conversions.hex_to_as(h) print answer return answer else: problem3(ct, d, modulus)
def problem4(ct1_b64, ct2_b64, N1, N2): # def problem4(): # N1 = 87750187518907655534583445808737942078016029371855217057442341331127022016930461105786023716013285380470222803872626192434912740863485532564125627646878636545449869643527771922181597178447982975143657375859594541373428795038041796818858805812228886812351199020336314262507362189851970680226889619203804537151 # N2 = 59077605606399909603607705484000546044333045357566473814158491087439387780574866766800852465743470772146755309189078604396507686696592563062056700875467732286553829707195406383141965288479916793429869646143662227281782900822010619445408818002981548245734527538573941174294649831309213962935858200869524073603 # ct1_b64 = 'Ur/BIau7ZZBdwxD8P3xDJFJGMfkJDXNU5rbY7GlvlRkGae4NEMo3pMq9r8Jk2akGSj47SZ00L+eTmeMIIfis3RoG7jjBdj03p5lLtgrLwnjP0lzr31fasl5+NVZIvmnoEt56Figi54lIAXEj4ig06MHFG2KfotLYJTnwabangS4=' # ct2_b64 = 'CPEXorDgegEqM6UttzFLaccAN/t4QB1FTDS+NL3TSofQlq3Rs/BebbNn4Qj/Vo4FmTwV3P0+n+hlIhjXzOgEgdgV3BmiBE3rIBHqUc+q0FoVvWJU1+jvFpEeellYZMX8vG7O9us5JKfDAHjPaHWZSwv++BSX4rh+5O01flxzlJA=' xy = euclid_etc.extended_euclid(N1, N2) p = xy[0] q1 = N1/p q2 = N2/p m1 = (p-1)*(q1-1) m2 = (p-1)*(q2-1) d1 = modinv(65537, m1) d2 = modinv(65537, m2) ct1_as_bits = b64_to_bits(ct1_b64) ct2_as_bits = b64_to_bits(ct2_b64) pt1_as_bits = pow(ct1_as_bits, d1, N1) pt2_as_bits = pow(ct2_as_bits, d2, N2) h1 = hex(pt1_as_bits) h2 = hex(pt2_as_bits) h1 = h1[2:-1] h2 = h2[2:-1] answer1 = conversions.hex_to_as(h1) answer2 = conversions.hex_to_as(h2) print answer1 print answer2 return 'done'
def problem8(p, g, gx_mod_p, firstComponent, secondComponent, y): gxy = euclid_etc.repeated_squaring(gx_mod_p, y, p) plaintext = euclid_etc.extended_euclid(gxy, p) plaintext_b10 = plaintext[1][0] plaintext_b10 = (plaintext_b10 * secondComponent) % p 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 problem6(p, g, gx_mod_p, x, firstComponent, secondComponent): gxy = euclid_etc.repeated_squaring(firstComponent,x,p) #gxy = g^xy (mod p) plaintext = euclid_etc.extended_euclid(gxy,p) plaintext = plaintext[1][0] #Just like problem 4, need to get the desired part plaintext_b10 = (plaintext * secondComponent) % p 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 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