title: How can I create a key pair in Java, based on a password?
body: <p>I want to allow Alice to create a public/private key pair so that Bob can send her confidential messages.  However, I want Alice to be able to check her messages from anywhere, and it would be a pain for her to have to carry around a memory stick containing her private key.  Is there some way that Alice can create a public/private key pair based on a password which she remembers?  In this way she could simply generate the private key (and public key) whenever she wanted to.</p>
  
  <p>The short version of this question is: Where can I find the Java equivalent of <a href="http://cryptico.wwwtyro.net/" rel="nofollow noreferrer">cryptico.js</a>.</p>
  
  <p>Also, <a href="https://stackoverflow.com/questions/11156275/generate-rsa-key-pair-in-javascript-based-on-a-password">here's the same question</a> on Stack Overflow, but for javascript.</p>
  
  <p>Edit: Here's my first attempt at a solution:</p>
  
  <pre><code>    SecureRandom saltRand = new SecureRandom(new byte[] { 1, 2, 3, 4 });
      byte[] salt = new byte[16];
      saltRand.nextBytes(salt);
  
      int keyLength = 3248;
      SecretKeyFactory factory = SecretKeyFactory
              .getInstance("PBKDF2WithHmacSHA1");
      KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 8192, keyLength);
      SecretKey key = factory.generateSecret(spec);
  
      SecureRandom keyGenRand = SecureRandom.getInstance("SHA1PRNG");
      keyGenRand.setSeed(key.getEncoded());
  
      KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
      gen.initialize(keyLength, keyGenRand);
      java.security.KeyPair p = gen.generateKeyPair();
  </code></pre>