I had troubles compiling all the most commonly mentioned cryptography libraries on my Windows 7 system and for Python 3.5.

This is the solution that finally worked for me.

from cryptography.fernet import Fernet
key = Fernet.generate_key() #this is your "password"
cipher_suite = Fernet(key)
encoded_text = cipher_suite.encrypt(b"Hello stackoverflow!")
decoded_text = cipher_suite.decrypt(encoded_text)
Answer from KRBA on Stack Overflow
๐ŸŒ
Medium
medium.com โ€บ @sunilnepali844 โ€บ complete-guide-to-encryption-and-decryption-in-python-for-beginners-61c2343c3f2b
Complete Guide to Encryption and Decryption in Python (For Beginners) | by Sunil Nepali | Medium
June 19, 2025 - Never use outdated algorithms like DES in production. can we decrypt data encrypted with AES using another algorithm ยท No, you cannot decrypt data encrypted with AES using another algorithm. AES (Advanced Encryption Standard) is a symmetric encryption algorithm โ€” it uses the same key for both encryption and decryption.
Discussions

What is the simplest way to encrypt images and text files in python?
I'm going to assume you are trying to learn how to do this and don't just want a solution posted. The simplest way is to use the cryptography library , which you'll need to install (usually pip install cryptography). If you aren't sure how to install modules, let me know, but I'll proceed assuming you either know or can find it. Next, we want to use the Fernet module to encrypt and decrypt files. Note that this encrypts and decrypts files, not just images and text, as there's no real meaningful distinction there. For illustration purposes, I'm going to use basic input and no looping, but in an actual program you'd want to save and load to files and add looping functionality for multiple files. I'm also having it create copies of the encrypted files as it's safer and less complicated. Once we're set up, we import the module and create a function to generate a key: from cryptography.fernet import Fernet user_key = input("Generate new key (y/n) ") if user_key.lower() == 'y': user_key = Fernet.generate_key() with open('key.key', 'wb') as f: f.write(user_key) else: with open('key.key', 'rb') as f: user_key = f.read() Now we've got a key, which is a string the program will use to encrypt or decrypt files. IMPORTANT: If you lose your key, you cannot decrypt the file. Be sure to save your keys somewhere safe. I save it to a file here because trying to copy and paste keys can be problematic. Why Fernet? There are multiple options, but I believe this is the easiest, using a direct "secret key" form of encryption. Encryption is a huge topic and there are all sorts of different methods for encrypting files, but things like public keys vs. private keys and different encryption algorithms are far beyond the beginner level and aren't necessary to know to get started. Just keep in mind this was chosen for simplicity while still being quite secure, however, there are many other options that may be better depending on the purpose of your encryption. Next, we need to encrypt a file. This is just a matter of reading the file and encrypting the contents, then saving the file again: file = input("Enter a file to encrypt (enter to skip): ") if file != '': with open(file, 'rb') as f: data = f.read() fernet = Fernet(user_key) encrypted = fernet.encrypt(data) new_file = file[:-4] + '_encrypted' + file[-4:] with open(new_file, 'wb') as f: f.write(encrypted) What's going on here? First, we read the file we want to encrypt as bytes (this is important). Then we create a Fernet object that can be used for encryption with our user key. This uses encrypt to encrypt the contents of the file. The new_file line just adds _encrypted into the file name so it doesn't overwrite the contents; if you like to live dangerously, just use file again in the second with block, which will overwrite the existing file. That's basically it to get an encrypted file. There are a lot of things you could do to make this more user-friendly, but for "simplest" this is about as simple as you can get. Having an encrypted file is kind of pointless if we can't decrypt it. So our next block will do just that: file = input("Enter a file to decrypt (enter to skip): ") if file != '': with open(file, 'rb') as f: data = f.read() fernet = Fernet(user_key) decrypted = fernet.decrypt(data) new_file = file[:-4] + '_decrypted' + file[-4:] with open(new_file, 'wb') as f: f.write(decrypted) Unsurprisingly, it's the same process, you just use decrypt instead of encrypt. Be sure to enter your encrypted file, not the original! You'll get an error if you try to decrypt a file that isn't encrypted. And that's basically it. This could be greatly improved by adding better error handling, saving keys more securely, etc. If you just want something encrypted that you can later decrypt, though, and someone isn't going to have access to the file on the computer to encrypted it on (and therefore can read your key file), this should be enough to get you started. Let me know if you have questions! More on reddit.com
๐ŸŒ r/learnpython
7
8
September 9, 2024
Python Newbie - Encryption and decryption help
Conceptually, encryption and decryption have to be inverse functions (with the exception of ROT13, because the function is its own inverse.) Here's your "encryption" algorithm: encrypted += char.translate(mapping) here's your "decryption" algorithm: decrypted += char.translate(mapping) Maybe you can see, as I do, that these are the same algorithm. More on reddit.com
๐ŸŒ r/learnpython
6
0
May 16, 2023
Encrypting & Decrypting Files in Python and why you should
We have to talk about key handling. The secret key does not go into the code. More on reddit.com
๐ŸŒ r/Python
16
22
March 5, 2022
Basic Encryption/Decryption program
Good job, and I like that you included the original file too so we can see how much you've improved. Here's a couple tips: When you're defining the plaintext and ciphertext lists, you can save some time by just making them strings instead of lists of characters You can use the "find" method built into strings instead of the inner loop in your functions To make it so it uses a "key" to encrypt/decrypt, you can use the "shuffle" function in the "random" library to create an arbitrary ciphertext string, and you can use the key as a seed for the process. There's going to be people that come in here and tell you that this really isn't an adequate cryptographic algorithm. What you've got is called a "substitution cipher", and its susceptible to quite a lot of effective attacks. Modern encryption techniques generally do some complex operation on each byte which is dependent on the byte, the key, the position of the byte, and some state based on all of the bytes already processed. This way, knowing some information about the original text doesn't give you any head start in attacking it. Also, I should note that the "random" module isn't actually a cryptographically secure random number generator, meaning that there are ways to predict it's output. More on reddit.com
๐ŸŒ r/Python
37
97
November 5, 2021
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
Encrypt/Decrypt With Custom Dictionary - Python Help - Discussions on Python.org
February 5, 2023 - Dear community, Iโ€™ve created a code to Encrypt/Decrypt lists of words using a custom dictionary. Although the Encrypt function work as expected, the Decrypt option is causing me some problems, Iโ€™ll explain why. The entire code: #Dictionary crypt = { "a": "100", "b": "101", "c": "102", "d": "103", "e": "104", "f": "105", "g": "106", "h": "107", "i": "108", "j": "109", "k": "110", "l": "111", "m": "112", "n": "113", "o": "114", "p"...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-encrypt-and-decrypt-strings-in-python
How to Encrypt and Decrypt Strings in Python? - GeeksforGeeks
August 14, 2024 - 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).
๐ŸŒ
Gitlab
deapsecure.gitlab.io โ€บ deapsecure-lesson05-crypt โ€บ 03-python-library โ€บ index.html
DeapSECURE Lesson 5: Cryptography for Privacy-Preserving Computation: Data Encoding and Encryption Using Python Libraries
July 31, 2024 - What we want to do here is encrypt and decrypt a message using the provided module. The following code snipet show how to do this: import codecs import aes # The master key (a secret) must be less than 128 bits (16 bytes): master_key = 0x5e413c # Initializing "E", the object that can perform the encrypting / decrypting: E = AES(master_key) # You can change any plaintext with 16 bytes in hexadecimal # the string must also under 16 letters text_string = 'Idea Fusion' # encode it to hex string plaintext_string = codecs.encode(text_string.encode(),'hex') #convert the hex string to number for encry
๐ŸŒ
Snyk
snyk.io โ€บ blog โ€บ symmetric-asymmetric-file-encryption-in-python
File encryption in Python: An in-depth exploration of symmetric and asymmetric techniques | Snyk
November 22, 2023 - PyNaCl is a Python binding to the networking and cryptography library ยท libsodium. It contains a treasure trove of cryptography tools, including SecretBox, which provides symmetric encryption functionality. The beauty of SecretBox lies in its simplicity, with just one key to encrypt and decrypt our data.
๐ŸŒ
Elc
elc.github.io โ€บ python-security โ€บ chapters โ€บ 06_Symmetric_Encryption.html
Symmetric Encryption โ€” Python Security
A futher categorization of symmetric encryption algorithms is between those with authentication features and those without. Both provide a way to securely transmit a meesage, however, authenticated Encryption also allows, among other things, to verify whether the ciphered message has been modified. Modifying the ciphered message in a non-authenticated scheme will produce a nonsensical output whereas in authenticated encryption it will throw an error. Python does not include any symmetric encryption features in its standard library, therefore, to use it one should install a third party tool.
Find elsewhere
๐ŸŒ
Innovate Yourself
innovationyourself.com โ€บ encryption-and-decryption
EASY ENCRYPTION AND DECRYPTION IN PYTHON โ€“ 8
October 13, 2021 - If you also want to encrypt or decrypt the uppercase value, digits or special character, then do include them in the alphabets so as to convert it. In this way you can encrypt and decrypt the full text that the user will give as an input.
๐ŸŒ
YouTube
m.youtube.com โ€บ watch
Encryption program in Python
Share your videos with friends, family, and the world
Published: November 17, 2022
๐ŸŒ
Analytics India Magazine
analyticsindiamag.com โ€บ deep-tech โ€บ implementing-encryption-and-decryption-of-data-in-python
Implementing Encryption and Decryption of Data in Python
India's leading AI and data science media platform โ€” in-depth coverage of artificial intelligence, machine learning, research and tech business.
๐ŸŒ
Medium
medium.com โ€บ @madeenali2003 โ€บ unlocking-cryptography-a-hands-on-guide-to-encrypting-and-decrypting-files-using-python-611766e73f7a
Unlocking Cryptography: A Hands-On Guide to Encrypting and Decrypting Files Using Python | by Muhammad Adeen Ali | Medium
December 5, 2023 - The public key is like a lock for encrypting data, while the Private key is the unique key to unlock and decrypt the data. ... Pythonโ€™s simplicity and powerful libraries make it a top choice for encryption tasks.
๐ŸŒ
UltraEdit
ultraedit.com โ€บ home โ€บ blog โ€บ how to encrypt and decrypt a file in python [updated 2025]
How To Encrypt And Decrypt A File In Python
August 31, 2025 - This library implements the AES symmetric encryption algorithm and uses the same key to encrypt and decrypt data. To get started with the cryptography library, you need to install it using the following command: ... The methods that implement ...
๐ŸŒ
Amanxai
amanxai.com โ€บ home โ€บ all articles โ€บ encrypt and decrypt using python
Encrypt and Decrypt using Python | Aman Kharwal
June 26, 2021 - To encrypt and decrypt with Python, you need to create a program in which it will first ask you if you want to encrypt a message or decrypt it.
๐ŸŒ
Pythonista Planet
pythonistaplanet.com โ€บ cryptography
Cryptography Using Python Modules โ€“ Pythonista Planet
August 8, 2022 - Encryption is basically a conversion of plain text into cipher text. Decryption is the process of retrieving the plain text from the cipher text using a secret key. There are three widely used types of cryptography: ... Modules are files that ...
๐ŸŒ
DevGenius
blog.devgenius.io โ€บ creating-custom-encrypting-encoding-and-decrypting-program-in-python-5a607bd8b67
Creating CUSTOM Encrypting(Encoding) and Decrypting program in Python | by Nagaraj Vaidya | Dev Genius
September 2, 2022 - This is used to generate the keys by calling the function generate_keys which we discussed in the above section. โ€ข stringToDecrypt โ€” The encrypted message that has to be decrypted. Line 3 โ€” Creating the encryption keys and storing it in dictionary named โ€˜keysโ€™ by calling the function generate_keys.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ encrypt-string-python
Python Encrypt String
April 11, 2023 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
๐ŸŒ
LinkedIn
linkedin.com โ€บ all โ€บ data security
How do you handle encryption and decryption exceptions and failures in python?
March 5, 2024 - Learn how to handle common encryption and decryption exceptions and failures in python using the try-except block and the cryptography module.
๐ŸŒ
Webscale
section.io โ€บ home โ€บ blog
Implementing RSA Encryption and Decryption in Python
May 29, 2026 - Infrastructure-layer first-party capture closes the latency gap. ... Five Founding Partner spots. Platinum revenue share locked for 12 months. How the Webscale AI Founding Partner program is built for agencies. ... What Shopware merchants miss with generic managed hosting: API-first edge routing, dynamic caching and AI commerce readiness built for the platform.
๐ŸŒ
CopyAssignment
copyassignment.com โ€บ aes-in-python-encrypt-decrypt-pycryptodome
AES in Python | Encrypt & Decrypt | PyCryptodome
Disclaimer: References to any specific company, product or services on this Site are not controlled by GoDaddy.com LLC and do not constitute or imply its association with or endorsement of third party advertisers