import Crypto
from Crypto.PublicKey import RSA
from Crypto import Random
random_generator = Random.new().read
key = RSA.generate(1024, random_generator) #generate public and private keys
publickey = key.publickey # pub key export for exchange
encrypted = publickey.encrypt('encrypt this message', 32)
#message to encrypt is in the above line 'encrypt this message'
print 'encrypted message:', encrypted #ciphertext
f = open ('encryption.txt', 'w'w)
f.write(str(encrypted)) #write ciphertext to file
f.close()
#decrypted code below
f = open ('encryption.txt', 'r')
message = f.read()
decrypted = key.decrypt(message)
print 'decrypted', decrypted
f = open ('encryption.txt', 'w')
f.write(str(message))
f.write(str(decrypted))
f.close()
>>> import rsa
>>> (bob_pub, bob_priv) = rsa.newkeys(512)
#Par Mouad En-nasiry
dic1={chr(i+96):i for i in range (1,27)}
dic1['+'],dic1['-']=27,28
def get_key(dic,val):
for key, value in dic.items():
if val == value:
return key
#trouver la cle d'un valuer
def exp(A, P):
#on a suppose que A>=1 P>=0
result = 1
while P >= 1:
result = result * A
P -= 1
return result #touver la puissance rapidement
def Mod(A, P, M):
return exp(A,P)%29 # donner le rest de la division euclidienne de A**P Par 29
def chiffrer(M):
Code = str()
for i in M.lower():
X=Mod(dic1[i],17,29)
Code+=get_key(dic1,X)
return Code.upper()
def decrypter(C):
Message = str()
for i in C.lower():
X=Mod(dic1[i],5,29)
Message+=get_key(dic1,X)
return Message.upper()
Msg=input('pour chiffrer Entrez 1 pour Decrypter Entrez 2:')
if Msg=='1':
Msg=input('entrer le message: ')
print(chiffrer(Msg))
elif Msg=='2':
Msg=input('entrer le message codé: ')
print(decrypter(Msg))