Add asymmetric encryption in python

This commit is contained in:
Victor Matheus Castro 2018-10-04 13:27:43 -03:00
parent f2a4a5be6b
commit 02dfe28d4b
2 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,8 @@
## Asymmetric Encryption in python
An example of asymmetric encryption in python using a public/private keypair - utilizes RSA from PyCrypto library
```bash
pip install pycrypto
python asymmetric.py
```

View File

@ -0,0 +1,34 @@
from Crypto import Random
from Crypto.PublicKey import RSA
import base64
def generate_keys(modulus_length=256*4):
privatekey = RSA.generate(modulus_length, Random.new().read)
publickey = privatekey.publickey()
return privatekey, publickey
def encryptit(message , publickey):
encrypted_msg = publickey.encrypt(message, 32)[0]
encoded_encrypted_msg = base64.b64encode(encrypted_msg)
return encoded_encrypted_msg
def decryptit(message, privatekey):
decoded_encrypted_msg = base64.b64decode(message)
decoded_decrypted_msg = privatekey.decrypt(decoded_encrypted_msg)
return decoded_decrypted_msg
if __name__ == '__main__':
message = "This is a awesome message!"
privatekey , publickey = generate_keys()
encrypted_msg = encryptit(message.encode("utf-8"), publickey)
decrypted_msg = decryptit(encrypted_msg, privatekey)
print(f'{privatekey.exportKey()} - ({len(privatekey.exportKey())})')
print(f'{publickey.exportKey()} - ({len(publickey.exportKey())})')
print(f'Original: {message} - ({len(message)})')
print(f'Encrypted: {encrypted_msg} - ({len(encrypted_msg)})')
print(f'Decrypted: {decrypted_msg} - ({len(decrypted_msg)})')