rsa-keygen.html
1d90667889fea3ff54bdfd78afd95923ceca86decf8828ad533dd820acc54d8f
<HTML>
<HEAD>
<TITLE>Generating RSA keys</TITLE>
</HEAD>
<BODY>
<H1>Generating RSA keys</H1>
Some C code kindly donated by Aggelos Keromitis
<A HREF="mailto:kermit@forthnet.gr"><kermit@forthnet.gr></A>
<HR>
<UL>
<LI> <A HREF="rsakg.c">rsakg.c</A> source listing
<LI> and the gmp-1.3.2.tar.gz GNU mp library (required, get from
gnu distribution site).
</UL>
<HR>
<H2>Compiling the gmp library</H2>
This is real easy to install just do:
<PRE>
% gzip -d gmp-1.3.2.tar.gz | tar xf
% cd gmp-1.3.2
% make
</PRE>
Thats it!
<H2>Compiling Aggelos rsakg.c</H2>
Copy the <A HREF="rsakg.c">rsakg.c</A> file into the gmp-1.3.2 directory.
<PRE>
% cd gmp-1.3.2
% cc -o rsakg rsakg.c -L./ -lgmp
</PRE>
Copy your rsakg binary to where ever you keep your binaries and add/or
it your search path.
<H2>Using Aggelos rsakg</H2>
It now takes key sizes in bits so to generate a 512 bit key you would
do:
<PRE>
% rsakg 512
</PRE>
It will generate 2 files pubkey.rsa and seckey.rsa (may take it a
minute or so depending on the speed of your machine) it should say:
<PRE>
Got one prime.
Got second prime.
written public key to "pubkey.rsa"
written secret key to "seckey.rsa"
</PRE>
as it runs.
<P>
then take a look at "pubkey.rsa":
<PRE>
e = 7f82b84bde3432563ea37745f9a01d859e41f0fb7eb9cf781f6f204266ec55087aaaa60dc4df6e50266b1957031515a08813fa0f1ee6aa377d915e352958eb5
n = ca870534aca46382c1f41530e526af108dcd6bf3d0a3e200b51c1844e315b5a73622b6129ffd9d2e299256a33f922961a702de46c584fc141204f74d8065b8d
</PRE>
and the "seckey.rsa":
<PRE>
d = 3fd15c1ec184d27bb5e692608b2526c627f5149ad269a443ba72e39ab0fd4114dbaceee8c385e64edbfd215d45267de6a81bf0f17b55abb293617a2910b602d
n = ca870534aca46382c1f41530e526af108dcd6bf3d0a3e200b51c1844e315b5a73622b6129ffd9d2e299256a33f922961a702de46c584fc141204f74d8065b8d
</PRE>
<P>
The e (public key exponent) and n (RSA modulus) numbers are your public key.
<P>
And the d (secret key exponent) number is your secret key, dont show
anyone your d number!
<P>
Okay to use with perl-rsa you just use the numbers like this (msg
being some data file you want to encrypt we encrypt the message
"squeamish ossifrage" here):
<PRE>
% echo squeamish ossifrage > msg
% rsa -k=[e] -n=[n] < msg > msg.rsa
</PRE>
Then to decrypt (to the screen):
<PRE>
% rsa -d -k=[d] -n=[n] < msg.rsa
</PRE>
Where [e] [n] and [d] are the huge numbers in seckey.rsa and
pubkey.rsa with those names, ie so you can cut and paste these into
your shell:
<p>
encrypt:
<PRE>
% echo squeamish ossifrage > msg
% rsa -k=7f82b84bde3432563ea37745f9a01d859e41f0fb7eb9cf781f6f204266ec55087aaaa60dc4df6e50266b1957031515a08813fa0f1ee6aa377d915e352958eb5 -n=ca870534aca46382c1f41530e526af108dcd6bf3d0a3e200b51c1844e315b5a73622b6129ffd9d2e299256a33f922961a702de46c584fc141204f74d8065b8d < msg > msg.rsa
</PRE>
<P>
decrypt:
<PRE>
% rsa -d -k=3fd15c1ec184d27bb5e692608b2526c627f5149ad269a443ba72e39ab0fd4114dbaceee8c385e64edbfd215d45267de6a81bf0f17b55abb293617a2910b602d -n=ca870534aca46382c1f41530e526af108dcd6bf3d0a3e200b51c1844e315b5a73622b6129ffd9d2e299256a33f922961a702de46c584fc141204f74d8065b8d < msg.rsa
</PRE>
<H2>Choosing small public key exponents</H2>
You may have noticed that some of the examples use a small number for
the public key exponent, PGP can do this because of the way it
combines RSA with IDEA but if you are interested in security you
shouldn't use small public exponents with pure RSA as it weakens the
security somewhat. If you want to do it anyway, just to play with the
numbers, or if you know what you're doing and are combining it with
something else then you can generate them like this.
<P>
Just give rsakg a second argument which is your choice of public
exponent. (This is usally quite small: PGP typically chooses 17 or
19, but you could choose 3 or 5 or whatever it's still going to work)
<PRE>
% rsakg 512 17
</PRE>
<HR>
<EM>
Comments, html bugs to me
(<A HREF="http://www.dcs.ex.ac.uk/~aba/">Adam Back</A>) at
<A HREF="mailto:aba@dcs.ex.ac.uk"><aba@dcs.ex.ac.uk></A>
</EM>
</BODY>
</HTML>