1/5
## Encrypting Your Keys in Python We'll learn how to encrypt our private key so that we don't ever have to store it in plain text. We'll use tools that will automatically do this encryption for us later on, but let's learn how to do it manually. We'll start by creating a new file called `encrypt_key.py`: ```python if __name__ == "__main__": main() ``` ```python def main(): pass ``` We'll create a `main()` function. Now, this `encrypt_key.py` file is going to be how we encrypt our key. To encrypt it, we're going to use the Python built-in tool called `getpass`. We'll start by removing any references to our private key in a `.env` file. Next, we'll import the `getpass` library: ```python import getpass ``` In our `main()` function, we'll use `getpass.getpass()` to prompt the user for their private key: ```python def main(): private_key = getpass.getpass("Enter your private key: ") # input() ``` This is similar to the `input()` command in Python. We'll then create an Eth account using the `eth_account` library that comes bundled with Web3.py: ```python from eth_account import Account ``` ```python def main(): private_key = getpass.getpass("Enter your private key: ") # input() my_account = Account.from_key(private_key) ``` We'll then ask the user for a password to encrypt the private key: ```python password = getpass.getpass("Enter a password:\n") # input() ``` We'll use the `\n` character to create a new line for the password prompt. We'll encrypt the private key using the password and the `Account` object we created: ```python encrypted_account = my_account.encrypt(password) ``` We'll then save the encrypted account to a file. To do this, we'll use the `pathlib` library that comes bundled with Python: ```python from pathlib import Path ``` We'll create a path object to specify the path to our encrypted keystore: ```python KEYSTORE_PATH = Path("keystore.json") ``` We can then use the path object to open the file and save the encrypted account using the `json` library that comes bundled with Python: ```python import json ``` ```python with KEYSTORE_PATH.open("w") as fp: json.dump(encrypted_account, fp) ``` To ensure that our encrypted keystore is not accidentally committed to version control, we should add it to our `.gitignore` file. We can now run our script from the terminal: ```bash python encrypt_key.py ``` We'll be prompted to enter our private key and then a password. Once we enter both, the script will save our encrypted private key to the `keystore.json` file. **Tag:** JSON object, keystore file, encrypt private key, `getpass`, `pathlib`, `json`
A practical guide to encrypting your keys in Python - This lesson will teach you how to encrypt your private key using Python and the getpass library. You'll learn to avoid storing your private key in plain text, and instead create a new file that will store it in an encrypted form.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using Moccasin
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on April 21, 2025
Duration: 2h 20min
Duration: 1h 51min
Duration: 58min
Duration: 2h 23min
Duration: 53min
Duration: 2h 24min
Duration: 28min
Duration: 1h 54min
Duration: 11min
Course Overview
About the course
Python basics
Introduction to Web3.py
Introduction to Titanoboa
Introduction to Moccasin
How to create an ERC-20
How to test Python code and Vyper smart contract
How to deploy Vyper smart contracts on ZKsync using Moccasin
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
On-chain Data Analyst
$59,000 - $139,000 (avg. salary)
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Last updated on April 21, 2025
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar