def test_encrypt_decrypt(self): for key in self.__data: cipher = RSA(*key) for pair in self.__data[key]: self.assertEqual(RSA.encrypt(cipher.public_key, pair[0]), pair[1]) self.assertEqual(cipher.decrypt(pair[1]), pair[0])
def test_abctf_sexyrsa(self): plain = b"ABCTF{i_h4ve_an_RSA_fetish_;)}" cipher = 293430917376708381243824815247228063605104303548720758108780880727974339086036691092136736806182713047603694090694712685069524383098129303183298249981051498714383399595430658107400768559066065231114145553134453396428041946588586604081230659780431638898871362957635105901091871385165354213544323931410599944377781013715195511539451275610913318909140275602013631077670399937733517949344579963174235423101450272762052806595645694091546721802246723616268373048438591 p = 1099610570827941329700237866432657027914359798062896153406865588143725813368448278118977438921370935678732434831141304899886705498243884638860011461262640420256594271701812607875254999146529955445651530660964259381322198377196122393 q = 1099610570827941329700237866432657027914359798062896153406865588143725813368448278118977438921370935678732434831141304899886705498243884638860011461262640420256594271701812607875254999146529955445651530660964259381322198377196122399 r = RSA(p, q) pub, priv = r.gen_keys() self.assertEqual(cipher, pub.encrypt(plain)) self.assertEqual(plain, priv.decrypt(cipher))
def test_simple(self): r = RSA(61, 53, 17) pub, priv = r.gen_keys() self.assertEqual(pub.n, 3233) self.assertEqual(priv.n, 3233) self.assertEqual(priv.d, 413) plain_true = b"H" cipher_true = 0xbb8 cipher = pub.encrypt(plain_true) self.assertEqual(cipher_true, cipher) self.assertEqual(plain_true, priv.decrypt(cipher))
def test_abctf_oldrsa(self): plain = b"ABCTF{th1s_was_h4rd_in_1980}" cipher = 0x41fcc60d2cd6e05c9883c099959de17b1305983c7c99b57b1133a3f507849bd1 n = 70736025239265239976315088690174594021646654881626421461009089480870633400973 e = 3 p = 238324208831434331628131715304428889871 q = 296805874594538235115008173244022912163 r = RSA(p, q, e) pub, priv = r.gen_keys() self.assertEqual(n, pub.n) self.assertEqual(n, priv.n) self.assertEqual(cipher, pub.encrypt(plain)) self.assertEqual(plain, priv.decrypt(cipher))
def handle_rsa(args): if args.action == "common": pk1 = PrivKey(args.n, args.e[0]) pk2 = PrivKey(args.n, args.e[1]) return common_modulus(pk1, pk2, args.c[0], args.c[1]) elif args.action == "eroot": plainmpz = gmpy2.iroot(gmpy2.mpz(args.c), args.e) return hex_to_str(plainmpz[0]) elif args.action == "hastad": pks, cs = [], [] for i in range(args.e): pks.append(PubKey(args.n[i], args.e)) cs.append(args.c[i]) return hastad(pks, cs) elif args.action == "wiener": pubk = PubKey(args.n, args.e) _, _, d = wiener(pubk) priv = PrivKey(args.n, d) return priv.decrypt(args.c) elif args.action == "crack": f = Factorizer(Algo[args.algo], args.limit) pq = f.factorize(args.n) r = RSA(pq[0], pq[1], args.e) pub, priv = r.gen_keys() return priv.decrypt(args.c) elif args.action == "decrypt": priv = PrivKey(args.n, args.d) return priv.decrypt(args.c) elif args.action == "decrypt-pq": r = RSA(args.p, args.q, args.e) pub, priv = r.gen_keys() return priv.decrypt(args.c) elif args.action == "encrypt": pub = PubKey(args.n, args.e) return pub.encrypt(args.m.encode()) elif args.action == "encrypt-pq": r = RSA(args.p, args.q, args.e) pub, priv = r.gen_keys() return pub.encrypt(args.m.encode())
def test_error(self): with self.assertRaises(RSAError): r = RSA(61, 53, 9922) r.gen_keys()
def test_public_key_not_modulus_coprime(self): with self.assertRaises(ValueError) as ve: RSA(61, 53, 33) self.assertEqual(ValueError, type(ve.exception))