예제 #1
0
 def test_BabyStepGiantStep(self):
     for i in range(SIZE):
         m = cp.prime_search(8, True)
         b = cp.primitive_root_search(m)
         a = cp.blum_blum_shub(6)
         result = cp.baby_step_giant_step(a, b, m)
         answer = cp.fast_exp(b, result, m)
         self.assertEqual(a, answer)
예제 #2
0
 def __init__(self, size=10, mod=None, base=None, key=None):
     self.mod = cp.prime_search(size, True) if not mod else mod
     # if not cp.miller_rabin(self.mod, 30):
     #     raise Exception('Modulus is not prime. Cannot safely encrypt.')
     #     return -1
     self.base = cp.primitive_root_search(self.mod) if not base else base
     self.__key_A = cp.blum_blum_shub(20) % self.mod
     self.key_pub = cp.fast_exp(self.base, self.__key_A,
                                self.mod) if not key else key
예제 #3
0
 def test_ElGamal(self):
     for i in range(SIZE):
         g = ciphers.ElGamal()
         message = cp.blum_blum_shub(6)
         c1, c2 = g.encrypt(message)
         decrypted = g.decrypt(c1, c2)
         cracked = g.crack(c1, c2)
         self.assertEqual(message, decrypted)
         self.assertEqual(message, cracked)
예제 #4
0
 def __init__(self, size=10, n=None, e=None):
     self.__p = cp.prime_search(size, True)
     self.__q = cp.prime_search(size, True)
     if not (cp.miller_rabin(self.__p) or cp.miller_rabin(self.q)):
         raise Exception(
             'P or Q is not prime. Cannot safely encrypt. Please try again.'
         )
         return -1
     self.__phi = ((self.__p - 1) * (self.__q - 1))
     if not n:
         self.n = self.__p * self.__q
     else:
         self.n = n
     if not e:
         e = cp.blum_blum_shub(size)
         while cp.gcd(self.__phi, e) != 1:
             e = cp.blum_blum_shub(size)
         self.e = e
     else:
         self.e = e
예제 #5
0
    def encrypt(self, message, mod=None, base=None, key=None):
        """
        function to encrypt a message, given the mututally agreed modulus,
        the mutual base, and the public key transmitted by alice during cipher 
        initialization. returns another public key (Bob's), c1, and the encrypted
        message, c2.

        Accepts an external key. Requires mod, base, and key. If not provided, uses
        the mod, base, and key created at initialization.
        """
        # if type(message) is str:
        #     message = cp.str_to_int(message)
        mod = self.mod if not mod else mod
        base = self.base if not base else base
        key = self.key_pub if not key else key
        if message > mod:
            raise Exception(
                'Message larger than modular group. Cannot safely encrypt. Please provide larger size at class initialization.'
            )
            return -1, -1
        key_B = cp.blum_blum_shub(20) % mod
        c1 = cp.fast_exp(base, key_B, mod)
        c2 = (cp.fast_exp(key, key_B, mod) * message) % mod
        return c1, c2
예제 #6
0
 def test_BlumBlumShub(self):
     for i in range(SIZE):
         s = random.randint(1,25)
         n = len(str(cp.blum_blum_shub(s)))
         self.assertEqual(s,n)
예제 #7
0
 def test_RSA(self):
     for i in range(SIZE):
         r = ciphers.RSA(7)
         message = cp.blum_blum_shub(6)
         self.assertEqual(message, r.decrypt(r.encrypt(message)))
         self.assertEqual(message, r.crack(r.encrypt(message)))