前后端數據的交互--如何實現數據加密?--02
數據加密是保護數據安全的重要手段,通過加密技術,我們可以確保即使數據被竊取,也無法直接讀取其中的信息。本文將介紹三種常見的加密方法:對稱加密、非對稱加密以及數據庫加密,并展示如何在實際項目中實現這些加密技術。
1. 對稱加密
對稱加密算法使用相同的密鑰進行加密和解密。AES(Advanced Encryption Standard)是目前最廣泛使用的對稱加密算法之一。
如何實現對稱加密
以下是一個使用 AES 進行對稱加密和解密的示例,采用 Python 語言和 pycryptodome 庫:
from Crypto.Cipher import AES from Crypto.Random 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 = key.encode('utf-8') plain_text = pad(plain_text).encode('utf-8') iv = get_random_bytes(AES.block_size) cipher = AES.new(key, AES.MODE_CBC, iv) encrypted_text = cipher.encrypt(plain_text) return base64.b64encode(iv + encrypted_text).decode('utf-8') def decrypt(encrypted_text, key): key = key.encode('utf-8') encrypted_text = base64.b64decode(encrypted_text) iv = encrypted_text[:AES.block_size] cipher = AES.new(key, AES.MODE_CBC, iv) plain_text = cipher.decrypt(encrypted_text[AES.block_size:]) return unpad(plain_text).decode('utf-8') key = "thisisaverysecurekey123" plain_text = "Sensitive Data" # 加密 encrypted_text = encrypt(plain_text, key) print(f"Encrypted Text: {encrypted_text}") # 解密 decrypted_text = decrypt(encrypted_text, key) print(f"Decrypted Text: {decrypted_text}")
解釋
- 填充:因為 AES 是塊加密算法,明文長度需要是塊大小的倍數,所以需要填充。
- IV(初始化向量):確保每次加密相同的明文時生成不同的密文。
- 加密和解密:使用相同的密鑰進行加密和解密。
2. 非對稱加密
非對稱加密使用一對密鑰:公鑰和私鑰。公鑰用于加密,私鑰用于解密。RSA(Rivest-Shamir-Adleman)是最常見的非對稱加密算法之一。
如何實現非對稱加密
以下是一個使用 RSA 進行非對稱加密和解密的示例,采用 Python 語言和 pycryptodome 庫:
from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP import base64 # 生成 RSA 密鑰對 key = RSA.generate(2048) private_key = key.export_key() public_key = key.publickey().export_key() def encrypt(plain_text, public_key): public_key = RSA.import_key(public_key) cipher = PKCS1_OAEP.new(public_key) encrypted_text = cipher.encrypt(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 = cipher.decrypt(encrypted_text) return plain_text.decode('utf-8') plain_text = "Sensitive Data" # 加密 encrypted_text = encrypt(plain_text, public_key) print(f"Encrypted Text: {encrypted_text}") # 解密 decrypted_text = decrypt(encrypted_text, private_key) print(f"Decrypted Text: {decrypted_text}")
解釋
- 密鑰生成:生成一對 RSA 密鑰,公鑰用于加密,私鑰用于解密。
- 加密和解密:使用公鑰進行加密,私鑰進行解密,確保數據傳輸的安全性。
3. 數據庫加密
數據庫加密用于保護存儲在數據庫中的敏感數據,如用戶密碼、信用卡信息等。通常,密碼需要使用哈希算法進行存儲,以確保即使數據庫泄露,也無法直接獲取用戶密碼。
如何實現數據庫加密
以下是一個使用 bcrypt 進行密碼哈希和驗證的示例,采用 Python 語言和 bcrypt 庫:
import bcrypt def hash_password(password): # 生成鹽并哈希密碼 salt = bcrypt.gensalt() hashed_password = bcrypt.hashpw(password.encode('utf-8'), salt) return hashed_password def check_password(password, hashed_password): # 驗證密碼 return bcrypt.checkpw(password.encode('utf-8'), hashed_password) password = "SecurePassword123" hashed_password = hash_password(password) print(f"Hashed Password: {hashed_password}") # 驗證密碼 is_correct = check_password(password, hashed_password) print(f"Password is correct: {is_correct}")
解釋
- 生成鹽并哈希密碼:使用
bcrypt.gensalt()生成一個隨機鹽,并將其與密碼一起進行哈希。 - 驗證密碼:使用
bcrypt.checkpw()驗證輸入的密碼是否與存儲的哈希密碼匹配。

浙公網安備 33010602011771號