Hi:<br><br>I can'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 "=====AES 256 Demo====="<br>PWD=""<br>Initial16bytes='0123456789ABCDEF'<br><br>a=0<br>for a in xrange(0,32):<br> b=hex(random.randint(1,16)-1)<br> PWD+=b.replace("0x","")<br>
print "AES-key",PWD<br>crypt = AES.new(PWD,AES.MODE_CBC,Initial16bytes)<br><br>plain="blabla what the hack blabla."<br>restbyte = 32-len(plain)%32<br>temp=""<br>a=0<br>for a in xrange(restbyte):<br>
temp+=" "<br><br>plain+=temp<br>print "text: \n",plain<br>c= crypt.encrypt(plain)<br>print "encrypted text: "<br>print c.encode("hex")<br>crypt = AES.new(PWD,AES.MODE_CBC,Initial16bytes)<br>
print "decrypted text: \n", crypt.decrypt(c)<br>#################### RSA<br>print "\n=====RSA 368 Demo====="<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,"")<br>
enc_PWD = publickeyClient.encrypt(PWD, "")<br>print "with publickeyClient encrypted AES-PWD:"<br>print enc_PWD<br>print "with privatekeyCMS signed AES-PWD:"<br>print signed_PWD<br><br><br>dec_PWD= privatekeyClient.decrypt(enc_PWD[0])<br>
print "identity check:\n",publickeyCMS.verify(dec_PWD,signed_PWD)<br>print "decrypted PWD:\n",dec_PWD<br><br><br>#################### ELGAMAL<br>K=""<br>a=0<br>for a in xrange(0,16):<br> b=hex(random.randint(1,16)-1)<br>
K+=b.replace("0x","")<br><br><br><br>print "\n=====ELGamal 368 Demo====="<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 "with publickeyClient encrypted AES-PWD:"<br>print enc_PWD<br>print "with privatekeyCMS signed AES-PWD:"<br>
print signed_PWD<br><br><br>dec_PWD= privatekeyClient.decrypt(enc_PWD)<br>print "identity check:\n",publickeyCMS.verify(dec_PWD,signed_PWD)<br>print "decrypted PWD:\n",dec_PWD<br><br>#################### DSA only sign<br>
K=""<br>a=0<br>for a in xrange(0,16):<br> b=hex(random.randint(1,16)-1)<br> K+=b.replace("0x","")<br><br>print "\n=====DSA 368 Demo====="<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 "identity check:\n",publickeyCMS.verify(dec_PWD,signed_PWD)<br>
print "decrypted PWD from ELGAMAL:\n",dec_PWD<br></pre><br>