Reputation: 21
How do I go about using RSA or similar Public Key-Private Key encryption in Python 3, preferably with a built-in module? All the resources I've found so far are for Python 2.7
Upvotes: 2
Views: 9316
Reputation: 481
Here is an implementation of RSA Encryption in Python 3 I have made, using the Crypto library (installed with the command pip install pycryptodome
)
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
from Crypto.Random import new as Random
from base64 import b64encode
from base64 import b64decode
class RSA_Cipher:
def generate_key(self,key_length):
assert key_length in [1024,2048,4096]
rng = Random().read
self.key = RSA.generate(key_length,rng)
def encrypt(self,data):
plaintext = b64encode(data.encode())
rsa_encryption_cipher = PKCS1_v1_5.new(self.key)
ciphertext = rsa_encryption_cipher.encrypt(plaintext)
return b64encode(ciphertext).decode()
def decrypt(self,data):
ciphertext = b64decode(data.encode())
rsa_decryption_cipher = PKCS1_v1_5.new(self.key)
plaintext = rsa_decryption_cipher.decrypt(ciphertext,16)
return b64decode(plaintext).decode()
The usage of the following class is like so:
- cipher = RSA_Cipher()
- cipher.generate_key(1024) #key length can be 1024, 2048 or 4096
- cipher.encrypt("hello world") #automatically uses generated key
- cipher.decrypt("nt3vNNqzyAo2SINPgsb/eOLU2PD0DF0EstvnIHUmYGX4CVAvS0pDEboqGcuitYAzSV10Ii+fliwihu/L0ISrL6w/tRDQILHFM5PrN2pqzK+Lu6QHKUShFdQtikduo1KHXGlJNd25sVlDOhWAq/FK/67Yeoyz6fSP6PNXRjX7Q+Q=)
Upvotes: 3
Reputation: 348
Python 3 doesn’t have very much in its standard library that deals with encryption. Instead, you get hashing libraries If you need secure hashes or message digest algorithms, then Python’s standard library has you covered in the hashlib module
If you want to encrypt your data with RSA, then you’ll need to either have access to a public / private RSA key pair or you will need to generate your own. For this example, we will just generate our own. Since it’s fairly easy to do, we will do it in Python’s interpreter: you must first install PyCrypto package for python 3
>>> from Crypto.PublicKey import RSA
>>> code = 'nooneknows'
>>> key = RSA.generate(2048)
>>> encrypted_key = key.exportKey(passphrase=code, pkcs=8,
protection="scryptAndAES128-CBC")
>>> with open('/path_to_private_key/my_private_rsa_key.bin', 'wb') as f:
f.write(encrypted_key)
>>> with open('/path_to_public_key/my_rsa_public.pem', 'wb') as f:
f.write(key.publickey().exportKey())
First, we import RSA from Crypto.PublicKey. Then we create a silly passcode. Next we generate an RSA key of 2048 bits. Now we get to the good stuff. To generate a private key, we need to call our RSA key instance’s exportKey method and give it our passcode, which PKCS standard to use and which encryption scheme to use to protect our private key. Then we write the file out to disk.
Next, we create our public key via our RSA key instance’s publickey method. We used a shortcut in this piece of code by just chaining the call to exportKey with the publickey method call to write it to disk as well.
Upvotes: 1