def test_encipher_rsa(): puk = rsa_public_key(2, 2, 1) assert encipher_rsa(2, puk) == 2 puk = rsa_public_key(2, 3, 1) assert encipher_rsa(2, puk) == 2 puk = rsa_public_key(5, 3, 3) assert encipher_rsa(2, puk) == 8
def test_mutltiprime_rsa_full_example(): # Test example from # https://iopscience.iop.org/article/10.1088/1742-6596/995/1/012030 puk = rsa_public_key(2, 3, 5, 7, 11, 13, 7) prk = rsa_private_key(2, 3, 5, 7, 11, 13, 7) assert puk == (30030, 7) assert prk == (30030, 823) msg = 10 encrypted = encipher_rsa(2 * msg - 15, puk) assert encrypted == 18065 decrypted = (decipher_rsa(encrypted, prk) + 15) / 2 assert decrypted == msg # Test example from # https://www.scirp.org/pdf/JCC_2018032215502008.pdf puk1 = rsa_public_key(53, 41, 43, 47, 41) prk1 = rsa_private_key(53, 41, 43, 47, 41) puk2 = rsa_public_key(53, 41, 43, 47, 97) prk2 = rsa_private_key(53, 41, 43, 47, 97) assert puk1 == (4391633, 41) assert prk1 == (4391633, 294041) assert puk2 == (4391633, 97) assert prk2 == (4391633, 455713) msg = 12321 encrypted = encipher_rsa(encipher_rsa(msg, puk1), puk2) assert encrypted == 1081588 decrypted = decipher_rsa(decipher_rsa(encrypted, prk2), prk1) assert decrypted == msg
def test_encipher_rsa(): puk = rsa_public_key(2, 3, 1) assert encipher_rsa(2, puk) == 2 puk = rsa_public_key(5, 3, 3) assert encipher_rsa(2, puk) == 8 with warns_deprecated_sympy(): puk = rsa_public_key(2, 2, 1) assert encipher_rsa(2, puk) == 2
def test_encipher_rsa(): puk = rsa_public_key(2, 3, 1) assert encipher_rsa(2, puk) == 2 puk = rsa_public_key(5, 3, 3) assert encipher_rsa(2, puk) == 8 with warns(NonInvertibleCipherWarning): puk = rsa_public_key(2, 2, 1) assert encipher_rsa(2, puk) == 2
def test_rsa_crt_extreme(): p = int( "10177157607154245068023861503693082120906487143725062283406501" "54082258226204046999838297167140821364638180697194879500245557" "65445186962893346463841419427008800341257468600224049986260471" "92257248163014468841725476918639415726709736077813632961290911" "0256421232977833028677441206049309220354796014376698325101693" ) q = int( "28752342353095132872290181526607275886182793241660805077850801" "75689512797754286972952273553128181861830576836289738668745250" "34028199691128870676414118458442900035778874482624765513861643" "27966696316822188398336199002306588703902894100476186823849595" "103239410527279605442148285816149368667083114802852804976893" ) r = int( "17698229259868825776879500736350186838850961935956310134378261" "89771862186717463067541369694816245225291921138038800171125596" "07315449521981157084370187887650624061033066022458512942411841" "18747893789972315277160085086164119879536041875335384844820566" "0287479617671726408053319619892052000850883994343378882717849" ) s = int( "68925428438585431029269182233502611027091755064643742383515623" "64321310582896893395529367074942808353187138794422745718419645" "28291231865157212604266903677599180789896916456120289112752835" "98502265889669730331688206825220074713977607415178738015831030" "364290585369150502819743827343552098197095520550865360159439" ) t = int( "69035483433453632820551311892368908779778144568711455301541094" "31487047642322695357696860925747923189635033183069823820910521" "71172909106797748883261493224162414050106920442445896819806600" "15448444826108008217972129130625571421904893252804729877353352" "739420480574842850202181462656251626522910618936534699566291" ) e = 65537 puk = rsa_public_key(p, q, r, s, t, e) prk = rsa_private_key(p, q, r, s, t, e) plaintext = 1000 ciphertext_1 = encipher_rsa(plaintext, puk) ciphertext_2 = encipher_rsa(plaintext, puk, [p, q, r, s, t]) assert ciphertext_1 == ciphertext_2 assert decipher_rsa(ciphertext_1, prk) == decipher_rsa( ciphertext_1, prk, [p, q, r, s, t] )
def test_rsa_multipower_exhanstive(): from sympy.core.numbers import igcd primes = [5, 5, 7] e = 7 args = primes + [e] puk = rsa_public_key(*args, multipower=True) prk = rsa_private_key(*args, multipower=True) n = puk[0] for msg in range(n): if igcd(msg, n) != 1: continue encrypted = encipher_rsa(msg, puk) decrypted = decipher_rsa(encrypted, prk) try: assert decrypted == msg except AssertionError: raise AssertionError( "The RSA is not correctly decrypted " "(Original : {}, Encrypted : {}, Decrypted : {})".format( msg, encrypted, decrypted ) )
def test_rsa_exhaustive(): p, q = 61, 53 e = 17 puk = rsa_public_key(p, q, e, totient='Carmichael') prk = rsa_private_key(p, q, e, totient='Carmichael') for msg in range(puk[0]): encrypted = encipher_rsa(msg, puk) decrypted = decipher_rsa(encrypted, prk) try: assert decrypted == msg except AssertionError: raise AssertionError( "The RSA is not correctly decrypted " \ "(Original : {}, Encrypted : {}, Decrypted : {})" \ .format(msg, encrypted, decrypted) )
def test_rsa_multiprime_exhanstive(): primes = [3, 5, 7, 11] e = 7 args = primes + [e] puk = rsa_public_key(*args, totient='Carmichael') prk = rsa_private_key(*args, totient='Carmichael') n = puk[0] for msg in range(n): encrypted = encipher_rsa(msg, puk) decrypted = decipher_rsa(encrypted, prk) try: assert decrypted == msg except AssertionError: raise AssertionError( "The RSA is not correctly decrypted " \ "(Original : {}, Encrypted : {}, Decrypted : {})" \ .format(msg, encrypted, decrypted) )
def rsa(msg, pqe, **kwargs): nd = rsa_private_key(*pqe) ne = rsa_public_key(*pqe) nlength = len(str(ne[0])) m = [] for x in msg: ordinal = ord(x) m.append(ordinal) cipher = [] for i in m: enciphered = encipher_rsa(i, ne) cipher.append(enciphered) m = [] for c in cipher: m.append(decipher_rsa(c, nd)) dt = "".join([chr(x) for x in m]) return cipher, dt
import sympy.crypto.crypto as crypto charList = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-!@#$%&*()" alicesPrivateKey = (n, d) a = crypto.encipher_rsa(c2, alicesPrivateKey) print(a)
from sympy.crypto.crypto import rsa_private_key, rsa_public_key, encipher_rsa, decipher_rsa a, b, c = 11, 13, 17 rsa_private_key(a, b, c) publickey = rsa_public_key(a, b, c) pt = 8 encipher_rsa(pt, publickey) privatekey = rsa_private_key(a, b, c) ct = 112 decipher_rsa(ct, privatekey)
# In[14]: from sympy.crypto.crypto import encipher_rsa, rsa_public_key # 准备公钥,选取p,q两个不同素数。 p, q, e = 13, 5, 7 # 生成公钥pub_key pub_key = rsa_public_key(p, q, e) # 待加密的明文消息 msg = 12 # 使用公钥pub_key对明文进行加密 encipher_rsa(msg, pub_key) # In[15]: from sympy.crypto.crypto import decipher_rsa, rsa_private_key p, q, e = 13, 5, 7 # 生成私钥pri_key pri_key = rsa_private_key(p, q, e) # 待解密的密文消息 msg = 38 # 使用私钥pri_key对密文进行解密 decipher_rsa(msg, pri_key)