How to do Text Encryption?

Hi everyone.

How to do text encryption? Instead of the file itself to be encrypted, I’m trying to find out on how to encrypt only specific text inside the file.

It would be great if someone can explain briefly or point me to a guide about this.

Thank you.

At a very high level?

  1. Open file
  2. Scan for text to be encrypted.
  3. Encrypt it, and replace that instance of the plain-text with the encrypted text.
  4. Repeat for all other instances of target text.
  5. Repeat for all other target texts to be encrypted.
  6. Save.
1 Like

How to Encrypt a Word in a Text File

Lets say you have a text file, and want to encrypt the word β€œSecret”.


  1. Generate an encryption key
  2. open the file
  3. Search through each word
  4. If word equals target than encrypt word with key
  5. Save changes to file

Project Requirements

  • cryptography - This package provides cryptographic recipes and primitives to Python developers.

Directory Structure

β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ cryptKey.key
β”‚   β”œβ”€β”€
β”‚   β”œβ”€β”€ encrypted_text.txt
β”‚   β”œβ”€β”€
β”‚   β”œβ”€β”€
β”‚   β”œβ”€β”€ original_text.txt

Project Files

  • cryptKey - this is your secret key which is used for encryption
  • - this file decrypts a word in a file
  • encrypted.txt - This will be the output file of the encrypted text
  • - This file encrypts a word in a file
  • - This file generates the secret key (cryptKey.key)
  • original_text.txt - This file is the original plain text file.


This file contains a word that you want to encrypt; β€œSecret .”


First, lets generate a secret key, and save it to file.


from cryptography.fernet import Fernet

# generate encryption key:
key = Fernet.generate_key()

# Write key to file:
with open('crypt_key.key', 'wb') as filekey:

Your key should look something like the screen shot below:


Now, lets open the original_text file, and encrypt the word Secret :

from cryptography.fernet import Fernet

# Open cryptKey from file:
with open('cryptKey.key', 'rb') as filekey:
    crypt_key =

# Using the encryption key
fernet = Fernet(crypt_key)

# Open File:
with open('original_text.txt', 'r') as file:
    pageFile =' ')
    # Print original file

    # Iterate through file:
    for i, word in enumerate(pageFile):
        if word == 'Secret':
             pageFile[i] = fernet.encrypt(word.encode())
    # show encrypted word

    result = ' '.join([word.decode() if isinstance(word, bytes) else word for word in pageFile])

    # Save results as
    with open('encrypted_text.txt', 'w') as file:

Screen Shot

Now lets decrypt the word in the file:

from cryptography.fernet import Fernet

# Write key to file:
with open('cryptKey.key', 'rb') as filekey:
    crypt_key =

# Using the generated key
fernet = Fernet(crypt_key)

# Open File:
with open('encrypted_text.txt','r') as file:    
    pageFile =' ')
    # Show encrypted file
    # Decrypt secret word   
    secretWord = fernet.decrypt(bytes(pageFile[-1], 'utf8')).decode()
    pageFile[-1] = secretWord
    # Show results

Screen Shot


I hope this helps answer your question on β€œhow to encrypt only specific text inside the file”. If you have any questions or concerns, please don’t hesitate to ask. If anyone catches any bugs or errors please let me know!

Best regards,