#102·jsencrypt

Unhelpful error message when trying to get a private key from a public one

Author: pmaxhoganCreated Apr 20, 2017Updated Jun 12, 2026
javascript
var private = new JSEncrypt();
var public = new JSEncrypt();
public.setPrivateKey(decrypt.getPublicKey());
public.getPrivateKey();

results in

image

A weird error message for "it is impossible to get a private key from a public one". This would be better:

image

Just run:

javascript
throw new TypeError("Cannot get a private key from a public one.");