[pycrypto] example
avo ga
avogatro2007 at googlemail.com
Tue Aug 25 16:38:03 CST 2009
Hi:
In this new demo
i use randint() and Random.new().read() from the new Crypto.random module.
DSA use now 512 bit key at least.
and i use sha512 for DSA.
DSA only verifty the sha512 hash of the original AES password.
about rsa:
Dwayne C. Litzenberger said something about OAEP for RSA.
this is a very critical issue, how can I use it.
Well, I feel now like child in a mine field.
So please help me check this again.
Thank you
-----------------------------------------------------------------------------------
#!/usr/bin/env python
from Crypto.Cipher import AES
from Crypto.Util.number import GCD
from Crypto import Random
import os,sys
#####################AES ####################
print "=====AES 256 Demo====="
# use AES to encrypt the real message
# use the more secure Crypto.Random to generate PWD and Initialbyte/IV
# AES key is 32 byte or 16*hex_digit
# Initial16bytes:16 bytes or 8*hex_digit
PWD=""
rpool = Random.new()
Random.atfork()
PWD = rpool.read(16).encode("hex")
Initial16bytes=rpool.read(8).encode("hex")
print "AES-key:",PWD,"len:",len(PWD)
print "Initial16bytes:",Initial16bytes
crypt = AES.new(PWD,AES.MODE_CBC,Initial16bytes)
plain="sex drugs and crypto"
#block ciffre need string with lenth 16: add the restbyte to plain
restbyte =(16-len(plain)%16)%16
temp_string=""
for a in range(restbyte):
temp_string+=" "
plain+=temp_string
#encryption
print "\nplain text: \n",plain,"\n"
crypt_txt= crypt.encrypt(plain)
print "encrypted text: \n",crypt_txt.encode("hex"),"\n"
#decryption
crypt = AES.new(PWD,AES.MODE_CBC,Initial16bytes)
print "decrypted text: \n", crypt.decrypt(crypt_txt)
#################### RSA ####################
print "\n=====RSA 368 Demo====="
#use 1 RSA key to encrypt the AES key
#use another RSA key to sign AES key
from Crypto.PublicKey import RSA
#start the random generator
rpool = Random.new()
Random.atfork()
# generate both RSA keys,
privatekeyCMS = RSA.generate(368, rpool.read)
Random.atfork()
privatekeyClient = RSA.generate(368, rpool.read)
publickeyCMS = privatekeyCMS.publickey()
publickeyClient = privatekeyClient.publickey()
#sign the AES PWD with server private key
signed_PWD = privatekeyCMS.sign(PWD,"")
#encrypt AES PWD with client public key
enc_PWD = publickeyClient.encrypt(PWD, "")
print "with publickeyClient encrypted AES-PWD:"
print enc_PWD[0].encode("hex"),"\n"
print "with privatekeyCMS signed AES-PWD:"
print signed_PWD[0],"\n"
#decryption
dec_PWD= privatekeyClient.decrypt(enc_PWD[0])
#verify identity of the
print "key verify:\n",publickeyCMS.verify(dec_PWD,signed_PWD)
print "decrypted PWD:\n",dec_PWD
#################### ELGAMAL ####################
from Crypto.PublicKey import ElGamal
print "\n=====ELGamal 368 Demo====="
#generate 2 ELGAMAL key pair
rpool = Random.new()
Random.atfork()
privatekeyCMS = ElGamal.generate(368, rpool.read)
privatekeyClient = ElGamal.generate(368, rpool.read)
publickeyCMS = privatekeyCMS.publickey()
publickeyClient = privatekeyClient.publickey()
#generate for each encryption session new K
K=rpool.read(16).encode("hex")
print "K for encrypt:",K
#encryption
enc_PWD = publickeyClient.encrypt(PWD, K)
#generate for each sign session new k
strong_random = Random.random.StrongRandom(randfunc=rpool.read)
k = strong_random.randint(2,privatekeyCMS.p-2)
temp_p=privatekeyCMS.p
while GCD(privatekeyCMS.p-1,k)>1:
k = strong_random.randint(3,temp_p-2)
print "k for sign:",k,"\n"
#signature
signed_PWD = privatekeyCMS.sign(PWD,k)
print "with publickeyClient encrypted AES-PWD:"
print enc_PWD[0].encode("hex")
print "with privatekeyCMS signed AES-PWD:"
print signed_PWD[0],"\n"
#decryption
dec_PWD= privatekeyClient.decrypt(enc_PWD)
#verify signature
print "verify key:\n",bool(publickeyCMS.verify(dec_PWD,signed_PWD))
print "decrypted PWD:\n",dec_PWD
#################### DSA only sign ####################
print "\n=====DSA 512 Demo====="
from Crypto.PublicKey import DSA
#start the randomgenerator to generate integer
rpool = Random.new()
strong_random = Random.random.StrongRandom(randfunc=rpool.read)
Random.atfork()
#generate Server DSA key
privatekeyCMS = DSA.generate(512, rpool.read)
publickeyCMS = privatekeyCMS.publickey()
# generatae sha hash, which will be signed by the private key
import hashlib
m = hashlib.sha512()
m.update(PWD)
print "sha512 hash",m.digest()
#generate for each sign session new k
k = strong_random.randint(3,privatekeyCMS.q-1)
print "k for sign:",k,"\n"
#sign
signed_PWD = privatekeyCMS.sign(m.digest(),k)
m = hashlib.sha512()
m.update(dec_PWD)
print "sha512 hash",m.digest()
#verify
print "verify key:\n",publickeyCMS.verify(m.digest(),signed_PWD)
print "decrypted PWD from ELGAMAL:\n",dec_PWD
#decrypt the real message using the AES key
crypt = AES.new(dec_PWD,AES.MODE_CBC,Initial16bytes)
print "decrypted text: \n", crypt.decrypt(crypt_txt)
print "\n=====End of Demo====="
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.dlitz.net/pipermail/pycrypto/attachments/20090826/2f120a24/attachment.htm
More information about the pycrypto
mailing list