In [41]:
ru = {}
for i in range(256,10000):
if 'а'<=chr(i)<='я' or 'А'<=chr(i)<='Я':
#print(i-848,'-', chr(i))
ru[chr(i)]=i-848
In [42]:
import math
def phi(n):
amount = 0
for k in range(1, n + 1):
if math.gcd(n, k) == 1:
amount += 1
return amount
In [46]:
e, n = 233, 403 # открытый ключ
E = lambda m: (pow(m,e))%n # Шифрование
In [47]:
d, n = 17,403 # закрытый ключ
D = lambda c: (pow(c,d))%n # расшифрование
In [40]:
p, q = 23, 17
phi(n)
Out[40]:
In [34]:
print ( (e*d)%phi(n))
In [15]:
e*d
Out[15]:
обратный элемент $$a^{-1} \equiv a^{\varphi(n)-1} \pmod n, если { \displaystyle (a,n)=1.} (a,n) = 1. $$
In [27]:
a_1 = lambda a,n: pow(a,phi(n)-1)%n
In [35]:
e = a_1(d,phi(n))
In [58]:
def strRSA(msg):
for m in msg:
print(m+" = "+str(ru[m]) + " RSA = "+str(E(ru[m])))
In [60]:
strRSA('жопа')
In [ ]: