decrypt = rsa.decrypt_string(ciphertext, R) print print "Bob gets this message:", decrypt #### Eve # Calculate products of the moduli (pubkeys) EXCEPT pubkey number i. ms = [None] * k for i in range(k): x = copy.copy(n) del x[i] ms[i] = reduce(lambda a, b: a * b, x) # Work thru Chinese Remainder Theorem result = 0 for i in range(k): result += c[i] * ms[i] * rsa.invmod(ms[i], n[i]) result = result % reduce(lambda a, b: a * b, n) # Get final text overheard = rsa.i2s(cuberoot(result)) print "Eve hears this message:", overheard #### tests #### assert message == decrypt assert message == overheard assert decrypt == overheard warn("Passed assertions:", __file__)
print "Along with signature..." print signature print "Does it verify?" print verify(signature, message, U) print #### Forging msg_to_forge = "hi mom" hash_mom = sha1(msg_to_forge).digest() block_mom = ("\x00\x01\xff\xff\x00ASN.1" + chr(len(hash_mom)) + hash_mom) bytes_to_add = (bits / 8) - len(block_mom) block_mom += "\x00" * bytes_to_add block_mom_cube = "\x00" + rsa.i2s(cuberoot(rsa.s2i(block_mom)) ** 3) forged_sig = cuberoot(rsa.s2i(block_mom_cube)) #### Check the sig print "A poor fool received message:", msg_to_forge print "Along with signature..." print forged_sig print "Does it verify?" result = verify(forged_sig, msg_to_forge, U) print result print #### tests #### assert result assert unpad(pkcs_1_5("Hello", 1024)) == "Hello"
decrypt = rsa.decrypt_string(ciphertext, R) print print "Bob gets this message:", decrypt #### Eve # Calculate products of the moduli (pubkeys) EXCEPT pubkey number i. ms = [None]*k for i in range(k): x = copy.copy(n) del x[i] ms[i] = reduce(lambda a, b: a*b, x) # Work thru Chinese Remainder Theorem result = 0 for i in range(k): result += c[i] * ms[i] * rsa.invmod(ms[i], n[i]) result = result % reduce(lambda a, b: a*b, n) # Get final text overheard = rsa.i2s(cuberoot(result)) print "Eve hears this message:", overheard #### tests #### assert message == decrypt assert message == overheard assert decrypt == overheard warn("Passed assertions:", __file__)