经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 编程经验 » 查看文章
前后端数据的交互--如何实现数据加密?--02
来源:cnblogs  作者:最小生成树  时间:2024/7/31 15:01:38  对本文有异议

数据加密是保护数据安全的重要手段,通过加密技术,我们可以确保即使数据被窃取,也无法直接读取其中的信息。本文将介绍三种常见的加密方法:对称加密、非对称加密以及数据库加密,并展示如何在实际项目中实现这些加密技术。

1. 对称加密

对称加密算法使用相同的密钥进行加密和解密。AES(Advanced Encryption Standard)是目前最广泛使用的对称加密算法之一。

如何实现对称加密

以下是一个使用 AES 进行对称加密和解密的示例,采用 Python 语言和 pycryptodome 库:

  1. from Crypto.Cipher import AES
  2. from Crypto.Random import get_random_bytes
  3. import base64
  4. def pad(s):
  5. return s + (AES.block_size - len(s) % AES.block_size) * chr(AES.block_size - len(s) % AES.block_size)
  6. def unpad(s):
  7. return s[:-ord(s[len(s) - 1:])]
  8. def encrypt(plain_text, key):
  9. key = key.encode('utf-8')
  10. plain_text = pad(plain_text).encode('utf-8')
  11. iv = get_random_bytes(AES.block_size)
  12. cipher = AES.new(key, AES.MODE_CBC, iv)
  13. encrypted_text = cipher.encrypt(plain_text)
  14. return base64.b64encode(iv + encrypted_text).decode('utf-8')
  15. def decrypt(encrypted_text, key):
  16. key = key.encode('utf-8')
  17. encrypted_text = base64.b64decode(encrypted_text)
  18. iv = encrypted_text[:AES.block_size]
  19. cipher = AES.new(key, AES.MODE_CBC, iv)
  20. plain_text = cipher.decrypt(encrypted_text[AES.block_size:])
  21. return unpad(plain_text).decode('utf-8')
  22. key = "thisisaverysecurekey123"
  23. plain_text = "Sensitive Data"
  24.  
  25. # 加密
  26. encrypted_text = encrypt(plain_text, key)
  27. print(f"Encrypted Text: {encrypted_text}")
  28. # 解密
  29. decrypted_text = decrypt(encrypted_text, key)
  30. print(f"Decrypted Text: {decrypted_text}")

 

解释

  • 填充:因为 AES 是块加密算法,明文长度需要是块大小的倍数,所以需要填充。
  • IV(初始化向量):确保每次加密相同的明文时生成不同的密文。
  • 加密和解密:使用相同的密钥进行加密和解密。

2. 非对称加密

非对称加密使用一对密钥:公钥和私钥。公钥用于加密,私钥用于解密。RSA(Rivest-Shamir-Adleman)是最常见的非对称加密算法之一。

如何实现非对称加密

以下是一个使用 RSA 进行非对称加密和解密的示例,采用 Python 语言和 pycryptodome 库:

  1. from Crypto.PublicKey import RSA
  2. from Crypto.Cipher import PKCS1_OAEP
  3. import base64
  4. # 生成 RSA 密钥对
  5. key = RSA.generate(2048)
  6. private_key = key.export_key()
  7. public_key = key.publickey().export_key()
  8. def encrypt(plain_text, public_key):
  9. public_key = RSA.import_key(public_key)
  10. cipher = PKCS1_OAEP.new(public_key)
  11. encrypted_text = cipher.encrypt(plain_text.encode('utf-8'))
  12. return base64.b64encode(encrypted_text).decode('utf-8')
  13. def decrypt(encrypted_text, private_key):
  14. private_key = RSA.import_key(private_key)
  15. encrypted_text = base64.b64decode(encrypted_text)
  16. cipher = PKCS1_OAEP.new(private_key)
  17. plain_text = cipher.decrypt(encrypted_text)
  18. return plain_text.decode('utf-8')
  19. plain_text = "Sensitive Data"
  20.  
  21. # 加密
  22. encrypted_text = encrypt(plain_text, public_key)
  23. print(f"Encrypted Text: {encrypted_text}")
  24. # 解密
  25. decrypted_text = decrypt(encrypted_text, private_key)
  26. print(f"Decrypted Text: {decrypted_text}")

 

解释

  • 密钥生成:生成一对 RSA 密钥,公钥用于加密,私钥用于解密。
  • 加密和解密:使用公钥进行加密,私钥进行解密,确保数据传输的安全性。

3. 数据库加密

数据库加密用于保护存储在数据库中的敏感数据,如用户密码、信用卡信息等。通常,密码需要使用哈希算法进行存储,以确保即使数据库泄露,也无法直接获取用户密码。

如何实现数据库加密

以下是一个使用 bcrypt 进行密码哈希和验证的示例,采用 Python 语言和 bcrypt 库:

  1. import bcrypt
  2. def hash_password(password):
  3. # 生成盐并哈希密码
  4. salt = bcrypt.gensalt()
  5. hashed_password = bcrypt.hashpw(password.encode('utf-8'), salt)
  6. return hashed_password
  7. def check_password(password, hashed_password):
  8. # 验证密码
  9. return bcrypt.checkpw(password.encode('utf-8'), hashed_password)
  10. password = "SecurePassword123"
  11. hashed_password = hash_password(password)
  12. print(f"Hashed Password: {hashed_password}")
  13. # 验证密码
  14. is_correct = check_password(password, hashed_password)
  15. print(f"Password is correct: {is_correct}")

 

解释

  • 生成盐并哈希密码:使用 bcrypt.gensalt() 生成一个随机盐,并将其与密码一起进行哈希。
  • 验证密码:使用 bcrypt.checkpw() 验证输入的密码是否与存储的哈希密码匹配。

原文链接:https://www.cnblogs.com/zx618/p/18333156

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号