I have python code that decrypt JWE, but my issue is that I need to keep up with quite high load of data to decode per second and my code is too slow...
Does someone maybe has a suggestion on how to make this code work faster?
This is the decryption code which should return {"test": "test"}
from jwcrypto import jwe, jwk
DATA = "eyJhbGciOiJSU0EtT0FFUC0yNTYiLCJlbmMiOiJBMjU2R0NNIiwia2lkIjoiZDgzNTIyNGItNmJlOS00MWQ0LTkyYWEtYzEyOGZkNWU5ZWNlIn0.Gadr3iydQTwKzDKp7wYeDX4gzu8_hD9t8iylgtklGdrKDOtK4pScUNuoBX92R9gnvWapNXpwcr2CEdQ45_POJ-HpFONOFCUDFuOxTfHn7ncytEDCv7a5TTbDAVBAmcH9mbkeo0DRok6vJxrprgl1USzK3UjhXzdUPTWhp8tMYtXXNzaPVbgthNqe4Hy4TX-K1Mcl_ZFcJn_JeLFE9kOWid1Dwaltzqh4pXQQoFMosumSNDGApY-DKpLpzcy_8UV890B1cnsceuggg88iPXM_GpJzKMuxkzt-451h7V500UOkdxz6M4PgAf8YLNmwziyJe3dOSEIlub0fqiZhhRNUvA.e3vioHj1RvZb58UL.-cBm-2fjYZB_EczWj78byA.3M3UqyaPUZIekudlT3aTjg"
DECRYPTION_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCs/yqrB8VjupUov9zApR6iEkEAPRzWrKvn+ZKlx6Rt/qTDNg0jjKahsHqTultndcB+58z+RQ/wAGGAoPm1f1JmY9P7pOGOOyGGEEsyonfa3PVwJ8Yryw3Cu0nwm2q0r9Pdv9d+BGdpPBubZ6Io5OIAtVrR7tjj8OZkGs3UXmMruXc8MTkXkO5Hccf7z+GNCu+ebNrM7sDVT+akjB2swRfHgsgwtLnnsCslyu55G6xiQIk2M7WURl7q37Y0pG4glYj4401n9lK0b8YbiuXhoeOY7799zd7gGSL1KdA14U0YSiHfbPLOV2e1Tjfv1adY97b2cSgt3ml6rbFj3siOyxyFAgMBAAECggEACu5Qlt/10/hdJmcBHtcQGXOH0ekKonHkptK5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FISHibok+/ofMAhxn9/Zt4StQYdTWtxyht6YhUFi1VKEWO1IqxLrXReIR9JAm5/gtwJBuKPh3DPw8HDlCtX8AQlh2EOInRp8hcUdPl"
KEY_ID = "d835224b-6be9-41d4-92aa-c128fd5e9ece"
def key_to_pem(key: str, is_public: bool = True) -> bytes:
pem = [f"-----BEGIN {'PUBLIC' if is_public else 'PRIVATE'} KEY-----"]
for group in grouper(key, 64):
pem.append(''.join(group))
pem.append(f"-----END {'PUBLIC' if is_public else 'PRIVATE'} KEY-----")
return str.encode('\n'.join(pem))
def decrypt_data(encrypted_data: str, pem_decryption_key: bytes, key_id: str) -> str:
private_key = jwk.JWK.from_pem(pem_decryption_key)
protected_header = {
"alg": "RSA-OAEP-256",
"enc": "A256GCM",
"kid": key_id,
}
jwetoken = jwe.JWE(protected=protected_header)
jwetoken.deserialize(encrypted_data, key=private_key)
payload = jwetoken.payload
return payload.decode("utf-8")
if __name__ == '__main__':
decrypt_key = key_to_pem(DECRYPTION_KEY, is_public=False)
print(decrypt_data(DATA, decrypt_key, KEY_ID))