def crack(): message = [ 84620, 66174, 66174, 5926, 9175, 87925, 54744, 54744, 65916, 79243, 39613, 9932, 70186, 85020, 70186, 5926, 65916, 72060, 70186, 21706, 39613, 11245, 34694, 13934, 54744, 9932, 70186, 85020, 70186, 54744, 81444, 32170, 53121, 81327, 82327, 92023, 34694, 54896, 5926, 66174, 11245, 9175, 54896, 9175, 66174, 65916, 43579, 64029, 34496, 53121, 66174, 66174, 21706, 92023, 85020, 9175, 81327, 21706, 13934, 21706, 70186, 79243, 9175, 66174, 81327, 5926, 74450, 21706, 70186, 79243, 81327, 81444, 32170, 53121 ] public_key = (29815, 100127) pk, public_key = rsa.generate_keypair() message = rsa.encrypt(public_key, "vi skulle egentlig ha en pong greie") e, n = public_key primes = rsa.PrimeGen(1000) for p in primes: if n % p == 0: q = n // p break phi = (p - 1) * (q - 1) d = rsa.multiplicative_inverse(e, phi) attempt = rsa.decrypt((d, n), message)
def test_multiplicative_inverse(self): self.assertEqual(23, rsa.multiplicative_inverse(7, 40)) self.assertEqual(1969, rsa.multiplicative_inverse(42, 2017)) self.assertEqual(0, rsa.multiplicative_inverse(40, 1)) self.assertEqual(169, rsa.multiplicative_inverse(121, 288)) self.assertEqual(734969, rsa.multiplicative_inverse(142169, 1694640)) self.assertEqual(1804547, rsa.multiplicative_inverse(9678731, 11181456))
def decryption(public_key, ciphered_text): primes = rsa.PrimeGen(100) n = [] for i in itertools.product(primes, repeat=2): if (i[0] * i[1] == public_key[1]): n.append(i) try: for x in range(0, len(n)): phi = (n[x][0] - 1) * (n[x][1] - 1) invers = rsa.multiplicative_inverse(public_key[0], phi) message = rsa.decrypt((invers, public_key[1]), ciphered_text) if (message.startswith("h")): d = invers return [message, [d, public_key[1]]] except ValueError: pass
def decryptPrivateKey(): primes = rsa.PrimeGen( 100) # this needs to be adjusted if higher encryption possibleN = [] for x in primes: for y in primes: if (x * y == public_key[1]): # if x*y equals N, add to list. possibleN.append([x, y]) try: for x in range(0, len(possibleN)): phi = (possibleN[x][0] - 1) * (possibleN[x][1] - 1) invers = rsa.multiplicative_inverse(public_key[0], phi) message = rsa.decrypt((invers, public_key[1]), cipher_text) if (message.startswith("h")): d = invers #possible 'd' out of (d, n) private key. print("Decrypted message:\n" + message) print("Private key is: ({},{})\n ".format(d, public_key[1])) except ValueError: pass
def decrypt(): try: primeList = rsa.PrimeGen(100) possibleKeys = [] for p in primeList: for q in primeList: n = p * q if n == publicKey[1]: myTuple = (p, q) possibleKeys.append(myTuple) for x in possibleKeys: p, q = x phi = (p - 1) * (q - 1) d = rsa.multiplicative_inverse(publicKey[0], phi) possibleKey = (d, p * q) decryption = rsa.decrypt(possibleKey, ciphertext) if decryption.startswith("h"): print("Private key (d, n):") print(f"({d}, {p*q})") print(decryption) break except ValueError as e: print(e)
def test_multiplicative_inverse(self): self.assertEqual(23, rsa.multiplicative_inverse(7, 40)) self.assertEqual(1969, rsa.multiplicative_inverse(42, 2017)) self.assertEqual(0, rsa.multiplicative_inverse(40, 1))
def test_multiplicative_inverse(self): tests = [(120, 23, 47),(160, 7, 23),(40, 3, 27),(48, 5, 29)] for test in tests: phin, e, ans = test self.assertEqual(rsa.multiplicative_inverse(phin, e), ans)
def generatePossiblePublicKey(publicKey, possiblePhi): possibleD = rsa.multiplicative_inverse(publicKey[0], possiblePhi) return (possibleD, publicKey[1])
def attack_rsa(n, e, message): p = pollards_rho(n) q = n / p phi_n = (p - 1) * (q - 1) d = multiplicative_inverse(phi_n, e) return decrypt(message, d, n)
def test_multiplicative_inverse(self): tests = [(120, 23, 47), (160, 7, 23), (40, 3, 27), (48, 5, 29)] for test in tests: phin, e, ans = test self.assertEqual(rsa.multiplicative_inverse(phin, e), ans)
import random import string import unittest import rsa class rsaTestCase(unittest.TestCase): def test_gcd(self): cases = [(12, 15, 3), (3, 7, 1)] for i, (a, b, c) in enumerate(cases): with self.subTest(case=i, a=a, c=c): self.assertEqual(c, rsa.gcd(a, b=b)) def test_multiplicative_inverse(self): cases = [(7, 40, 23)] for i, (e, phi, res) in enumerate(cases): with self.subTest(case=i, e=e, res=res): self.assertEqual(res, rsa.multiplicative_inverse(e, phi=phi)) if __name__ == '__main__': unittest.main()
def test_multiplicative_inverse(self): self.assertEqual(rsa.multiplicative_inverse(7, 40), 23)