


How to Safely Encrypt and Obscure Strings Using Passwords in Python?
Encrypting Strings with a Password in Python
Disclaimer:
Encryption is a critical aspect of data security, and it should be handled with utmost care. Implementing encryption incorrectly can leave your data vulnerable to attack. Consider using well-established libraries and following best practices for secure encryption.
Using a Cryptography Library: Fernet
Python's cryptography library provides a user-friendly and secure solution for encrypting strings using a password. Fernet is a built-in recipe within cryptography that simplifies the encryption process.
Generating a Key:
To use Fernet, you first need to generate a secret key. It is crucial to keep this key a secret.
<code class="python">from cryptography.fernet import Fernet key = Fernet.generate_key() # Store this securely</code>
Encrypting:
<code class="python">from cryptography.fernet import Fernet def encrypt(message: bytes, key: bytes) -> bytes: return Fernet(key).encrypt(message)</code>
Decrypting:
<code class="python">from cryptography.fernet import Fernet def decrypt(token: bytes, key: bytes) -> bytes: return Fernet(key).decrypt(token)</code>
Example Usage:
<code class="python">message = "John Doe" encrypted_token = encrypt(message.encode(), key) decrypted_message = decrypt(encrypted_token, key).decode() print(decrypted_message) # Output: John Doe</code>
Obscuring Data
If you need to obscure data rather than encrypt it, you can use base64 encoding:
<code class="python">import base64 def obscure(data: bytes) -> bytes: return base64.b64encode(data) def unobscure(obscured: bytes) -> bytes: return base64.b64decode(obscured)</code>
Example Usage:
<code class="python">data = b"Hello world!" obscured = obscure(data) unobscured = unobscure(obscured) print(unobscured.decode()) # Output: Hello world!</code>
Verifying Data Integrity
If you need to ensure data integrity without encryption, you can use HMAC signatures:
<code class="python">import hmac import hashlib def sign(data: bytes, key: bytes) -> bytes: return hmac.new(key, data, hashlib.sha256).digest() def verify(signature: bytes, data: bytes, key: bytes) -> bool: return hmac.compare_digest(hmac.new(key, data, hashlib.sha256).digest(), signature)</code>
Example Usage:
<code class="python">data = b"Hello world!" key = secrets.token_bytes(32) signature = sign(data, key) if verify(signature, data, key): print("Signature is valid") else: print("Signature is invalid")</code>
The above is the detailed content of How to Safely Encrypt and Obscure Strings Using Passwords in Python?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Fastapi ...

Using python in Linux terminal...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

About Pythonasyncio...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Discussion on the reasons why pipeline files cannot be written when using Scapy crawlers When learning and using Scapy crawlers for persistent data storage, you may encounter pipeline files...
