鱼C论坛

 找回密码
 立即注册
查看: 2050|回复: 5

[技术交流] 【Python】Web开发【5】

[复制链接]
发表于 2020-3-13 18:38:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 LYF511 于 2020-3-20 20:30 编辑

今天我们来继续学习Python Web开发【5】

ps:本课程没有特别深奥的地方(如:数据库等),会Python和一点html基础就可以学习!

题外话:这个主题好像有点冷门。。。

没关系,我们继续做


今天,我们来完善第4课的实例

1.加密数据

首先,我们了解一下流行的加密方法,如图:


                               
登录/注册后可看大图


现在,你应该知道我们今天要用什么加密算法了


AES

参考资料

方便需要,我们先写个类,并且测试:

要安装第三方模块:Crypto 教程


  1. import base64
  2. from Crypto.Cipher import AES

  3. class aes:
  4.     def Add(self, s):
  5.         while len(s) % 16 != 0:
  6.             s += '\0'
  7.         return str.encode(s)

  8.     def __init__(self, key, mode):
  9.         if len(key) not in (16, 24, 32):
  10.             raise KeyError("Key's length must in (16, 24, 32)")
  11.         self.aes = AES.new(str.encode(key), mode)

  12.     def encrypted(self, text):
  13.         text = self.Add(text)
  14.         encryptedText = base64.encodebytes(self.aes.encrypt(text))
  15.         return str(encryptedText, encoding='utf8').replace('\n', '')

  16.     def decrypted(self, text):
  17.         text = bytes(text, encoding='utf8')
  18.         decryptedText = self.aes.decrypt(base64.decodebytes(text)).rstrip(b'\0').decode("utf8")
  19.         return str(decryptedText)

  20. key = '1234567890123456'  # 密钥长度必须为16、24或32位,分别对应AES-128、AES-192和AES-256
  21. text = 'abcdefg'  # 待加密文本

  22. Aes = aes(key, AES.MODE_ECB)
  23. encrypted_text = Aes.encrypted(text)
  24. decrypted_text = Aes.decrypted(encrypted_text)

  25. print('加密值:', encrypted_text)
  26. print('解密值:', decrypted_text)
  27. print('Key :', key)
  28. print('Text', text)
  29. print(decrypted_text == text)
复制代码

由于我们需要Key,固定那太容易被别人发现了,所有我们来一个随机Key:

参考资料

  1. import base64
  2. import string
  3. import random
  4. from Crypto.Cipher import AES

  5. class aes:
  6.     def Add(self, s):
  7.         while len(s) % 16 != 0:
  8.             s += '\0'
  9.         return str.encode(s)

  10.     def __init__(self, key, mode):
  11.         if len(key) not in (16, 24, 32):
  12.             raise KeyError("Key's length must in (16, 24, 32)")
  13.         self.aes = AES.new(str.encode(key), mode)

  14.     def encrypted(self, text):
  15.         text = self.Add(text)
  16.         encryptedText = base64.encodebytes(self.aes.encrypt(text))
  17.         return str(encryptedText, encoding='utf8').replace('\n', '')

  18.     def decrypted(self, text):
  19.         text = bytes(text, encoding='utf8')
  20.         decryptedText = self.aes.decrypt(base64.decodebytes(text)).rstrip(b'\0').decode("utf8")
  21.         return str(decryptedText)

  22. key = ''.join(random.sample(string.ascii_letters + string.digits, 16))

  23. text = ''.join(random.sample(string.ascii_letters + string.digits, random.randint(10, 50)))

  24. Aes = aes(key, AES.MODE_ECB)
  25. encrypted_text = Aes.encrypted(text)
  26. decrypted_text = Aes.decrypted(encrypted_text)

  27. print('加密值:', encrypted_text)
  28. print('解密值:', decrypted_text)
  29. print('Key :', key)
  30. print('Text', text)
  31. print(decrypted_text == text)
复制代码

有了这些基础,这个就简单,改一改main.py就行

出于安全性考虑,每次登陆或者注册都改一次密钥,base64加密保存

鱼油们先想一想,再看答案哟!

游客,如果您要查看本帖隐藏内容请回复


2.做的真正登录

待更新。。。

评分

参与人数 2荣誉 +1 鱼币 +1 贡献 +1 收起 理由
一个账号 + 1 加油!
乘号 + 1 + 1 鱼C有你更精彩^_^

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-13 18:55:15 | 显示全部楼层
是pycryptodome模块
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-13 19:05:18 | 显示全部楼层
支持楼主
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-13 19:34:13 | 显示全部楼层

emmm不是那个,是这个,不信上机
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-13 19:34:42 | 显示全部楼层
LYF511 发表于 2020-3-13 19:34
emmm不是那个,是这个,不信上机

我知道,我说用pip安装的时候
是pycryptodome
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-13 19:35:25 | 显示全部楼层
qiuyouzhi 发表于 2020-3-13 19:34
我知道,我说用pip安装的时候
是pycryptodome

放了链接的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-10 14:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表