Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python-pkc11 with javascript + flask #177

Open
biyani701 opened this issue Jul 8, 2024 · 0 comments
Open

python-pkc11 with javascript + flask #177

biyani701 opened this issue Jul 8, 2024 · 0 comments

Comments

@biyani701
Copy link

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?

@biyani701 biyani701 changed the title python-pkc11 with javascript python-pkc11 with javascript + flask Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant