Hi:<br>In this new demo <br>i use randint() and  Random.new().read() from the new Crypto.random module.<br>DSA use now 512 bit key at least.<br>and i use  sha512 for DSA.<br>DSA only verifty the sha512 hash of the original AES password.<br>
<br>about rsa:<br><span class="gI"><span class="ik"><img class=" QrVm3d" id="upi" name="upi" src="images/cleardot.gif" height="16px" width="16px"></span><span class="gD" style="color: rgb(121, 6, 25);">Dwayne C. Litzenberger said something about OAEP</span></span> for RSA.<br>
    this is a very critical issue, how can I use it.<br><br>Well, I feel now like child in a mine field. <br>So please help me check this again.<br><br><br>Thank you<br>-----------------------------------------------------------------------------------<br>
#!/usr/bin/env python<br>from Crypto.Cipher import AES<br>from Crypto.Util.number import GCD<br>from Crypto import Random<br>import os,sys<br>#####################AES ####################<br>print &quot;=====AES 256 Demo=====&quot;<br>
# use AES to encrypt the real message<br># use the more secure Crypto.Random to generate PWD and Initialbyte/IV<br># AES key is 32 byte or 16*hex_digit<br># Initial16bytes:16 bytes or 8*hex_digit<br>PWD=&quot;&quot;<br>rpool = Random.new() <br>
Random.atfork() <br><br>PWD = rpool.read(16).encode(&quot;hex&quot;)<br>Initial16bytes=rpool.read(8).encode(&quot;hex&quot;)<br><br>print &quot;AES-key:&quot;,PWD,&quot;len:&quot;,len(PWD)<br>print &quot;Initial16bytes:&quot;,Initial16bytes<br>
crypt = AES.new(PWD,AES.MODE_CBC,Initial16bytes)<br><br>plain=&quot;sex drugs and crypto&quot;<br>#block ciffre need string with lenth 16: add the restbyte to plain<br>restbyte =(16-len(plain)%16)%16<br>temp_string=&quot;&quot;<br>
for a in range(restbyte):<br>    temp_string+=&quot; &quot;<br>plain+=temp_string<br>#encryption<br>print &quot;\nplain text: \n&quot;,plain,&quot;\n&quot;<br>crypt_txt= crypt.encrypt(plain)<br>print &quot;encrypted text: \n&quot;,crypt_txt.encode(&quot;hex&quot;),&quot;\n&quot;<br>
#decryption<br>crypt = AES.new(PWD,AES.MODE_CBC,Initial16bytes)<br>print &quot;decrypted text: \n&quot;, crypt.decrypt(crypt_txt)<br>#################### RSA ####################<br>print &quot;\n=====RSA 368 Demo=====&quot;<br>
#use 1 RSA key to encrypt the AES key<br>#use another RSA key to sign AES key<br>from Crypto.PublicKey import RSA<br><br>#start the random generator<br>rpool = Random.new() <br>Random.atfork()<br><br># generate both RSA keys,  <br>
privatekeyCMS = RSA.generate(368, rpool.read)<br>Random.atfork()<br>privatekeyClient = RSA.generate(368, rpool.read)<br>publickeyCMS = privatekeyCMS.publickey()<br>publickeyClient = privatekeyClient.publickey()<br><br>#sign the AES PWD with server private key<br>
signed_PWD = privatekeyCMS.sign(PWD,&quot;&quot;)<br>#encrypt AES PWD with client public key<br>enc_PWD = publickeyClient.encrypt(PWD, &quot;&quot;)<br>print &quot;with publickeyClient encrypted AES-PWD:&quot;<br>print enc_PWD[0].encode(&quot;hex&quot;),&quot;\n&quot;<br>
print &quot;with privatekeyCMS signed AES-PWD:&quot;<br>print signed_PWD[0],&quot;\n&quot;<br><br>#decryption<br>dec_PWD= privatekeyClient.decrypt(enc_PWD[0])<br>#verify identity of the <br>print &quot;key verify:\n&quot;,publickeyCMS.verify(dec_PWD,signed_PWD)<br>
print &quot;decrypted PWD:\n&quot;,dec_PWD<br><br><br>#################### ELGAMAL ####################<br>from Crypto.PublicKey import ElGamal<br>print &quot;\n=====ELGamal 368 Demo=====&quot;<br><br><br>#generate 2 ELGAMAL key pair<br>
rpool = Random.new() <br>Random.atfork()<br>privatekeyCMS = ElGamal.generate(368, rpool.read)<br>privatekeyClient = ElGamal.generate(368, rpool.read)<br>publickeyCMS = privatekeyCMS.publickey()<br>publickeyClient = privatekeyClient.publickey()<br>
<br>#generate for each encryption session new K<br>K=rpool.read(16).encode(&quot;hex&quot;)<br>print &quot;K for encrypt:&quot;,K<br>#encryption<br>enc_PWD = publickeyClient.encrypt(PWD, K)<br><br>#generate for each sign session new k<br>
strong_random = Random.random.StrongRandom(randfunc=rpool.read)<br>k = strong_random.randint(2,privatekeyCMS.p-2)<br>temp_p=privatekeyCMS.p<br>while GCD(privatekeyCMS.p-1,k)&gt;1:<br>    k = strong_random.randint(3,temp_p-2)<br>
print &quot;k for sign:&quot;,k,&quot;\n&quot;<br>#signature<br>signed_PWD = privatekeyCMS.sign(PWD,k)<br><br><br>print &quot;with publickeyClient encrypted AES-PWD:&quot;<br>print enc_PWD[0].encode(&quot;hex&quot;)<br>print &quot;with privatekeyCMS signed AES-PWD:&quot;<br>
print signed_PWD[0],&quot;\n&quot;<br><br>#decryption<br><br>dec_PWD= privatekeyClient.decrypt(enc_PWD)<br>#verify signature<br>print &quot;verify key:\n&quot;,bool(publickeyCMS.verify(dec_PWD,signed_PWD))<br>print &quot;decrypted PWD:\n&quot;,dec_PWD<br>
<br>#################### DSA only sign ####################<br><br><br>print &quot;\n=====DSA 512 Demo=====&quot;<br>from Crypto.PublicKey import DSA<br>#start the randomgenerator to generate integer <br>rpool = Random.new()<br>
strong_random = Random.random.StrongRandom(randfunc=rpool.read)<br>Random.atfork() <br><br>#generate Server DSA key<br>privatekeyCMS = DSA.generate(512, rpool.read)<br>publickeyCMS = privatekeyCMS.publickey()<br><br># generatae sha hash, which will be signed by the private key<br>
import hashlib<br>m = hashlib.sha512()<br>m.update(PWD)<br>print &quot;sha512 hash&quot;,m.digest()<br><br>#generate for each sign session new k <br>k = strong_random.randint(3,privatekeyCMS.q-1)<br><br>print &quot;k for sign:&quot;,k,&quot;\n&quot;<br>
<br>#sign<br>signed_PWD = privatekeyCMS.sign(m.digest(),k)<br><br><br>m = hashlib.sha512()<br>m.update(dec_PWD)<br><br>print &quot;sha512 hash&quot;,m.digest()<br><br>#verify<br>print &quot;verify key:\n&quot;,publickeyCMS.verify(m.digest(),signed_PWD)<br>
print &quot;decrypted PWD from ELGAMAL:\n&quot;,dec_PWD<br><br>#decrypt the real message using the AES key<br>crypt = AES.new(dec_PWD,AES.MODE_CBC,Initial16bytes)<br>print &quot;decrypted text: \n&quot;, crypt.decrypt(crypt_txt)<br>
print &quot;\n=====End of Demo=====&quot;<br><br>