Data encryption is an important means of securing data. With encryption, we can ensure that even if the data is stolen, the information in it cannot be read directly. This article will introduce three common encryption methods: symmetric encryption, asymmetric encryption, and database encryption, and show how to implement these encryption techniques in real projects.
1. Symmetric encryption
Symmetric encryption algorithms use the same key for encryption and decryption.AES (Advanced Encryption Standard) is one of the most widely used symmetric encryption algorithms.
How to implement symmetric encryption
The following is an example of symmetric encryption and decryption using AES, using the Python language and thepycryptodome
Coop:
from import AES from import get_random_bytes import base64 def pad(s): return s + (AES.block_size - len(s) % AES.block_size) * chr(AES.block_size - len(s) % AES.block_size) def unpad(s): return s[:-ord(s[len(s) - 1:])] def encrypt(plain_text, key): key = ('utf-8') plain_text = pad(plain_text).encode('utf-8') iv = get_random_bytes(AES.block_size) cipher = (key, AES.MODE_CBC, iv) encrypted_text = (plain_text) return base64.b64encode(iv + encrypted_text).decode('utf-8') def decrypt(encrypted_text, key): key = ('utf-8') encrypted_text = base64.b64decode(encrypted_text) iv = encrypted_text[:AES.block_size] cipher = (key, AES.MODE_CBC, iv) plain_text = (encrypted_text[AES.block_size:]) return unpad(plain_text).decode('utf-8') key = "thisisaverysecurekey123" plain_text = "Sensitive Data" # encrypted encrypted_text = encrypt(plain_text, key) print(f"Encrypted Text: {encrypted_text}") # declassification decrypted_text = decrypt(encrypted_text, key) print(f"Decrypted Text: {decrypted_text}")
account for
- padding: Because AES is a block encryption algorithm, the plaintext length needs to be a multiple of the block size, so padding is required.
- IV (initialization vector): Ensure that a different ciphertext is generated each time the same plaintext is encrypted.
- Encryption and decryption: Encrypt and decrypt using the same key.
2. Asymmetric encryption
Asymmetric encryption uses a pair of keys: a public key and a private key. The public key is used for encryption and the private key is used for decryption.RSA (Rivest-Shamir-Adleman) is one of the most common asymmetric encryption algorithms.
How to implement asymmetric encryption
The following is an example of asymmetric encryption and decryption using RSA, using the Python language and thepycryptodome
Coop:
from import RSA from import PKCS1_OAEP import base64 # Generate RSA key pairs key = (2048) private_key = key.export_key() public_key = ().export_key() def encrypt(plain_text, public_key): public_key = RSA.import_key(public_key) cipher = PKCS1_OAEP.new(public_key) encrypted_text = (plain_text.encode('utf-8')) return base64.b64encode(encrypted_text).decode('utf-8') def decrypt(encrypted_text, private_key): private_key = RSA.import_key(private_key) encrypted_text = base64.b64decode(encrypted_text) cipher = PKCS1_OAEP.new(private_key) plain_text = (encrypted_text) return plain_text.decode('utf-8') plain_text = "Sensitive Data" # encrypted encrypted_text = encrypt(plain_text, public_key) print(f"Encrypted Text: {encrypted_text}") # declassification decrypted_text = decrypt(encrypted_text, private_key) print(f"Decrypted Text: {decrypted_text}")
account for
- Key Generation: Generate a pair of RSA keys, a public key for encryption and a private key for decryption.
- Encryption and decryption: Encryption is performed using a public key and decryption is performed using a private key to ensure the security of data transmission.
3. Database encryption
Database encryption is used to protect sensitive data stored in databases, such as user passwords, credit card information, etc. Typically, passwords need to be stored using a hash algorithm to ensure that even if the database is compromised, there is no direct access to user passwords.
How to implement database encryption
The following is an example of how to use thebcrypt
Example of performing password hashing and authentication, using the Python language and thebcrypt
Coop:
import bcrypt def hash_password(password): # Generate salt and hash password salt = () hashed_password = (('utf-8'), salt) return hashed_password def check_password(password, hashed_password): # Verify Password return (('utf-8'), hashed_password) password = "SecurePassword123" hashed_password = hash_password(password) print(f"Hashed Password: {hashed_password}") # Verify Password is_correct = check_password(password, hashed_password) print(f"Password is correct: {is_correct}")
account for
-
Generate salt and hash password: Use
()
Generate a random salt and hash it with the password. -
Verify Password: Use
()
Verifies that the entered password matches the stored hashed password.