diff options
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/python_client.py | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/scripts/python_client.py b/scripts/python_client.py new file mode 100644 index 0000000..fe96cc2 --- /dev/null +++ b/scripts/python_client.py | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | from Crypto.PublicKey import RSA | ||
| 2 | import json | ||
| 3 | from Crypto.Cipher import PKCS1_OAEP | ||
| 4 | from Crypto.Signature import PKCS1_v1_5 | ||
| 5 | from Crypto.Hash import SHA512, SHA384, SHA256, SHA, MD5 | ||
| 6 | from Crypto import Random | ||
| 7 | from base64 import b64encode, b64decode | ||
| 8 | hash = "SHA-256" | ||
| 9 | |||
| 10 | # def newkeys(keysize): | ||
| 11 | # random_generator = Random.new().read | ||
| 12 | # key = RSA.generate(keysize, random_generator) | ||
| 13 | # private, public = key, key.publickey() | ||
| 14 | # return public, private | ||
| 15 | |||
| 16 | # def importKey(externKey): | ||
| 17 | # return RSA.importKey(externKey) | ||
| 18 | |||
| 19 | # def getpublickey(priv_key): | ||
| 20 | # return priv_key.publickey() | ||
| 21 | |||
| 22 | |||
| 23 | def encrypt(message, pub_key): | ||
| 24 | cipher = PKCS1_OAEP.new(pub_key) | ||
| 25 | return cipher.encrypt(message) | ||
| 26 | |||
| 27 | |||
| 28 | if __name__ == "__main__": | ||
| 29 | |||
| 30 | myself = { | ||
| 31 | "student_id": "e2482057", | ||
| 32 | "public_key": """-----BEGIN PUBLIC KEY----- | ||
| 33 | MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3agASpH/TplIAX0YBqmh | ||
| 34 | 5q3Iq6+LcJtlUVWiI/v0T74XwYPZaJpAArHaiMUGXAWxmzfbvEo1wE9RzySYV/5k | ||
| 35 | QSpYDRekpOn0flIAQHORVbJ08s0udH6/c2AyAzqiwZbR1DRr7M90pSLvWvzHQT+c | ||
| 36 | kT6rXYcp9GlSAv3AXRw5ZYalbQf7ST/Mb4T8O1MRkAatzXg3T4x3XJ3uxHOletLL | ||
| 37 | SzsfY52kEn0uaFG6UI7UG50h8jcjqBxn+ETbn2YEZG5ecmPdYNakq2pqrdWXWMhE | ||
| 38 | AUd927qkxgg/nyyFqwxIbTxebxzpNX8IHMT8PgNdMxVMqnhBWxiw1nborY+pwGVL | ||
| 39 | MwIDAQAB | ||
| 40 | -----END PUBLIC KEY-----""" | ||
| 41 | } | ||
| 42 | with open("../secrets/gradecoin.pub", "r") as fs: | ||
| 43 | data = fs.read() | ||
| 44 | pubkeyobj = RSA.importKey(data) | ||
| 45 | |||
| 46 | cipher = PKCS1_OAEP.new(pubkeyobj) | ||
| 47 | |||
| 48 | ser = json.dumps(myself, separators=(',', ':')) | ||
| 49 | |||
| 50 | a = cipher.encrypt(ser) | ||
| 51 | |||
| 52 | print(f"{a}") | ||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | |||
