前后端数据的交互--如何实现数据加密?--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() 验证输入的密码是否与存储的哈希密码匹配。
相关推荐
皮卡蛋炒饭.16 分钟前
数据结构—排序
数据结构·算法·排序算法
??tobenewyorker1 小时前
力扣打卡第23天 二叉搜索树中的众数
数据结构·算法·leetcode
贝塔西塔1 小时前
一文读懂动态规划:多种经典问题和思路
算法·leetcode·动态规划
众链网络2 小时前
AI进化论08:机器学习的崛起——数据和算法的“二人转”,AI“闷声发大财”
人工智能·算法·机器学习
2 小时前
Unity开发中常用的洗牌算法
java·算法·unity·游戏引擎·游戏开发
飒飒真编程3 小时前
C++类模板继承部分知识及测试代码
开发语言·c++·算法
GeminiGlory4 小时前
算法练习6-大数乘法(高精度乘法)
算法
熬了夜的程序员4 小时前
【华为机试】HJ61 放苹果
算法·华为·面试·golang
马特说4 小时前
基于随机森林的金融时间序列预测系统:从数据处理到实时预测的完整流水线
算法·随机森林·金融
呆呆的小鳄鱼4 小时前
leetcode:HJ18 识别有效的IP地址和掩码并进行分类统计[华为机考][字符串]
算法·leetcode·华为