Hello,<br>I'm trying to import/export keys from/to <a href="http://pear.php.net/package/Crypt_RSA">Crypt_RSA</a>, using <a href="http://www.dlitz.net/software/pycrypto/">PyCrypto</a>. My problem is that while using PyCrypto to generate both public and private keys, the e(exponent?) is always the same.<br>
According to this site: <a href="http://pajhome.org.uk/crypt/rsa/rsa.html">http://pajhome.org.uk/crypt/rsa/rsa.html</a>, and yet others, the e(exponent?) is used for the public key, and d for the private key.<br>
<table class="lined" align="center" cellspacing="0"><tbody><tr><td width="50%">
<h3 style="text-align: center;">Public Key</h3>
<p><span class="math">n = 133<br>
e = 5</span></p>
</td>
<td width="50%">
<h3 style="text-align: center;">Secret Key</h3>
<p><span class="math">n = 133<br>
d = 65</span></p></td></tr></tbody></table><br>With Crypt_RSA the e(exponent!) does change. And, currently, Crypt_RSA 'should' import a key with:<br><pre><font color="blue">function </font><a href="http://pear.php.net/package/Crypt_RSA/docs/latest/Crypt_RSA/Crypt_RSA_Key.html#methodCrypt_RSA_Key" target="_blank">Crypt_RSA_Key</a><strong>(</strong><strong>$modulus</strong><strong>, </strong><strong>$exp</strong><strong>, </strong><strong>$key_type</strong><strong>, </strong><strong>$wrapper_name </strong>= <span>'default'</span><strong>, </strong><strong>$error_handler </strong>= <span>''</span><strong>)</strong></pre>
<br>I've made some examples to find out why it's no working, I couldn't find anything besides what I'm telling here, I'm getting:<br><pre><strong></strong></pre>I'm getting:<br><br><b>php output:</b><br>
public:<br>e = 576770076438170600293020230006<div id=":gv" class="ArwC7c ckChnd">11111558980619493053522755045300999066282094579<br>n = 8181236468479469953170512321145304204212043012891758446155743461774497958816815322484398388795674807728198188380330393972375008693925124720339387183472981<br>
<br>private:<br>e = 437180361711592053741873121324365600858808727935298705094471030803249044470609246732980373383618106117311336729585366532974229499133803473168583498751419<br>n = 8181236468479469953170512321145304204212043012891758446155743461774497958816815322484398388795674807728198188380330393972375008693925124720339387183472981<br>
<br>plain text: This is the secret phrase testing.<br>key: YTozOntpOjA7czo2NDoiVSFbfK0ZrvVUOUo/EejHR7YO4F8ugtRGrDPSjrTZEMYZqLUuiZhNhxr2ZdSyvoBN/2wfVnYJzqqYrYT/BA01nCI7aToxO3M6MzI6IvPv6+fj39vXU1DMyMRAvbk1sq4qpyOgHJkVkg6LB4R/IjtpOjI7czo2OiJwdWJsaWMiO30=<br>
encrypted text: mBL6f3pRyFX34nB/0H5jL3VOwylAjJzGx32UKS11EOhtooAVjdiYZHGh5jUOAUWj3GD9+ccPPyh5afXmGrq3UA==<br>decrypted text: This is the secret phrase testing.<br><br><b>python output:</b><br>privkey<br> n=6725954312330247844957820045120819261330667638399165421086669714474128366894973621051940873915934423533814876921018123156635212781400913635892080927069649<br>
e=65537<br> d=4335534734949590616144210259946732528112578457728805761841499642766064251636039038235790562167224376876190746808970727821310520543898873435686861965120773<br> p=67052724006509355618909096968505962144329319327659728670702224750645922227811<br>
q=100308442527663820502897858910213458115962396303231005493840992047817473580859<br> u=49237107846093262220122150056829317849796914810691525815572680169759496162772<br><br>pubkey<br> n=6725954312330247844957820045120819261330667638399165421086669714474128366894973621051940873915934423533814876921018123156635212781400913635892080927069649<br>
e=65537<br><br>msg= This is the secret phrase testing.<br>data= Sh'fâªaÓÏ!_ÍTÊÅ\~1rA²µG!ðbS(e¨®e{]Èî¬<PÍL³,$h<br>strdata= ("Sh'f\x12\xe2\xaa\x82a\xd3\xcf\x98!_\xcd\x94T\xca\x05\xc5\\~\x7f1r\x95\x9eA\xb2\xb5G!\x15\xf0bS(e\x94\xa8\x90\xaee\x01{]\xc8\xee\xac<\x07P\xcd\x87\x8eL\x84\x8e\x8f\xb3,\x9a$h",)<br>
decrypted= This is the secret phrase testing.<br>
</div><br>here are the sources:<br><b>cryp.py<br></b>from Crypto.PublicKey import RSA<br>from Crypto.Util.randpool import RandomPool<br>import os<br>rpool = RandomPool()<br><br># Generate keys<br>print os.urandom(0);<br>
privkeyA = RSA.generate(512, rpool.get_bytes)<br>pubkeyA = privkeyA.publickey()<br><br>msg = 'This is the secret phrase testing.'<br>msgc = pubkeyA.encrypt(msg, '')<br>msgf = privkeyA.decrypt(msgc)<br><br>
print 'privkey\n\tn=%s\n\te=%s\n\td=%s\n\tp=%s\n\tq=%s\n\tu=%s\n' % (privkeyA.n, privkeyA.e, privkeyA.d, privkeyA.p, privkeyA.q, privkeyA.u)<br>print 'pubkey\n\tn=%s\n\te=%s\n' % (pubkeyA.n, pubkeyA.e)<br>
print 'msg=\t\t%s\ndata=\t\t%s\nstrdata=\t%s\ndecrypted=\t%s\n' % (msg, msgc[0],str(msgc),msgf)<br><br><b>cryp.php<br></b><?php<br> require_once 'Crypt/RSA.php';<br> require_once 'Crypt/RSA/MathLoader.php';<br>
<br> //Generates the pair keys<br> function generate_key_pair()<br> {<br> global $public_key,$private_key;<br> $obj = &Crypt_RSA_MathLoader::loadWrapper('default');<br><br> $key_pair = new Crypt_RSA_KeyPair(512);<br>
<br> //Returns public key from the pair<br> $public_key = $key_pair->getPublicKey();<br> echo "public:\ne = ".$obj->bin2int($public_key->getExponent())."\nn = ".$obj->bin2int($public_key->getModulus())."\n\n";<br>
<br> //Returns private key from the pair<br> $private_key = $key_pair->getPrivateKey();<br> echo "private:\ne = ".$obj->bin2int($private_key->getExponent())."\nn = ".$obj->bin2int($private_key->getModulus())."\n\n";<br>
}<br><br> //Check runtime errors<br> function check_error(&$obj)<br> {<br> if (PEAR::isError($obj)) {<br> echo "error: ", $obj->getMessage(), "\n";<br> }<br>
if ($obj->isError()){<br> $error = $obj->getLastError();<br> switch ($error->getCode()) {<br> case CRYPT_RSA_ERROR_WRONG_TAIL :<br> // nothing to do<br> break;<br>
default:<br> // echo error message and exit<br> echo 'error: ', $error->getMessage();<br> exit;<br> }<br> }<br> }<br><br> generate_key_pair();<br> $plain_text = 'This is the secret phrase testing.';<br>
echo "plain text:\t\t".$plain_text."\n";<br><br> //get string represenation of the public key<br> $key = Crypt_RSA_Key::fromString($public_key->toString()); <br> echo "key:\t\t\t".$public_key->toString()."\n";<br>
<br> $rsa_obj = new Crypt_RSA;<br> check_error($rsa_obj);<br> <br> //Ecnrypts $plain_text by the key $key.<br> $encrypted = $rsa_obj->encrypt($plain_text, $key);<br><br> $enc_text = $encrypted;<br>
echo "encrypted text:\t\t".$enc_text."\n";<br> <br> //Get string represenation of the private key<br> $key2 = Crypt_RSA_Key::fromString($private_key->toString());<br> check_error($key2);<br>
<br> //Check encrypting/decrypting function's behaviour<br> $rsa_obj->setParams(array('dec_key' => $key2));<br> check_error($rsa_obj);<br><br> //Decrypts $enc_text<br> $decrypted = $rsa_obj->decrypt($enc_text);<br>
echo "decrypted text:\t\t".$decrypted."\n";<br><br>?><br><br>Thanks in advance.<br>