


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

AI Hentai Generator
Generate AI Hentai for free.

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



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

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...

Fastapi ...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.
