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
52
53
54
55
56
57
|
from Crypto.PublicKey import RSA
import json
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA512, SHA384, SHA256, SHA, MD5
from Crypto import Random
from base64 import b64encode, b64decode
hash = "SHA-256"
# def newkeys(keysize):
# random_generator = Random.new().read
# key = RSA.generate(keysize, random_generator)
# private, public = key, key.publickey()
# return public, private
# def importKey(externKey):
# return RSA.importKey(externKey)
# def getpublickey(priv_key):
# return priv_key.publickey()
def encrypt(message, pub_key):
cipher = PKCS1_OAEP.new(pub_key)
return cipher.encrypt(message)
if __name__ == "__main__":
myself = {
"student_id": "e2482057",
"public_key": """-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3agASpH/TplIAX0YBqmh
5q3Iq6+LcJtlUVWiI/v0T74XwYPZaJpAArHaiMUGXAWxmzfbvEo1wE9RzySYV/5k
QSpYDRekpOn0flIAQHORVbJ08s0udH6/c2AyAzqiwZbR1DRr7M90pSLvWvzHQT+c
kT6rXYcp9GlSAv3AXRw5ZYalbQf7ST/Mb4T8O1MRkAatzXg3T4x3XJ3uxHOletLL
SzsfY52kEn0uaFG6UI7UG50h8jcjqBxn+ETbn2YEZG5ecmPdYNakq2pqrdWXWMhE
AUd927qkxgg/nyyFqwxIbTxebxzpNX8IHMT8PgNdMxVMqnhBWxiw1nborY+pwGVL
MwIDAQAB
-----END PUBLIC KEY-----"""
}
with open("../secrets/gradecoin.pub", "r") as fs:
data = fs.read()
pubkeyobj = RSA.importKey(data)
cipher = PKCS1_OAEP.new(pubkeyobj)
ser = json.dumps(myself, separators=(',', ':'))
a = cipher.encrypt(ser)
print(f"{a}")
|