[pycrypto] example
avo ga
avogatro2007 at googlemail.com
Mon Aug 24 08:52:26 CST 2009
Hi:
I can't find any example for DSA or ELGAMAL(with google).
so i wrote a simple example.
http://www.jabbertor.de/wp-content/uploads/2009/08/pycryptotest.txt
Could someone help to check this?
maybe some Hint about security or perfoumance?
The script is not commented, sry for that
#!/usr/bin/env python
from Crypto.Cipher import AES
import os,sys,random
#####################AES
print "=====AES 256 Demo====="
PWD=""
Initial16bytes='0123456789ABCDEF'
a=0
for a in xrange(0,32):
b=hex(random.randint(1,16)-1)
PWD+=b.replace("0x","")
print "AES-key",PWD
crypt = AES.new(PWD,AES.MODE_CBC,Initial16bytes)
plain="blabla what the hack blabla."
restbyte = 32-len(plain)%32
temp=""
a=0
for a in xrange(restbyte):
temp+=" "
plain+=temp
print "text: \n",plain
c= crypt.encrypt(plain)
print "encrypted text: "
print c.encode("hex")
crypt = AES.new(PWD,AES.MODE_CBC,Initial16bytes)
print "decrypted text: \n", crypt.decrypt(c)
#################### RSA
print "\n=====RSA 368 Demo====="
from Crypto.PublicKey import RSA
from Crypto.Util.randpool import RandomPool
rpool = RandomPool()
privatekeyCMS = RSA.generate(368, rpool.get_bytes)
privatekeyClient = RSA.generate(368, rpool.get_bytes)
publickeyCMS = privatekeyCMS.publickey()
publickeyClient = privatekeyClient.publickey()
signed_PWD = privatekeyCMS.sign(PWD,"")
enc_PWD = publickeyClient.encrypt(PWD, "")
print "with publickeyClient encrypted AES-PWD:"
print enc_PWD
print "with privatekeyCMS signed AES-PWD:"
print signed_PWD
dec_PWD= privatekeyClient.decrypt(enc_PWD[0])
print "identity check:\n",publickeyCMS.verify(dec_PWD,signed_PWD)
print "decrypted PWD:\n",dec_PWD
#################### ELGAMAL
K=""
a=0
for a in xrange(0,16):
b=hex(random.randint(1,16)-1)
K+=b.replace("0x","")
print "\n=====ELGamal 368 Demo====="
from Crypto.PublicKey import ElGamal
from Crypto.Util.randpool import RandomPool
rpool = RandomPool()
privatekeyCMS = ElGamal.generate(368, rpool.get_bytes)
privatekeyClient = ElGamal.generate(368, rpool.get_bytes)
publickeyCMS = privatekeyCMS.publickey()
publickeyClient = privatekeyClient.publickey()
enc_PWD = publickeyClient.encrypt(PWD, K)
print privatekeyCMS.can_sign()
signed_PWD = privatekeyCMS.sign(PWD,97)
print "with publickeyClient encrypted AES-PWD:"
print enc_PWD
print "with privatekeyCMS signed AES-PWD:"
print signed_PWD
dec_PWD= privatekeyClient.decrypt(enc_PWD)
print "identity check:\n",publickeyCMS.verify(dec_PWD,signed_PWD)
print "decrypted PWD:\n",dec_PWD
#################### DSA only sign
K=""
a=0
for a in xrange(0,16):
b=hex(random.randint(1,16)-1)
K+=b.replace("0x","")
print "\n=====DSA 368 Demo====="
from Crypto.PublicKey import DSA
rpool = RandomPool()
privatekeyCMS = DSA.generate(368, rpool.get_bytes)
publickeyCMS = privatekeyCMS.publickey()
signed_PWD = privatekeyCMS.sign(PWD,K)
print "identity check:\n",publickeyCMS.verify(dec_PWD,signed_PWD)
print "decrypted PWD from ELGAMAL:\n",dec_PWD
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.dlitz.net/pipermail/pycrypto/attachments/20090824/e0fe2c33/attachment.htm
More information about the pycrypto
mailing list