Beispiel #1
0
    def decrypt(self,
                g=None,
                modulus=None,
                private_key=None,
                cipher_text_1=None,
                cipher_text_2=None):
        if private_key is not None:
            self.set_private_key(private_key)
        if g is not None:
            self.set_g(g)
        if modulus is not None:
            self.set_modulus(modulus)
        if cipher_text_1 is not None:
            self.set_cipher_text_1(cipher_text_1)
        if cipher_text_2 is not None:
            self.set_cipher_text_2(cipher_text_2)

        if None not in [
                self.private_key, self.g, self.modulus, self.cipher_text_1,
                self.cipher_text_2
        ]:
            x = mod_inv(
                fast_power(self.cipher_text_1, self.private_key, self.modulus),
                self.modulus)
            self.message = (x * self.cipher_text_2) % self.modulus
            return self.message
        else:
            return None
Beispiel #2
0
def bsgs(
    g,
    h,
    p,
    smoothness=10000
):  # Solving x given g,h,p for the equation g^x (mod p) = h, so logg(h) (mod p) = x
    N = order(g, p, smoothness)
    n = int(sqrt(N)) + 1
    e = 1
    u = mod_inv(fast_power(g, n, p), p)
    uk = u
    gi = g
    hui = (h * uk) % p
    list_one = {gi: 1}
    list_two = {hui: 1}
    # Create the two list
    for i in range(2, n + 1):
        gi = (gi * g) % p
        uk = (uk * u) % p
        hui = (h * uk) % p
        list_one.update({gi: i})
        list_two.update({hui: i})
    # Now Check for a collision between the list
    for huk in list_two.keys():
        for gk in list_one.keys():
            if huk == gk:
                x = (list_one.get(gk) + (list_two.get(huk) * n)) % N
                xs = []
                for i in range(0, int(((p - 1) / N) + 1)):
                    xs.append(x + N * i)
                return xs
Beispiel #3
0
 def slope(self, P, Q):  # Where P and Q are tuples
     if P == Q:
         inv_2y_p = mod_inv((2 * P[1]) % self.modulus, self.modulus)
         if isinstance(inv_2y_p, tuple):
             d = inv_2y_p[1]
             return [None, None, d]
         slope = ((3 * P[0] * P[0] + self.A) * inv_2y_p) % self.modulus
         return slope
     else:
         y_diff = (P[1] - Q[1]) % self.modulus
         x_diff = (P[0] - Q[0]) % self.modulus
         inv_x_diff = mod_inv(x_diff, self.modulus)
         if isinstance(inv_x_diff, tuple):
             d = inv_x_diff[1]
             return [None, None, d]
         slope = (y_diff * inv_x_diff) % self.modulus
         return slope
Beispiel #4
0
 def decrypt(self, c, p = None, q = None, e = None):
     if q is not None and p is not None:
         self.q = q
         self.p = p
         self. N = self.p * self.q
     if e is not None:
         self.e = e
     self.c = c
     if None in [self.N, self.p, self.q, self.e, self.c]:
         raise ValueError("c, e, p, q, or N is None")
     g = eea(self.p - 1, self.q-1).get("gcd")
     self.d = int(mod_inv(self.e % ((self.p-1)*(self.q-1)/g), (self.p-1)*(self.q-1)/g))
     self.m = fast_power(self.c, self.d, self.N)
     return self.m
Beispiel #5
0
 def test_mod_inv_4(self):
     a = 173920
     m = 100207
     with self.assertRaises(ValueError):
         self.assertEqual(mod_inv(a, m), b)
Beispiel #6
0
 def test_mod_inv_3(self):
     a = 632
     m = 999749
     b = 93331
     self.assertEqual(mod_inv(a, m), b)
Beispiel #7
0
 def test_mod_inv_2(self):
     a = 3
     m = 7
     b = 5
     self.assertEqual(mod_inv(a, m), b)
Beispiel #8
0
 def test_mod_inv_1(self):
     print("\n\nRunning test for src module: mod_inv")
     a = 5
     m = 7
     b = 3
     self.assertEqual(mod_inv(a, m), b)