pyauth / python-pkcs11

PKCS#11/Cryptoki support for Python
MIT License
150 stars 71 forks source link

python-pkc11 with javascript + flask #177

Open biyani701 opened 4 months ago

biyani701 commented 4 months ago

For my use case, i want to encrypt the string in javascript while string decryption will happen on server side (Flask based application). I am using the below function. However, if the string is encrypted like this, the returned value is a string and not a bytes object that private key expects.

Below is the code that i am using for encoding after extracting the modulus and exponent from public key in python

modulusB64 = base64.b64encode(public_key[Attribute.MODULUS]).decode('utf-8') exponentB64 = base64.b64encode(public_key[Attribute.PUBLIC_EXPONENT]).decode('utf-8')

These are passed to javascript.

async function encryptWithPublicKey(modulusB64, exponentB64, data) { // Decode base64 strings to bytes function base64ToHex(str) { var raw = atob(str); var result = ''; for (var i = 0; i < raw.length; i++) { var hex = raw.charCodeAt(i).toString(16); result += (hex.length === 2 ? hex : '0' + hex); } return result.toUpperCase(); }

// Decode base64 to hexadecimal
var modulusHex = base64ToHex(modulusB64);
var exponentHex = base64ToHex(exponentB64);

// Create RSAKey object and set public key
    var rsa = new RSAKey();
    rsa.setPublic(modulusHex, exponentHex);
    console.log("set public key" + rsa);

    var encrypted = rsa.encrypt(data);
    return encrypted;
}

the return value encrypted is passed to private key decrypt key_private[0].decrypt(encrypted_text_hsm)

However, i am getting <_cython_3_0_10.generator object at 0x0000025EC04F2EF0>

how to address this issue?