So I am making an application that basically creates strings that must be encrypted before they are stored on a user's device. If the user blindly starts running the application without creating a password before hand OR they cannot come up with a long, strong password, would this method be appropriate? (Language is java)

  1. Create a probable prime of 512 bit length
    • BigInteger.probablePrime(512, new Random())
  2. Add a random number of 512 bit length
    • .add(new BigInteger(512, new Random()))
  3. Convert number into Base32
    • .toString(32);

The above steps will yield a Base32 string that is about 100-103 characters long and will look something like this: 2si95285qk2trk65hona7q0j27o81ph6adjs2obd2g83q9nho52tonku7s3uajtja54ri2dkbque39hil14f1nrlef0736mtda7rtll

And it is up to the user to safely store this password if they do not create one themselves.

So my question, is this a plausible method to create a random password if the user does not make one before hand or cannot create a password this long (though it is not required)?

  • 1
    $\begingroup$ You depend heavily on the quality of the initialization of Random(), which itself depends on the particular version of Java, and on the platform. Thus the answer is context-dependent (and largely off-topic). $\endgroup$
    – fgrieu
    Apr 8, 2015 at 19:16
  • 2
    $\begingroup$ Generating cryptographically strong random numbers is not hard. And a base64-encoded 128-bit random symmetric key is only 44 characters long. What do you believe you gain by performing voodoo with prime numbers? $\endgroup$ Apr 8, 2015 at 19:51
  • 1
    $\begingroup$ Is there a specific reason you are starting with a prime number? Is there a reason why a random number of the same size wouldn't work as well? Also, I second fgrieu's question about Random() -- should you trust it to generate cryptographical keys? $\endgroup$
    – poncho
    Apr 8, 2015 at 20:13
  • $\begingroup$ @Stephen I used your link and came up with new BigInteger(512, new SecureRandom()).toString(32). I thought choosing a random prime number would help in my case of randomness but from what everyone is saying, it is rather pointless. $\endgroup$
    – rdadkins
    Apr 8, 2015 at 20:19
  • 1
    $\begingroup$ Why not use something like the Diceware 8k list to generate a passphrase for the user, with SecureRandom? That way you get a secure passphrase they might actually be able to keep, instead of changing it for an (almost certainly weaker) custom password. $\endgroup$ Apr 8, 2015 at 23:58

1 Answer 1


If you just need random numbers, there's no point in generating random primes. Just make sure that you're using a cryptographically secure random number generator, properly seeded from a secure entropy source.

Also, passwords made up of random letters and numbers are very hard to remember (and type). For a password meant to be memorized by a human, it's much more efficient (in terms of security vs. effort) to generate a passphrase made out of random words instead. See diceware and this xkcd strip for some well known examples.

Also, 100 random base-32 characters equals 500 bits of entropy. That's ridiculous for a password; 128 bits of entropy is plenty for anything, and you're probably OK with 80 bits or even less, depending on what kind of threats you're expecting. If your key derivation function does proper key stretching, that'll add some 10 to 30 bits of effective strength to your password (depending on the iteration count), allowing you to reduce its length even further.

A typical randomly chosen word has about 10 to 14 bits of entropy, depending on the size of the word list (a diceware passphrase has just under 13 bits of entropy per word), so generating a passphrase of five or six random words and using a key-stretching KDF should be safe enough.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.