How to Encrypt and Decrypt Strings in Python

Encryption is a fundamental concept in the realm of security and privacy. It ensures that sensitive data remains confidential and is protected from unauthorized access or eavesdropping.

Encryption terminology

  1. Plain Text: This is the original, readable message or data fed into the encryption process.

  2. Encryption Algorithm: This is a method or formula used for turning plain text into an encrypted message.
  3. Encryption Key: This is a piece of information, often a string of random characters, used with the encryption algorithm to convert the plain text into an encrypted form. The security of encrypted data is largely dependent on the strength and secrecy of the encryption key.
  4. Cipher Text: This is the scrambled or unreadable output produced after encrypting the plain text. Without the appropriate decryption key, this text should ideally be meaningless and unreadable.
  5. Decryption: This is the reverse process of encryption. It involves converting the cipher text back into its original form (plain text) using a decryption key, which may or may not be the same as the encryption key, depending on the encryption scheme used.

Symmetric Encryption

In this method, the same key is used for both encryption and decryption. AES (Advanced Encryption Standard) is a commonly used symmetric encryption algorithm.

The data is encoded and decoded in symmetric-key encryption with the same key. This is the easiest way of encryption, but also less secure. The receiver needs the key for decryption, so a safe way needs to transfer keys. Anyone with the key can read the data in the middle.

Install the Python cryptography library with the following command:

pip install cryptography


from cryptography.fernet import Fernet

message = "Database is key"

key = Fernet.generate_key()

fernet = Fernet(key)

encMessage = fernet.encrypt(message.encode())

print("original string: ", message)
print("encrypted string: ", encMessage)

decMessage = fernet.decrypt(encMessage).decode()

print("decrypted string: ", decMessage)


original string: Database is key
encrypted string: b'gAAAAABk0i99dYJbzSIKAdADPSVTP6eR3jPRgArfPblpaYyRrQzgiy_j
decrypted string: Database is key

Asymmetric Encryption

This method involves two keys: a public key and a private key. The public key is used for encryption, while the private key is used for decryption. RSA (Rivest–Shamir–Adleman) is a widely used asymmetric encryption algorithm.

The public key is used to encrypt the data, and the private key is used to decrypt the data. By the name, the public key can be public (can be sent to anyone who needs to send data). No one has your private key, so no one in the middle can read your data.

Install the Python rsa library with the following command.

pip install rsa


import rsa

publicKey, privateKey = rsa.newkeys(512)

message = "Database is key"

encMessage = rsa.encrypt(message.encode(),

print("original string: ", message)
print("encrypted string: ", encMessage)

decMessage = rsa.decrypt(encMessage, privateKey).decode()

print("decrypted string: ", decMessage)


Asymmetric-key Encryption

I hope this helps!

2 thoughts on “How to Encrypt and Decrypt Strings in Python”

  1. FYI: This produces the error: TypeError: Only byte strings can be passed to C code. If you try to solve it by encoding the message, CBC fails with ValueError: Error 3 while encrypting in CBC mode. You might want to take a look as there is no padding function available to solve! Both encoding of the message object (as bytes) and a padding function (for non 16b length) are necessary for crypto to actually work.

  2. …The solution is:

    PAD = lambda s: s + (32 – len(s) % 32) * ‘ ‘
    encrypted_text = obj.encrypt(PAD(message).encode(“utf-8”))

    …this rectifies the message length and subsequently encodes before encrypting. Decryption functions as shown. Happy Encrypting… [R]


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.