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

encryption returning false #296

Open
Superior126 opened this issue Jun 11, 2023 · 4 comments
Open

encryption returning false #296

Superior126 opened this issue Jun 11, 2023 · 4 comments

Comments

@Superior126
Copy link

Superior126 commented Jun 11, 2023

I'm trying to encrypt some text with a server public key, but it keeps returning false for whatever reason.

Example pub key from server:

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3fQZaIIUSnWYVR++XoHZ
g7GQS996veS+vxY+FF7Oy9Kd943XHrzrYMuCOaOruB6SEZzu2U1xrXnEbTTdwNcj
/WZZCZ1JUPEILhLOPpTB9IE6AsyDfZJZcLeP459Db+5Pw79nf/Q8zyF5d8KS5fWD
INnS38X1EGV/nd7upCcYc8XXeX0a4c2Esd63OK6p2yrlVbjaxPokB6HFf6iZ+WXa
q6a8oO8Fd8wHWSZYnnlGJF0u3kbyas3gHFjiITpTFjQK2QavxzCuR/Slq4SEVMQS
lnF+wWnrhBE/0EcGyepXpQ+EwoNGe3vG04LS2pUPOZswetQrgTYAUFrvgicnvhiw
MQIDAQAB
-----END PUBLIC KEY-----

Client code:

// Create a new JSEncrypt instance
const encryptor = new JSEncrypt();

// Set the server's public key
encryptor.setPublicKey(server_pub_key);

// Encrypt the JSON string using the encryptor
const encryptedCredentials = encryptor.encrypt(jsonCredentials);

console.log(encryptedCredentials)
@beshad
Copy link

beshad commented Jun 15, 2023

i also have the similar issue after i upgraded to latest version. now i get error message jsencrypt_1.JSEncrypt is not a constructor and encrypt() method returns string | boolean(false), any ideas?

@siawyoung
Copy link

Most probably the public key is invalid, for some reason. Double check that.

@pirqqs
Copy link

pirqqs commented Oct 19, 2023

The way to declare your public key is :

const publicKeyCasting =
  "-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDY0xIs5oSVl8dxCW4zDTtIdtfOVMjT8bI6l6tDb3BW6HSIAXqEsp6wfeVHJ+7LHFDGSn4TrURpL6FFKHPz7EVzZrcZjNU+jn8l2W9hDXnFc/v3qR3SRXuG71ZUiCP3g8j1SdY+sB0Yn0046PdFiWrIX8qiqMpNX2fHiJH5uq71/QIDAQAB-----END PUBLIC KEY-----"

@coryryan269
Copy link

coryryan269 commented Sep 13, 2024

Same problem here. Tried every variation of encoding, preceding key with the "-----BEGIN PUBLIC KEY-----" and following with "-----END PUBLIC KEY-----"... it all results in false.

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

5 participants