Example #1
0
 def test_getStrongPrime(self):
     """Util.number.getStrongPrime"""
     self.assertRaises(ValueError, number.getStrongPrime, 256)
     self.assertRaises(ValueError, number.getStrongPrime, 513)
     bits = 512
     x = number.getStrongPrime(bits)
     self.assertNotEqual(x % 2, 0)
     self.assertEqual(x > (1 << bits-1)-1, 1)
     self.assertEqual(x < (1 << bits), 1)
     e = 2**16+1
     x = number.getStrongPrime(bits, e)
     self.assertEqual(number.GCD(x-1, e), 1)
     self.assertNotEqual(x % 2, 0)
     self.assertEqual(x > (1 << bits-1)-1, 1)
     self.assertEqual(x < (1 << bits), 1)
     e = 2**16+2
     x = number.getStrongPrime(bits, e)
     self.assertEqual(number.GCD((x-1)>>1, e), 1)
     self.assertNotEqual(x % 2, 0)
     self.assertEqual(x > (1 << bits-1)-1, 1)
     self.assertEqual(x < (1 << bits), 1)
    def keygen(security_param = 2048):
        p = number.getPrime(security_param // 2)
        q = number.getPrime(security_param // 2)
        n = p*q

        phi = (p - 1)*(q - 1)
        e = randint(0, phi)
        while number.GCD(e, phi) != 1:
            e = randint(0, phi)

        key_coder = RsaKeyCoder()
        public_key = key_coder.encode_public_key(e, n)

        key_encryptor = KeyEncryptor.get_instance()
        d = number.inverse(e, phi)
        private_key = key_encryptor.encrypt(key_coder.encode_private_key(d, n))

        return public_key, private_key