-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathrsa.py
More file actions
executable file
·51 lines (43 loc) · 1.17 KB
/
rsa.py
File metadata and controls
executable file
·51 lines (43 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2019 frey <[email protected]>
# blog:https://www.vhcffh.com
#
# Distributed under terms of the MIT license.
import random
# 判断是否是素数
def is_prime(n):
if n == 2:
return True
for i in range(2, n // 2 + 1):
if n % i == 0:
return False
return True
# 生成两个不同的素数p,q
def get_pq():
p, q = 0, 0
while True:
p, q = random.randint(10, 100), random.randint(10, 100)
if is_prime(p) and is_prime(q) and p != q:
return p, q
# 生成e和d
def get_ed(p, q):
phn = (p - 1) * (q - 1) # n的欧拉函数
while True:
e = random.randint(2, 50)
if is_prime(e):
for d in range(2, 1000):
if e * d % phn == 1:
return e, d
if __name__ == '__main__':
p, q = get_pq()
n = p * q
e, d = get_ed(p, q)
print("公钥: (n,e) =", (n, e))
print("私钥: (n,d) =", (n, d))
m = [random.randint(2, 100) for i in range(10)]
c = [mi ** e % n for mi in m]
print("明文 m =", m)
print("密文 c =", c)
print("解密 m =", [ci ** d % n for ci in c])