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
- Method 2: Use the RSA Library to Encrypt a String in Python
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:
Once you have installed the module, start by importing the “fernet” package and creating your string to be encrypted:
stringVar = "LinuxHint's Secret"
After that, generate a key using the generate_key() method add store it inside a new variable:
Create a new Fernet instance with the help of the generated key:
Use the Fernet instance “ferVar” to call the encrypt() method and pass in the string with encode() method applied on it:
Lastly, print both the original string and the encrypted string onto the terminal using the print() method:
print("Encrypted string: ", encString)
The complete code snippet for this example is as:
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:
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:
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:
pubkeyVar, priKeyVar = rsa.newkeys(512)
After that, create the string to be encrypted:
Encrypt the string using the encrypt() method of rsa by providing the string and the public key in the arguments:
Once that is done, print the original string and the encrypted string on the terminal using the following lines:
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:
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.