Hi:<br><br>I can&#39;t find any example for DSA or ELGAMAL(with google).<br>so i wrote a simple example.<br><br><a href="http://www.jabbertor.de/wp-content/uploads/2009/08/pycryptotest.txt">http://www.jabbertor.de/wp-content/uploads/2009/08/pycryptotest.txt</a><br>
<br>Could someone help to check this?<br>maybe some Hint about security or perfoumance?<br>The script is not commented, sry for that<br><br><pre>#!/usr/bin/env python<br>from Crypto.Cipher import AES<br>import os,sys,random<br>
#####################AES<br>print &quot;=====AES 256 Demo=====&quot;<br>PWD=&quot;&quot;<br>Initial16bytes=&#39;0123456789ABCDEF&#39;<br><br>a=0<br>for a in xrange(0,32):<br>    b=hex(random.randint(1,16)-1)<br>    PWD+=b.replace(&quot;0x&quot;,&quot;&quot;)<br>
print &quot;AES-key&quot;,PWD<br>crypt = AES.new(PWD,AES.MODE_CBC,Initial16bytes)<br><br>plain=&quot;blabla what the hack blabla.&quot;<br>restbyte = 32-len(plain)%32<br>temp=&quot;&quot;<br>a=0<br>for a in xrange(restbyte):<br>
    temp+=&quot; &quot;<br><br>plain+=temp<br>print &quot;text: \n&quot;,plain<br>c= crypt.encrypt(plain)<br>print &quot;encrypted text: &quot;<br>print c.encode(&quot;hex&quot;)<br>crypt = AES.new(PWD,AES.MODE_CBC,Initial16bytes)<br>
print &quot;decrypted text: \n&quot;, crypt.decrypt(c)<br>#################### RSA<br>print &quot;\n=====RSA 368 Demo=====&quot;<br>from Crypto.PublicKey import RSA<br>from Crypto.Util.randpool import RandomPool<br>rpool = RandomPool()<br>
<br>privatekeyCMS = RSA.generate(368, rpool.get_bytes)<br>privatekeyClient = RSA.generate(368, rpool.get_bytes)<br>publickeyCMS = privatekeyCMS.publickey()<br>publickeyClient = privatekeyClient.publickey()<br><br>signed_PWD = privatekeyCMS.sign(PWD,&quot;&quot;)<br>
enc_PWD = publickeyClient.encrypt(PWD, &quot;&quot;)<br>print &quot;with publickeyClient encrypted AES-PWD:&quot;<br>print enc_PWD<br>print &quot;with privatekeyCMS signed AES-PWD:&quot;<br>print signed_PWD<br><br><br>dec_PWD= privatekeyClient.decrypt(enc_PWD[0])<br>
print &quot;identity check:\n&quot;,publickeyCMS.verify(dec_PWD,signed_PWD)<br>print &quot;decrypted PWD:\n&quot;,dec_PWD<br><br><br>#################### ELGAMAL<br>K=&quot;&quot;<br>a=0<br>for a in xrange(0,16):<br>    b=hex(random.randint(1,16)-1)<br>
    K+=b.replace(&quot;0x&quot;,&quot;&quot;)<br><br><br><br>print &quot;\n=====ELGamal 368 Demo=====&quot;<br>from Crypto.PublicKey import ElGamal<br>from Crypto.Util.randpool import RandomPool<br>rpool = RandomPool()<br>
<br>privatekeyCMS = ElGamal.generate(368, rpool.get_bytes)<br>privatekeyClient = ElGamal.generate(368, rpool.get_bytes)<br>publickeyCMS = privatekeyCMS.publickey()<br>publickeyClient = privatekeyClient.publickey()<br><br>
<br>enc_PWD = publickeyClient.encrypt(PWD, K)<br>print privatekeyCMS.can_sign()<br>signed_PWD = privatekeyCMS.sign(PWD,97)<br>print &quot;with publickeyClient encrypted AES-PWD:&quot;<br>print enc_PWD<br>print &quot;with privatekeyCMS signed AES-PWD:&quot;<br>
print signed_PWD<br><br><br>dec_PWD= privatekeyClient.decrypt(enc_PWD)<br>print &quot;identity check:\n&quot;,publickeyCMS.verify(dec_PWD,signed_PWD)<br>print &quot;decrypted PWD:\n&quot;,dec_PWD<br><br>#################### DSA only sign<br>
K=&quot;&quot;<br>a=0<br>for a in xrange(0,16):<br>    b=hex(random.randint(1,16)-1)<br>    K+=b.replace(&quot;0x&quot;,&quot;&quot;)<br><br>print &quot;\n=====DSA 368 Demo=====&quot;<br>from Crypto.PublicKey import DSA<br>
rpool = RandomPool()<br><br>privatekeyCMS = DSA.generate(368, rpool.get_bytes)<br>publickeyCMS = privatekeyCMS.publickey()<br>signed_PWD = privatekeyCMS.sign(PWD,K)<br>print &quot;identity check:\n&quot;,publickeyCMS.verify(dec_PWD,signed_PWD)<br>
print &quot;decrypted PWD from ELGAMAL:\n&quot;,dec_PWD<br></pre><br>