How to encrypt a string in Python?

Ways to encrypt a string in Python.

In simple terms, encrypting a string is to transform the given plain text of a string into ciphertext. Essentially, it fulfills the purpose of encoding the given data. As encryption means securing the contents of the string, we can compare it to any real-life example where we put a lock over something. Naturally, there is a need for the existence of a key, which will be eventually utilized to unlock something or in our case, decrypt the string.

In this tutorial, we demonstrate the several ways available to us that help in encrypting a string in Python.

Using the Simple Crypt module.

Relevant only for Python 2.7 and Python 3, using the simple-crypt module is one of the fastest ways to implement the task of encrypting a string in Python. A single line of code is all that is needed to convert plain text into ciphertext, and it is capable of achieving this feat in a matter of seconds.

It uses the AES256 cipher and the algorithm implementation is supplied by the PyCrypto library. 

Simple Crypt incorporates both the encrypt and decrypt features. Installing both the pycrypto and the simplecrypt modules are essential before proceeding to utilize this method.

Consider the following code:

from simplecrypt import encrypt, decrypt
key1 = 'adh'
v1 = 'Something different'
cipher = encrypt(key1, v1)
print(cipher)

A cluster of random letters would come out to be the output of the above code.

Then, the code ciphertext can be decoded again by making use of the decrypt function.

print(decrypt('adh', cipher))
# Output: 'Something different'

Using the cryptocode module.

cryptocode is a simple module that is capable of carrying out the task of encrypting and decrypting a string in all versions of Python 3 or above.

The cryptocode module needs manual installation before utilizing its functions, and the installation can be simply carried out by making use of the pip command.

Consider the following code.

import cryptocode
encdd = cryptocode.encrypt("Something different","adh")
## And then to decode it:
decdd = cryptocode.decrypt(encdd, "adh")
print(decdd)
# Output : 'Something different'

Using the Cryptography module.

TheCryptography module is a bit different, as it is utilized to achieve Symmetric-key Encryption in Python. Symmetric-key encryption keeps it simple and uses just one key for both encryption and decryption processes.

The pip command can be utilized to manually install the cryptography package.

Consider the following code.

from cryptography.fernet import Fernet
 
v1 = "Something different"
key = Fernet.generate_key()
  
fernet = Fernet(key)
  
encc = fernet.encrypt(v1.encode())
  
decc = fernet.decrypt(enctex).decode()

print("The original string: ", v1)
print("message after encryption: ", encc)
print("message after decryption: ", decc)

Output:

The original string: Something different
message after encryption: <a stack of random letters>
message after decryption: Something different

The only disadvantage of utilizing this method is that the security aspect of the encryption takes a slight hit, meaning it is not as secure as Asymmetric key encryption.

Using the RSA algorithm.

The RSA algorithm is one of the ways that make use of Asymmetric-key encryption. The keys for encoding and decoding are different in Asymmetric-key encryption.

We use aprivate key and public key, with the public key being used for encryption and the private key focusing more on the decryption part.

The rsa library can be installed externally. The general pip command is good enough to install this library.

Consider the following code.

import rsa
pubk, privk = rsa.newkeys(512)
v1 = "Something different"
encc = rsa.encrypt(v1.encode(),pubk)  

decc = rsa.decrypt(encc, privk).decode()
print("The original string: ", v1)
print("message after encryption: ", encc)  
print("message after decryption: ", decc)

Output:

The original string: Something different 
message after encryption: <a stack of random letters> 
message after decryption: Something different

To Conclude.

This tutorial aims to assist programmers of all levels of skill and experience and demonstrates the different ways available to encrypt a string in Python.

Explore more from this category at Python Strings. Alternatively, search and view other topics at All Tutorials.