15

Is there a way to generate both private and public keys using javascript?

And I need to those key in a database.

-----BEGIN RSA PRIVATE KEY----- MIICXQIBAAKBgQDlOJu6TyygqxfWT7eLtGDwajtNFOb9I5XRb6khyfD1Yt3YiCgQ WMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76xFxdU6jE0NQ+Z+zEdhUTooNR aY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4gwQco1KRMDSmXSMkDwIDAQAB AoGAfY9LpnuWK5Bs50UVep5c93SJdUi82u7yMx4iHFMc/Z2hfenfYEzu+57fI4fv xTQ//5DbzRR/XKb8ulNv6+CHyPF31xk7YOBfkGI8qjLoq06V+FyBfDSwL8KbLyeH m7KUZnLNQbk8yGLzB3iYKkRHlmUanQGaNMIJziWOkN+N9dECQQD0ONYRNZeuM8zd 8XJTSdcIX4a3gy3GGCJxOzv16XHxD03GW6UNLmfPwenKu+cdrQeaqEixrCejXdAF z/7+BSMpAkEA8EaSOeP5Xr3ZrbiKzi6TGMwHMvC7HdJxaBJbVRfApFrE0/mPwmP5 rN7QwjrMY+0+AbXcm8mRQyQ1+IGEembsdwJBAN6az8Rv7QnD/YBvi52POIlRSSIM V7SwWvSK4WSMnGb1ZBbhgdg57DXaspcwHsFV7hByQ5BvMtIduHcT14ECfcECQATe aTgjFnqE/lQ22Rk0eGaYO80cc643BXVGafNfd9fcvwBMnk0iGX0XRsOozVt5Azil psLBYuApa66NcVHJpCECQQDTjI2AQhFc1yRnCU/YgDnSpJVm1nASoRUnU8Jfm3Oz uku7JUXcVpt08DFSceCEX9unCuMcT72rAQlLpdZir876 -----END RSA PRIVATE KEY-----

-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlOJu6TyygqxfWT7eLtGDwajtN FOb9I5XRb6khyfD1Yt3YiCgQWMNW649887VGJiGr/L5i2osbl8C9+WJTeucF+S76 xFxdU6jE0NQ+Z+zEdhUTooNRaY5nZiu5PgDB0ED/ZKBUSLKL7eibMxZtMlUDHjm4 gwQco1KRMDSmXSMkDwIDAQAB -----END PUBLIC KEY-----

Thanks!

Guilherme GM
  • 567
  • 2
  • 5
  • 15

2 Answers2

7

Check out http://wwwtyro.github.io/cryptico/

If you are saving the keys to a database anyway it could be worth your while looking at doing this server side using something like nodejs

Have a look here https://github.com/juliangruber/keypair that library hasn't been updated in some time so it may be worth a search.

David Cusack
  • 146
  • 3
  • It's on the cliente side. Is there a way to get the private key using cryptico to store it in a database? – Guilherme GM Jul 01 '16 at 23:59
  • All database storage has to be done server side. If you generate something client side it has to be passed to the server and the web server code would then pass it to the database for storage. Have a google of these: PHP NodeJS MongoDB MariaDB If you have never written server side code there are plenty of youtube tutorials out there, "Derek Banas" and "The New Boston" are a couple. – David Cusack Jul 02 '16 at 00:07
  • Do not use cryptico for any purpose. According to [this issue](https://github.com/wwwtyro/cryptico/issues/20) that's been open since 2016, cryptico apparently replaces the global random number generator for Javascript with a deterministic, seedable PRNG. That's a major security issue. cryptico is also a dead project as it was archived on GitHub since August 2022. – CubicleSoft May 11 '23 at 17:14
7

this question is 4 years old but I found it well before this answer below, I don't think the Web Crypto API existed at the time. it can do RSASSA-PKCS1-v1_5, RSA-PSS, RSA-OAEP, ECDSA, or ECDH. key pairs on client side https://developer.mozilla.org/en-US/docs/Web/API/CryptoKeyPair

SubtleCrypto.generateKey()
SubtleCrypto.deriveKey(); 
cubesareneat
  • 302
  • 2
  • 14
  • 1
    Relevant MDN page with code examples: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey – tlgs Feb 24 '22 at 19:54
  • 1
    Works great, when the page is using SSL. otherwise, cryptico.js is your solution. Very much recommend SSL, though.. – thphoenix May 10 '22 at 15:32