How can I in perl, efficiently (fast) generate a RSA keypair, with custom random data? The function should be like that, if you supply the same random data over, and over and over again, it should generate the same keypair over and over and over again. I tried with this:
#!/usr/bin/perl use CGI ':standard'; use Digest::SHA qw(sha512); use Crypt::OpenSSL::Random; use Crypt::OpenSSL::RSA; $passphrase = param('genpass'); $prngdata = ""; print "Content-Type: text/html\n\n"; if ($passphrase) { Crypt::OpenSSL::Random::random_seed(&genrsarandom(1024)); Crypt::OpenSSL::RSA->import_random_seed(); $rsa = Crypt::OpenSSL::RSA->generate_key(1024); print "PRNG data length: ". length($prngdata)."<br>"; print "PRNG data in hex: " . uc(unpack("H*", $prngdata)) . "<br><br>"; $pubkey = $rsa->get_public_key_string(); $privkey = $rsa->get_private_key_string(); $pubkey =~ s/\n/<br>/sgi; $privkey =~ s/\n/<br>/sgi; print "Public key: <br>" . $pubkey . "<br><br>"; print "Private key: <br>" . $privkey; } else { print "<form action=\"RSAgen.cgi\">PassPhrase: <input type=\"text\" na +me=\"genpass\"><input type=\"submit\" value=\"genrsa\"></form>"; } #################################################### # CSPRNG BASED ON A PASSWORD # RETURNS SAME RANDOM DATA UPON SUPPLYING # THE SAME PASSWORD #################################################### sub genrsarandom() { $bytes = $_[0]; $randomdata = ""; $numberofrounds = int($bytes / 64) + 1; for ($i = 0; $i < $numberofrounds; $i++) { $randomdata = $randomdata . sha512($passphrase . sha512($i.$i)); } $randomdata = substr($randomdata, 0, $bytes); $prngdata = $randomdata; return $randomdata; }
But even if I supply the same password to the genrsarandom() function everytime (by using a global variable), the Crypt::OpenSSL::RSA function still generates different keypairs everytime. Have a look at it here:

http://www.sebn.us.to/old/RSAgen.cgi

Enter something in the textbox. It now generates a keypair, but the problem is that the keypair is different everytime, even if you supply the same password again. However, theres NO error in the genrsarandom() function since it returns the same random data everytime the same password is supplied. The system should return the same keypair everytime the same password is entered.

How can I make so the Crypt::OpenSSL::RSA in some way directly use my supplied random data for key generation?


In reply to Generate RSA keypair in perl efficently with custom PRNG by sebastiannielsen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.