kjur / jsrsasign

The 'jsrsasign' (RSA-Sign JavaScript Library) is an opensource free cryptography library supporting RSA/RSAPSS/ECDSA/DSA signing/validation, ASN.1, PKCS#1/5/8 private/public key, X.509 certificate, CRL, OCSP, CMS SignedData, TimeStamp, CAdES and JSON Web Signature/Token in pure JavaScript.
https://kjur.github.io/jsrsasign
Other
3.27k stars 643 forks source link

KEYUTIL.generateKeypair creates two public keys #573

Closed ericmelvin closed 1 year ago

ericmelvin commented 1 year ago

Running generateKeypair seems to create two public keys.

// Generate private/public key pair var rsaKeypair = KEYUTIL.generateKeypair("RSA", 1024);

// Output private key console.log(KEYUTIL.getPEM(rsaKeypair.prvKeyObj));

-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCQKrpju2waIQ+oZEke2kI5Tt1i aPPmAwOjxXLyUKoi+qNWrTNQL+6/1/SDFHeBoDT4I7Ann6TFDYAkqLSSjKFYXoBK n3P+e5RAaTzSsUBncqko9Ytqr0vED3Wn+ZYkjmEMEmIJhyYMTD/Wkk3tVrNC9ZdC I4HpblSqdGb6pnEm1wIDAQAB -----END PUBLIC KEY-----

// Output public key console.log(KEYUTIL.getPEM(rsaKeypair.pubKeyObj));

-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCQKrpju2waIQ+oZEke2kI5Tt1i aPPmAwOjxXLyUKoi+qNWrTNQL+6/1/SDFHeBoDT4I7Ann6TFDYAkqLSSjKFYXoBK n3P+e5RAaTzSsUBncqko9Ytqr0vED3Wn+ZYkjmEMEmIJhyYMTD/Wkk3tVrNC9ZdC I4HpblSqdGb6pnEm1wIDAQAB -----END PUBLIC KEY-----

prvKeyObj shows true for public key and private key: prvKeyObj: Object { e: 65537, isPrivate: true, isPublic: true, … }

pubKeyObj shows (correct?) bools for isPublic and isPrivate: pubKeyObj: Object { e: 65537, isPublic: true, isPrivate: false, … } ​​

kjur commented 1 year ago

Please specify an optional argument "PKCS1PRV" or "PKCS8PRV" explicitly for getPEM with private key:

KEYUTIL.getPEM(rsaKeyPair.prvKeyObj, "PKCS1PRV")
// or
KEYUTIL.getPEM(rsaKeyPair.prvKeyObj, "PKCS8PRV")