def __init__(self): self.e = 3 self.p = getStrongPrime(512, e=self.e) self.q = getStrongPrime(512, e=self.e) self.n = self.p * self.q self.phi = (self.p - 1) * (self.q - 1) self.d = inverse(self.e, self.phi) ## generate secret seed self.seed = pow(1 << 1023, self.e, self.n) self.seed = long_to_bytes(self.seed)
def main(): p, q, d = int(getStrongPrime(512)), int(getStrongPrime(512)), 0 n = p * q t = (p - 1) * (q - 1) e = 3 print("p = ", p) print("q = ", q) print("e = ", e) print("t = ", t) print("n = ", n) enc = enc(42, n, e) print(enc) d = egcd(e, t) print("d = ", d) print(dec(enc, n, d))
def main(): p, q, d = int(getStrongPrime(512)), int(getStrongPrime(512)), 0 n = p * q et = (p - 1) * (q - 1) e = 3 print("p:", p) print("q:", q) print("n:", n) print("et:", et) print("e:", e) enc = encrypt(42, n, e) print(enc) d = extended_euclid(e, et) print("d:", d) print(decrypt(enc, n, d))
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 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 > (1L << bits-1)-1, 1) self.assertEqual(x < (1L << 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 > (1L << bits-1)-1, 1) self.assertEqual(x < (1L << 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 > (1L << bits-1)-1, 1) self.assertEqual(x < (1L << bits), 1)