02-02-2023, 12:29 PM
Two Easy Ways to Encrypt and Decrypt Python Strings
5/5 – (1 vote)
Today I gave a service consultant access to one of my AWS servers. I have a few files on the server that I was reluctant to share with the service consultant because these files contain sensitive personal data. Python is my default way to solve these types of problems. Naturally, I wondered how to encrypt this data using Python — and decrypt it again after the consultant is done? In this article, I’ll share my learnings!![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
Question: Given a Python string. How to encrypt the Python string using a password or otherwise and decrypt the encrypted phrase to obtain the initial cleartext again?
There are several ways to encrypt and decrypt Python strings. I decided to share only the top two ways (my personal preference is Method 1):
Method 1: Cryptography Library Fernet
![[Image: image-3.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-3.png)
To encrypt and decrypt a Python string, install and import the library, generate a Fernet key, and create a object with it. You can then encrypt the string using the method and decrypt the encrypted string using the method.
If you haven’t already, you must first install the library using the shell command or variants thereof.
See more here.
Here’s a minimal example where I’ve highlighted the encryption and decryption calls:
This small script first imports the class from the library that provides high-level cryptographic primitives and algorithms such as
A Fernet key is then generated and used to create a object. The input string to be encrypted is then provided as an argument to the method of the object. This method encrypts the string using the Fernet key and returns an encrypted string.
The encrypted string is then provided as an argument to the method of the object. This method decrypts the encrypted string using the Fernet key and returns a decrypted string.
Finally, the original string and the decrypted string are printed to the console.
The output is as follows:
Try it yourself in our Jupyter Notebook:
![[Image: image.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image.png)
Method 2: PyCrypto Cipher
![[Image: image-4.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-4.png)
Install and import the PyCrypto library to encrypt and decrypt a string. As preparation, you need to make sure to pad the input string to 32 characters using to make sure it is the correct length. Then, define a secret key, i.e., a “password”. Finally, encrypt the string using the AES algorithm, which is a type of symmetric-key encryption.
You can then decrypt the encrypted string again by using the same key.
Here’s a small example:
This code imports the PyCrypto library and uses it to encrypt and decrypt a string.
The input string is, which is padded to 32 characters to make sure it is the correct length.
Then, a secret key (password) is defined.
The string is encrypted using the AES algorithm, which is a type of symmetric-key encryption.
The encrypted string is then decrypted using the same key and the original and decrypted strings are printed. Here’s the output:
Try it yourself in our Jupyter Notebook:
![[Image: image-1.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-1.png)
Thanks for Visiting!![[Image: 2665.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/2665.png)
To keep learning Python in practical coding projects, check out our free email academy — we have cheat sheets too!
5/5 – (1 vote)
Today I gave a service consultant access to one of my AWS servers. I have a few files on the server that I was reluctant to share with the service consultant because these files contain sensitive personal data. Python is my default way to solve these types of problems. Naturally, I wondered how to encrypt this data using Python — and decrypt it again after the consultant is done? In this article, I’ll share my learnings!
![[Image: 1f447.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/1f447.png)
Question: Given a Python string. How to encrypt the Python string using a password or otherwise and decrypt the encrypted phrase to obtain the initial cleartext again?There are several ways to encrypt and decrypt Python strings. I decided to share only the top two ways (my personal preference is Method 1):
Method 1: Cryptography Library Fernet
![[Image: image-3.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-3.png)
To encrypt and decrypt a Python string, install and import the
Code:
cryptographyCode:
FernetCode:
Fernet.encrypt()Code:
Fernet.decrypt()If you haven’t already, you must first install the
Code:
cryptographyCode:
pip install cryptography
See more here.Here’s a minimal example where I’ve highlighted the encryption and decryption calls:
Code:
# Import the cryptography library
from cryptography.fernet import Fernet # Generate a Fernet key
key = Fernet.generate_key() # Create a Fernet object with that key
f = Fernet(key) # Input string to be encrypted
input_string = "Hello World!" # Encrypt the string
encrypted_string = f.encrypt(input_string.encode()) # Decrypt the encrypted string
decrypted_string = f.decrypt(encrypted_string) # Print the original and decrypted strings
print("Original String:", input_string)
print("Decrypted String:", decrypted_string.decode())This small script first imports the
Code:
FernetCode:
cryptography- symmetric encryption,
- public-key encryption,
- hashing, and
- digital signatures.
A Fernet key is then generated and used to create a
Code:
FernetCode:
encrypt()Code:
FernetThe encrypted string is then provided as an argument to the
Code:
decrypt()Code:
FernetFinally, the original string and the decrypted string are printed to the console.
The output is as follows:
Code:
Original String: Hello World!
Decrypted String: Hello World!Try it yourself in our Jupyter Notebook:
![[Image: image.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image.png)
Method 2: PyCrypto Cipher
![[Image: image-4.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-4.png)
Install and import the PyCrypto library to encrypt and decrypt a string. As preparation, you need to make sure to pad the input string to 32 characters using
Code:
string.rjust(32)You can then decrypt the encrypted string again by using the same key.
Here’s a small example:
Code:
# Import the PyCrypto library
import Crypto # Input string to be encrypted (padding to adjust length)
input_string = "Hello World!".rjust(32) # Secret key (pw)
key = b'1234567890123456' # Encrypt the string
cipher = Crypto.Cipher.AES.new(key)
encrypted_string = cipher.encrypt(input_string.encode()) # Decrypt the encrypted string
decrypted_string = cipher.decrypt(encrypted_string) # Print the original and decrypted strings
print("Original String:", input_string)
print("Decrypted String:", decrypted_string.decode())This code imports the PyCrypto library and uses it to encrypt and decrypt a string.
The input string is
Code:
"Hello World!"Then, a secret key (password) is defined.
The string is encrypted using the AES algorithm, which is a type of symmetric-key encryption.
The encrypted string is then decrypted using the same key and the original and decrypted strings are printed. Here’s the output:
Code:
Original String: Hello World!
Decrypted String: Hello World!Try it yourself in our Jupyter Notebook:
![[Image: image-1.png]](https://blog.finxter.com/wp-content/uploads/2023/02/image-1.png)
Thanks for Visiting!
![[Image: 2665.png]](https://s.w.org/images/core/emoji/14.0.0/72x72/2665.png)
To keep learning Python in practical coding projects, check out our free email academy — we have cheat sheets too!

