import os from cryptography.hazmat.primitives.ciphers.modes import CBC from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.primitives import padding import base64 import binascii def bin2hex(binStr): return binascii.hexlify(binStr) def hex2bin(hexStr): return binascii.unhexlify(hexStr) iv = hex2bin("6e657665726d696e647468656e66756b") key = hex2bin("6e6565726d666e646e6565726d666e64") mode = CBC(iv) cipher = Cipher(algorithms.AES(key), mode) encryptor = cipher.encryptor() payload = dict() payload["public_key"] = """-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoNsLhsQzL+5HiGuaI4mu K0TqiLEOFPOl8kd9pWR8eop0b7/N2ggDRXATIkV+XwzeQp+k9lGT1h2B2bDhxn51 lBESb2rSqKKzTVf4SXoPxqvPwsi1bnmHcRG8tTr+ZWcJdblXabMUkKUJCrwKhvr8 UpOi0tndcc7buVq4EWCIBUeG4SCFHeBu1Ue9Zus7BpwjSxF4jHH4M8ssl/ioN8+m qz9LcDQe3kvVRMlVbaXjYnWyP0VjwRbQuwIjMsDVaTQUIVtPZY9el7LiIxCqZ4mO cnnwlIZyGLN52uqDZ1cz7aSO18YIGcbCpxuOuPhSzrnZNvHJZFIkacd477fK+PKB 7QIDAQAB -----END PUBLIC KEY-----""" payload["student_id"] = "e223795" payload["passwd"] = "pmcTCKox99NFsqp" import json payload_string = json.dumps(payload) byte_guys = str.encode(payload_string) padder = padding.PKCS7(128).padder() padded_data = padder.update(byte_guys) padded_data += padder.finalize() ct = encryptor.update(padded_data) + encryptor.finalize() c = base64.b64encode(ct) import Crypto from Crypto.Cipher import PKCS1_OAEP from Crypto.PublicKey import RSA gradecoin_key = """-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyGuqiCPGcguy+Y9TH7Bl 7XlEsalyqb9bYlzpbV0dnqZ3lPkEPkuOhkN+GcuiV6iXtSwyh7nB+xTRXKJFRUBO /jbN8jfcxVwBu0JxjF3v1YRBxbOHhz2A295mbKD9xHQCKxkfYBNkUXxj8gd+GaDv QiSW5NdrX/lEkvqfGtdEX1m2+HdcG0+3YW24Xg0znhCwLr+sorLuJaDy9Xa0Uo+D PWGC5s001U/BxkCIWJ+eJQCb7Bv+9vXb8BGRK/ecMb/fb6h5O+8fgB64RCHMgcc2 v+Q/dPt8kHX1OJdMuYUrUJGACppMQY3W6e1HdlRIBcZKL2LMZ2CrIB/2D5LiJhPT hQIDAQAB -----END PUBLIC KEY-----""" publickey = RSA.import_key(gradecoin_key) asy_cipher = PKCS1_OAEP.new(publickey, hashAlgo=Crypto.Hash.SHA256) ciphertext = asy_cipher.encrypt(key) key = base64.b64encode(ciphertext)