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.28k stars 644 forks source link

Generate Invalid RSA PEM Format #628

Open emn178 opened 3 months ago

emn178 commented 3 months ago

Following code:

var kp = KEYUTIL.generateKeypair('RSA', 1024);
privateKey = KEYUTIL.getPEM(kp.prvKeyObj, 'PKCS5PRV', '12345678', 'AES-256-CBC');

Sometimes it has a blank line before -----END RSA PRIVATE KEY-----, for example

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,A441CCFC835FD714E8573D9ED0701F76

oFE+zAOFpbY4fdfZsZ8wxhsgoxkRWwlCLzavjOZTjBGGKNypzYMiinK0fyIt1Q0A
HVoNLm81A7/2kAwrcex3L/iimBZoAYFjBMu+FC0+L3UbegKJVUXhQe2nenz8tpeI
SwsBtRvAnbraPfYfL6xFUgyALH7+pYyinr0oQJ0Bvye+0tVKTJ0mgYIxOqakpL7p
P7gcoC89Lifqrr0oNfVKYxI8R7hLgPUGGY7NYMy/NHkJY06dDPWMTY43nrykZ3+m
rdFJk/cwCWS84QwkZPLdEGog7FPTwLFJmjwnt2UnlICr7xb6PinUad74OoincRHf
5SBRxU7a56E7LHHgFJefxaORXBMym0EXw9+GzBmAu/hKblTkZImRgg+LS6NXvRBV
6X8KLp2s05/OylYMF4u9i/wL2SfT3soycbwx8hMsJK3aaLyUr0rOFmqWNcJ8bozw
YTbwWAatTkJkzaXvhZNFFzmLv6ppoZMTt8VhLXHbuJ+7b9FYOgYgZZmoudqUlrNO
RJPcONqkY0kWKUaCfbOViyhue9BTiQ6uqqaf7xgriAUn1h1SGs+1BetAPrrgdmV6
GQAHev882AuSE39bTb5f2wc1UQFHiOXvI+n/P0bWpWMPy28ydRDeK7IabXXeVjid
ODKPWhJpm90xbmOTDsrJ+y/8v/P0Rp66k8OpzeF76cH7bKQWvCShlxqyk/NIJCb1
S0lQxMSnPU7w9GgPx90NZR57L/wqd7PMt3EJjlpEzfm2jJZnu3n+aH8Pxlny6reG
/uODpwU8KLBu/RBj0MrYksGvUKmDHdi7jcI7BHCGE0NQul8XK36zyL9Efqgu4IG0

-----END RSA PRIVATE KEY-----

When you use it in openssl, it will return error

$ openssl rsa -in priv-key.pem -pubout > pub-key.pem
unable to load Private Key
8525234496:error:0908F066:PEM routines:get_header_and_data:bad end line:crypto/pem/pem_lib.c:841: