Python

Python Encrypt String

There are multiple instances where the programmer needs to encrypt the information of the user to safeguard it. The information is generally stored in a string variable, and therefore, to safeguard this information, the programmer encrypts the string. There are various packages/modules which provide methods to encrypt and decrypt the strings.

This post will be your guide to encrypting strings in Python using various modules and will contain the following content:

Method 1: Use the Cryptography Module to Encrypt Strings

The “Cryptography” contains a package named “Fernet” that can be used to encrypt strings in Python. However, you will have to install the “cryptography” module by using the following command in the terminal:

pip install cryptography

Once you have installed the module, start by importing the “fernet” package and creating your string to be encrypted:

from cryptography.fernet import Fernet

stringVar = "LinuxHint's Secret"

After that, generate a key using the generate_key() method add store it inside a new variable:

keyVar = Fernet.generate_key()

Create a new Fernet instance with the help of the generated key:

ferVar = Fernet(keyVar)

Use the Fernet instance “ferVar” to call the encrypt() method and pass in the string with encode() method applied on it:

encString = ferVar.encrypt(stringVar.encode())

Lastly, print both the original string and the encrypted string onto the terminal using the print() method:

print("Original String: ", stringVar)
print("Encrypted string: ", encString)

The complete code snippet for this example is as:

from cryptography.fernet import Fernet

stringVar = "LinuxHint's Secret"
keyVar = Fernet.generate_key()
ferVar = Fernet(keyVar)
encString = ferVar.encrypt(stringVar.encode())

print("Original String: ", stringVar)
print("Encrypted string: ", encString)

When you executed this code snippet on your machine, it will display the following results on the terminal:

You have successfully encrypted your string in Python using the cryptography library. However, to decrypt the string, use the following command:

decString = ferVar.decrypt(encString).decode()
print("\nDecrypted string: ", decString)

Append this code in the above code snippet, and execute it to get the following outcome on the terminal:

As you can see from the output image above, you were able to not only encrypt a string in Python but also decrypt it using the cryptography library.

Method 2: Use the RSA Library to Encrypt a String in Python

Another library that can be used to encrypt and decrypt strings in Python is the “rsa” library. To use this library, you will have to install it using the following command:

pip install rsa

The “rsa” library uses public and private keys to encrypt and decrypt strings, and these strings can be generated using the newkeys(512) method. Therefore, import the rsa library and create public and private key variables:

import rsa
 
pubkeyVar, priKeyVar = rsa.newkeys(512)

After that, create the string to be encrypted:

stringVar = "LinuxHint Confidential"

Encrypt the string using the encrypt() method of rsa by providing the string and the public key in the arguments:

encString = rsa.encrypt(stringVar.encode(),pubkeyVar)

Once that is done, print the original string and the encrypted string on the terminal using the following lines:

print("The Original string: ", stringVar)
print("The Encrypted string: ", encString)

When you execute this code, it will produce the following results on the terminal:

You have successfully encrypted a string in Python using the “rsa” library. To decrypt the same string, append the following lines in the above code snippet:

decMessage = rsa.decrypt(encString, priKeyVar).decode()
print("\nThe Decrypted string: ", decMessage)

When you execute this code snippet, this time it will produce the following results:

You have successfully learned how to encrypt strings using the rsa library.

Conclusion

The user can use the cryptography and the rsa library to encrypt and decrypt strings in Python. To use the methods of these libraries, you will have to install these libraries with the help of the “pip install” command. The rest of the process has been thoroughly explained in this guide.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.