-
Notifications
You must be signed in to change notification settings - Fork 0
/
14. rsa1.py
45 lines (34 loc) · 1.06 KB
/
14. rsa1.py
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
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
from Crypto.Hash import SHA256 as SHA
def createPEM():
# 개인키 생성
private_key = RSA.generate(1024)
with open('privatekey.pem', 'wb+') as f:
f.write(private_key.exportKey('PEM'))
public_key = private_key.publickey()
with open('publickey.pem', 'wb+') as f:
f.write(public_key.exportKey('PEM'))
def readPEM(pemfile):
h = open(pemfile, 'r')
key = RSA.importKey(h.read())
h.close()
return key
def rsa_enc(msg):
public_key = readPEM('publickey.pem')
cipher = PKCS1_OAEP.new(public_key)
encdata = cipher.encrypt(msg)
return encdata
def rsa_dec(msg):
private_key = readPEM('privatekey.pem')
cipher = PKCS1_OAEP.new(private_key)
decdata = cipher.decrypt(msg)
return decdata
def main():
#createPEM()
msg = 'samsjang loves python'
ciphered = rsa_enc(msg.encode('utf-8'))
print(ciphered)
deciphered = rsa_dec(ciphered)
print(deciphered)
main()