├── .gitignore
├── .idea
├── RSA.iml
├── misc.xml
├── modules.xml
└── workspace.xml
├── LICENSE
├── README.md
├── main.py
├── makeRsaKeys.py
├── operation.py
├── primeTools.py
├── rsa.py
└── static
├── 512cipher.png
├── cipher.png
├── gui.png
└── keys.png
/.gitignore:
--------------------------------------------------------------------------------
1 |
2 | .DS_Store
3 | __pycache__
4 | rsa_privkey.txt
5 | rsa_pubkey.txt
6 | plaintext.txt
7 | ciphertext.txt
8 |
--------------------------------------------------------------------------------
/.idea/RSA.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
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 | true
33 | DEFINITION_ORDER
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 | 1525091017336
124 |
125 |
126 | 1525091017336
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 SliverYou
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## RSA 加解密的 Python 实现
2 |
3 | ### 环境
4 |
5 | 1. 使用的 Python 版本是:Python 3.6.3。
6 | 2. 无使用其他第三方库,根据密码学实验要求纯手工实现。
7 |
8 | ### 使用
9 |
10 | **进行加密**
11 |
12 | 在得到的项目文件夹下使用如下命令即可启动 GUI 界面,先点击生成按钮生成密钥,然后输入待加密明文,点击加密/解密按钮即可:
13 |
14 | ```Python
15 | $ python3 main.py
16 | ```
17 |
18 | **注意事项:**
19 |
20 | * RSA 属公钥密码体制,所以在运行过程中会在本地生成 rsa_pubkey.txt 和 rsa_privkey.txt 文件,用于存储公钥和私钥。
21 | * 密钥的位数默认为1024位,填充规则选用 PKCS1-PADDING,即对1024位的密钥,其字节为128位,对输入的数据将会采用 128 - 11 = 117 字节为一个分组进行加密,不足117字节的将会选择随机数进行填充。
22 | * 输入明文不用在意个数,最好是在200字符以内。
23 | * 加密结果会用 Base64 进行编码,并存储在 ciphertext.txt 文件中;解密结果将存储在 plaintext.txt 文件中。
24 |
25 | 
26 |
27 | 
28 |
29 | **选择加密位数**
30 |
31 | 在位数框输入框中输入想要的生成的密钥的位数后,点击生成即可生成对应位数的公私钥:
32 |
33 | 
34 |
35 | 可以看到新生成的公私钥文件:
36 |
37 | 
38 |
39 |
40 | ### 拓展
41 |
42 | 参考 operation.py 和 rsa.py 文件,可对其功能进行拓展。
--------------------------------------------------------------------------------
/main.py:
--------------------------------------------------------------------------------
1 | # -*- coding: UTF-8 -*-
2 | __author__ = 'Sliver'
3 |
4 | import os
5 | from tkinter import *
6 | import tkinter.messagebox as mbox
7 |
8 | from rsa import RSA
9 | from makeRsaKeys import makeKeyFiles, delKeyFiles, Key
10 |
11 |
12 | class Cipher(Frame):
13 | def __init__(self, master):
14 | frame = Frame(master)
15 | frame.pack()
16 | self.var1 = Variable()
17 | self.var2 = Variable()
18 | self.var3 = Variable()
19 | self.var4 = Variable()
20 |
21 | self.title1 = Label(frame, text="对消息进行加密和解密", fg='red')
22 | self.title1.grid(row=0, column=1)
23 |
24 | self.lab1 = Label(frame, text="消息:")
25 | self.lab1.grid(row=1, column=0, sticky=W)
26 |
27 | self.ent1 = Entry(frame)
28 | self.ent1.grid(row=1, column=1, sticky=W)
29 |
30 | self.button1 = Button(frame, text="加密", command=self.encrypt, fg='orange')
31 | self.button1.grid(row=1, column=2)
32 |
33 | self.lab2 = Label(frame, text="结果:")
34 | self.lab2.grid(row=2, column=0, sticky=W)
35 |
36 | self.ent2 = Entry(frame, textvariable=self.var1)
37 | self.ent2.grid(row=2, column=1, sticky=W)
38 |
39 | self.button2 = Button(frame, text="解密", command=self.decrypt, fg='orange')
40 | self.button2.grid(row=2, column=2)
41 |
42 | self.info = Label(frame, text="", textvariable=self.var2, fg='orange')
43 | self.info.grid(row=3, column=1)
44 |
45 | self.lab_null = Label(frame, text="")
46 | self.lab_null.grid(row=4, column=1)
47 |
48 | self.title2 = Label(frame, text="生成 RSA 公钥和密钥", fg='red')
49 | self.title2.grid(row=5, column=1)
50 |
51 | self.lab3 = Label(frame, text="位数:")
52 | self.lab3.grid(row=6, column=0, sticky=W)
53 |
54 | self.ent3 = Entry(frame)
55 | self.ent3.grid(row=6, column=1, sticky=W)
56 |
57 | self.lab4 = Label(frame, text="公钥:")
58 | self.lab4.grid(row=7, column=0, sticky=W)
59 |
60 | self.ent4 = Entry(frame, textvariable=self.var3)
61 | self.ent4.grid(row=7, column=1, sticky=W)
62 |
63 | self.lab5 = Label(frame, text="私钥:")
64 | self.lab5.grid(row=8, column=0, sticky=W)
65 |
66 | self.ent5 = Entry(frame, textvariable=self.var4)
67 | self.ent5.grid(row=8, column=1, sticky=W)
68 |
69 | self.button3 = Button(frame, text="生成", command=self.generate, fg='orange')
70 | self.button3.grid(row=9, column=1, sticky=W)
71 |
72 | self.button4 = Button(frame, text="清除", command=self.delete, fg='orange')
73 | self.button4.grid(row=9, column=1, sticky=E)
74 |
75 | def encrypt(self):
76 | message = self.ent1.get()
77 | size = self.ent3.get()
78 | result = RSA(message, pubkey=size + '_pubkey.txt', privkey=size + '_privkey.txt') if size else RSA(message)
79 | ciphertext = result.ciphertext
80 | print('Ciphertext: ' + ciphertext)
81 | self.var2.set('已加密至 ciphertext.txt 文件。')
82 |
83 | def decrypt(self):
84 | with open('ciphertext.txt', 'rt') as f:
85 | message = f.read()
86 |
87 | size = self.ent3.get()
88 | result = RSA(message, pubkey=size + '_pubkey.txt', privkey=size + '_privkey.txt') if size else RSA(message)
89 |
90 | plaintext = result.plaintext
91 | if plaintext:
92 | print('Plaintext: ' + plaintext)
93 | self.var1.set(plaintext)
94 | self.var2.set('已解密至 plaintext.txt 文件。')
95 | else:
96 | mbox.showwarning('错误', '密文缺失信息,无法解密!')
97 |
98 | def generate(self):
99 | size = self.ent3.get()
100 | if size:
101 | Key.setKeySize(int(size))
102 | flag = makeKeyFiles(name=size, keysize=int(size)) if size else makeKeyFiles()
103 |
104 | if flag:
105 | self.var3.set(flag[0])
106 | self.var4.set(flag[1])
107 | mbox.showinfo('成功', '公钥和私钥生成成功!')
108 | else:
109 | mbox.showwarning('错误', '当前文件夹下已有同名公钥和私钥,请选择别的名称进行生成!')
110 |
111 | def delete(self):
112 | name = self.ent3.get()
113 | flag = delKeyFiles(name) if name else delKeyFiles()
114 | if flag:
115 | self.var3.set('')
116 | self.var4.set('')
117 | mbox.showinfo('成功', '公钥和私钥清除成功!')
118 | else:
119 | mbox.showwarning('错误', '当前文件夹下无指定公钥和私钥,请核对后重新进行操作!')
120 |
121 |
122 | root = Tk()
123 | root.title('RSA')
124 | root.geometry('400x280')
125 | app = Cipher(root)
126 | root.mainloop()
--------------------------------------------------------------------------------
/makeRsaKeys.py:
--------------------------------------------------------------------------------
1 | # -*- coding: UTF-8 -*-
2 | __author__ = 'Sliver'
3 |
4 | import random, os
5 | from primeTools import generateLargePrime, gcd, modInverse
6 |
7 |
8 | class Key:
9 | _keysize = 1024
10 |
11 | @classmethod
12 | def getKeySize(cls):
13 | '''返回当前的 keySize 表示的数值。'''
14 | return cls._keysize
15 |
16 | @classmethod
17 | def setKeySize(cls, size):
18 | '''设置 keySize 的值。'''
19 | if size in [64, 128, 256, 512, 1024]:
20 | cls._keysize = size
21 | else:
22 | raise ValueError('The size must be in the [64, 128, 256, 512, 1024].')
23 |
24 | @classmethod
25 | def getByteSize(cls):
26 | '''返回当前 keySize 下的 byteSize 表示的数值。'''
27 | return cls._keysize // 8
28 |
29 |
30 | def generateKeys(keysize):
31 | '''生成指定位数的 RSA 公钥和私钥,默认生成1024位公钥和私钥,且名称前缀为 rsa。'''
32 |
33 | # 创建两个大素数 p 和 q,并计算它们的乘积 n
34 | p, q = generateLargePrime(keysize), generateLargePrime(keysize)
35 | n = p * q
36 |
37 | # 生成 e,并确保 e 和 (p - 1) * (q - 1) 互素
38 | while True:
39 | e = random.randrange(2 ** (keysize - 1), 2 ** (keysize))
40 | if gcd(e, (p - 1) * (q - 1)) == 1:
41 | break
42 |
43 | # 计算解密系数 d,并使其在 (p - 1) * (q - 1) 下与 e 互为逆元
44 | d = modInverse(e, (p - 1) * (q - 1))
45 |
46 | publicKey, privateKey = (n, e), (n, d)
47 |
48 | print('Public key:', publicKey)
49 | print('Private key:', privateKey)
50 |
51 | return (publicKey, privateKey)
52 |
53 |
54 | def makeKeyFiles(name='rsa', keysize=Key.getKeySize()):
55 | '''生成对应的 RSA 公钥和私钥文件。'''
56 |
57 | # 防止覆盖已有的公钥和私钥文件
58 | if os.path.exists('{}_pubkey.txt'.format(name)) or os.path.exists('{}_privkey.txt'.format(name)):
59 | print(
60 | 'WARNING: The file {}_pubkey.txt or {}_privkey.txt already exists! Use a different name or delete these files and re-run this program.'.format(
61 | name, name))
62 | return False
63 |
64 | publicKey, privateKey = generateKeys(keysize)
65 |
66 | # 将公钥和私钥写入文件中
67 | with open('{}_pubkey.txt'.format(name), 'wt') as f:
68 | f.write('{},{}'.format(publicKey[0], publicKey[1]))
69 |
70 | with open('{}_privkey.txt'.format(name), 'wt') as f:
71 | f.write('{},{}'.format(privateKey[0], privateKey[1]))
72 |
73 | return (publicKey, privateKey)
74 |
75 |
76 | def delKeyFiles(name='rsa'):
77 | '''删除已有的 RSA 公钥和私钥文件。'''
78 | if os.path.exists('{}_pubkey.txt'.format(name)) and os.path.exists('{}_privkey.txt'.format(name)):
79 | os.remove('{}_pubkey.txt'.format(name))
80 | os.remove('{}_privkey.txt'.format(name))
81 | return True
82 | return False
83 |
84 |
85 | if __name__ == '__main__':
86 | makeKeyFiles(name='rsa', keysize=Key.getKeySize())
87 | # delKeyFiles('rsa')
88 |
--------------------------------------------------------------------------------
/operation.py:
--------------------------------------------------------------------------------
1 | # -*- coding: UTF-8 -*-
2 | __author__ = 'Sliver'
3 |
4 | import random
5 | from makeRsaKeys import Key
6 |
7 |
8 | def bin2dex(binflow):
9 | '''
10 | 将8位二进制数据流转化为十进制数值。
11 | 如:'01100001' -> 97
12 | '''
13 | return int(binflow, 2)
14 |
15 |
16 | def bytes2int(byteflow):
17 | '''将 bytes 类型数据转化大整型数值。'''
18 | return int.from_bytes(byteflow, 'big')
19 |
20 |
21 | def int2bytes(intflow):
22 | '''将大整型数值转化为 bytes 类型。'''
23 | KEYSIZE, BYTESIZE = Key.getKeySize(), Key.getByteSize()
24 |
25 | intflow = bin(intflow).replace('0b', '')
26 | size = 2 * KEYSIZE
27 | mod = len(intflow) % size
28 |
29 | # 不足 2 * KEYSIZE 位的补全位 2 * KEYSIZE 位
30 | if mod:
31 | space = size - mod
32 | intflow = '0' * space + intflow
33 | result = []
34 |
35 | for i in range(2 * BYTESIZE):
36 | temp = intflow[i * 8:i * 8 + 8]
37 | result.append(bin2dex(''.join(temp)))
38 |
39 | return bytes(result)
40 |
41 |
42 | def padding(message, length):
43 | '''填充函数,将信息根据 PKCS1 填充方案,对字节进行填充。'''
44 | BYTESIZE = Key.getByteSize()
45 |
46 | mod = length % BYTESIZE
47 | space = BYTESIZE - mod - 3
48 | message = bytes(
49 | [0x00, 0x02] + [random.randint(1, 255)
50 | for _ in range(space)] + [0x00]) + message
51 | return message
52 |
53 |
54 | def readKeyFile(filename):
55 | '''读取密钥文件,取出其中的信息:n 和 EorD。'''
56 | with open(filename, 'rt') as f:
57 | content = f.read()
58 | n, EorD = content.split(',')
59 | return (int(n), int(EorD))
60 |
61 |
62 | def unpadding(byteflow):
63 | '''去除填充项,只选择有意义的项。'''
64 | index = byteflow.rfind(b'\x00')
65 | return byteflow[index + 1:]
66 |
67 |
68 | if __name__ == '__main__':
69 | # string = 'RSA密码有两个有趣的性质:1、在合理的时间内要破译它是及其困难的,因为任何一个能很快破解这个密码的算法都在本质上相当于找出一种对非常大的数快速进行因式分解的新方法。2、它是一个公钥密码:此密码过程有两个部分,首先,信息必须要由发送者变为加密形式,其次,接收者必须对之解密。在公钥密码体系中,密码被设计成:尽管可以得知对一条信息加密所必需的方法,但并不能让你由此得知对这条信息解密的方法。'
70 |
71 | m = 9
72 | n, e = readKeyFile('rsa_pubkey.txt')
73 | cipherdigit = pow(m, e, n)
74 | print(cipherdigit)
75 |
76 | n, d = readKeyFile('rsa_privkey.txt')
77 | plaindigit = pow(cipherdigit, d, n)
78 | print(plaindigit)
79 |
80 | print((int.from_bytes(b'abcdefgh', 'big')))
81 | print(bytes2int(b'abcdefgh'))
82 |
83 | print((7017280452245743464).to_bytes(8, 'big'))
84 |
--------------------------------------------------------------------------------
/primeTools.py:
--------------------------------------------------------------------------------
1 | # -*- coding: UTF-8 -*-
2 | __author__ = 'Sliver'
3 |
4 | import random
5 |
6 |
7 | def millerRabin(num):
8 | '''Miller-Rabin 素性检测算法。'''
9 | if num <= 2:
10 | return True if num == 2 else False
11 | d, s = num - 1, 0
12 |
13 | while d % 2 == 0:
14 | # 令 num = 2 ^ s * d,其中 d 为一个奇数
15 | d = d // 2
16 | s += 1
17 | x = [0 for _ in range(s + 1)]
18 |
19 | for _ in range(20):
20 | # 在 [2, num - 1] 区间中挑选一个随机数 a
21 | a = random.randint(2, num - 1)
22 | # 计算 (a ^ d) % num 的值
23 | x[0] = pow(a, d, num)
24 | for i in range(1, s + 1):
25 | # 根据二次探测定理排查合数
26 | x[i] = (x[i - 1] ** 2) % num
27 | if x[i] == 1 and x[i - 1] != 1 and x[i - 1] != num - 1:
28 | return False
29 | if x[s] != 1:
30 | return False
31 |
32 | return True
33 |
34 |
35 | def isPrime(num):
36 | '''对 Miller-Rabin 素性检测算法的封装,判断一个数是否为素数。'''
37 | if (num < 2):
38 | return False
39 |
40 | lowPrimes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
41 | 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
42 | 101, 103, 107, 109, 113, 127, 131, 137, 139, 149,
43 | 151, 157, 163, 167, 173, 179, 181, 191, 193, 197,
44 | 199, 211, 223, 227, 229, 233, 239, 241, 251, 257,
45 | 263, 269, 271, 277, 281, 283, 293, 307, 311, 313,
46 | 317, 331, 337, 347, 349, 353, 359, 367, 373, 379,
47 | 383, 389, 397, 401, 409, 419, 421, 431, 433, 439,
48 | 443, 449, 457, 461, 463, 467, 479, 487, 491, 499,
49 | 503, 509, 521, 523, 541, 547, 557, 563, 569, 571,
50 | 577, 587, 593, 599, 601, 607, 613, 617, 619, 631,
51 | 641, 643, 647, 653, 659, 661, 673, 677, 683, 691,
52 | 701, 709, 719, 727, 733, 739, 743, 751, 757, 761,
53 | 769, 773, 787, 797, 809, 811, 821, 823, 827, 829,
54 | 839, 853, 857, 859, 863, 877, 881, 883, 887, 907,
55 | 911, 919, 929, 937, 941, 947, 953, 967, 971, 977,
56 | 983, 991, 997]
57 |
58 | if num in lowPrimes:
59 | return True
60 |
61 | for prime in lowPrimes:
62 | if num % prime == 0:
63 | return False
64 |
65 | return millerRabin(num)
66 |
67 |
68 | def generateLargePrime(keysize=1024):
69 | '''生成一个大素数,默认生成1024位大素数。'''
70 | while True:
71 | num = random.randrange(2 ** (keysize - 1), 2 ** keysize)
72 | if isPrime(num):
73 | return num
74 |
75 |
76 | def gcd(a, b):
77 | '''返回两个数的最大公约数。'''
78 | while a != 0:
79 | a, b = b % a, a
80 | return b
81 |
82 |
83 | def modInverse(a, m):
84 | '''计算数值 a 在数值 m 下的模逆,即 (a * a ^ -1) % m = 1。'''
85 | if gcd(a, m) != 1:
86 | return None
87 |
88 | s0, t0, r0 = 1, 0, a
89 | s1, t1, r1 = 0, 1, m
90 |
91 | while r1 != 0:
92 | q = r0 // r1
93 | s1, t1, r1, s0, t0, r0 = (s0 - q * s1), (t0 - q * t1), (r0 - q * r1), s1, t1, r1
94 |
95 | return s0 % m
96 |
97 |
98 | if __name__ == '__main__':
99 | # print(millerRabin(3))
100 | # prime = generateLargePrime()
101 | # print(prime)
102 | # lst = list(filter(isPrime, range(0, 100000)))
103 | # print(lst)
104 | print(modInverse(37, 197))
105 | print(myPow(5, 3, 10))
106 |
--------------------------------------------------------------------------------
/rsa.py:
--------------------------------------------------------------------------------
1 | # -*- coding: UTF-8 -*-
2 | __author__ = 'Sliver'
3 |
4 | import base64
5 | from operation import *
6 | from makeRsaKeys import Key
7 |
8 |
9 | class RSA:
10 | def __init__(self, message, pubkey='rsa_pubkey.txt', privkey='rsa_privkey.txt'):
11 | self.message = message
12 | self.pubkey = pubkey
13 | self.privkey = privkey
14 | self.BYTESIZE = Key.getByteSize()
15 |
16 | @property
17 | def ciphertext(self):
18 | return self.__encrypt()
19 |
20 | @property
21 | def plaintext(self):
22 | return self.__decrypt()
23 |
24 | def __encrypt(self):
25 | message = bytes(self.message.encode('utf-8'))
26 | length ,size = len(message), self.BYTESIZE - 11
27 | result = bytes()
28 |
29 | if length <= size:
30 | # PKCS1 填充方案
31 | result = padding(message, length)
32 | n, e = readKeyFile(self.pubkey)
33 | cipherdigit = pow(bytes2int(result), e, n)
34 | result = int2bytes(cipherdigit)
35 | else:
36 | times, mod = divmod(length, size)
37 | n, e = readKeyFile(self.pubkey)
38 | if mod:
39 | times += 1
40 | # 分组进行填充
41 | for i in range(times):
42 | temp = padding(message[i * size:i * size + size], len(message[i * size:i * size + size]))
43 | cipherdigit = pow(bytes2int(temp), e, n)
44 | result += int2bytes(cipherdigit)
45 |
46 | self.write2file(base64.b64encode(result), flag=0)
47 | return base64.b64encode(result).decode('ascii')
48 |
49 | def __decrypt(self):
50 | message = base64.b64decode(self.message.encode('ascii'))
51 | length, size, result = len(message), 2 * self.BYTESIZE, bytes()
52 | times, mod = divmod(length, size)
53 |
54 | if mod:
55 | return False
56 |
57 | for i in range(times):
58 | temp = message[i * size:i * size + size]
59 | n, d = readKeyFile(self.privkey)
60 | plaindigit = pow(bytes2int(temp), d, n)
61 | result += unpadding(int2bytes(plaindigit))
62 |
63 | self.write2file(result, flag=1)
64 | return result.decode('utf-8')
65 |
66 | @staticmethod
67 | def write2file(bytesflow, flag=0):
68 | name = ['ciphertext', 'plaintext']
69 | with open('{}.txt'.format(name[flag]), 'wb') as f:
70 | f.write(bytesflow)
71 |
72 |
73 | if __name__ == '__main__':
74 | r1 = RSA('你好,我的名字叫 Sliver,你呢?春风十里,不如你。')
75 | c = r1.ciphertext
76 | print(c)
77 |
78 | r2 = RSA(c)
79 | p = r2.plaintext
80 | print(p)
--------------------------------------------------------------------------------
/static/512cipher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sliveryou/rsa-python-cipher/61d5bb659e9f7a0fae6fdc533e6e6ff62f8b0885/static/512cipher.png
--------------------------------------------------------------------------------
/static/cipher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sliveryou/rsa-python-cipher/61d5bb659e9f7a0fae6fdc533e6e6ff62f8b0885/static/cipher.png
--------------------------------------------------------------------------------
/static/gui.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sliveryou/rsa-python-cipher/61d5bb659e9f7a0fae6fdc533e6e6ff62f8b0885/static/gui.png
--------------------------------------------------------------------------------
/static/keys.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sliveryou/rsa-python-cipher/61d5bb659e9f7a0fae6fdc533e6e6ff62f8b0885/static/keys.png
--------------------------------------------------------------------------------