前后端数据的交互--如何实现数据加密?--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() 验证输入的密码是否与存储的哈希密码匹配。
posted @ 2024-07-30 18:42  最小生成树  阅读(10)  评论(0编辑  收藏  举报