加密的分类
| 1、单向加密 :MD5、sha系列不可逆 |
| 2、对称加密:AES、DES |
| 3、非对称加密:RSA、DSA |
| 4、补充算法:base64 |
| |
1.md5
| import hashlib |
| m = hashlib.md5() |
| m.update('helloworld'.encode("utf8")) |
| print(m.hexdigest()) |
2.sha
| import hashlib |
| sha1 = hashlib.sha1() |
| data = 'helloword' |
| sha1.update(data.encode('utf-8')) |
| sha1_data = sha1.hexdigest() |
| print(sha1_data) |
3.des加密
| # pip3 install pycryptodomex -i https://pypi.douban.com/simple |
| # DES是一个分组加密算法,典型的DES以64位为分组对数据加密,加密和解密用的是同一个算法。它的密钥长度是56位(因为每个第8 位都用作奇偶校验),密钥可以是任意的56位的数,而且可以任意时候改变。 |
| |
| from Cryptodome.Cipher import DES |
| key = b'88888888' |
| data = "hello world" |
| count = 8 - (len(data) % 8) |
| plaintext = data + count * "=" |
| des = DES.new(key, DES.MODE_ECB) |
| ciphertext = des.encrypt(plaintext.encode()) |
| print(ciphertext) |
| |
| plaintext = des.decrypt(ciphertext) |
| plaintext = plaintext[:(len(plaintext)-count)] |
| print(plaintext) |
4. 非对称加密算法-RSA
| # 安装模块 |
| pip3 install rsa -i https://pypi.douban.com/simple |
| |
| import rsa |
| # 返回 公钥加密、私钥解密 |
| public_key, private_key = rsa.newkeys(1024) |
| print(public_key) |
| print(private_key) |
| |
| # plaintext = b"hello world" |
| # ciphertext = rsa.encrypt(plaintext, public_key) |
| # print('公钥加密后:',ciphertext) |
| # plaintext = rsa.decrypt(ciphertext, private_key) |
| # print('私钥解密:',plaintext) |
| |
| ### 使用私钥签名 |
| plaintext = b"hello world" |
| sign_message = rsa.sign(plaintext, private_key, "MD5") |
| print('私钥签名后:',sign_message) |
| |
| ## 验证私钥签名 |
| plaintext = b"hello world" |
| # method = rsa.verify(b"hello world", sign_message, public_key) |
| method = rsa.verify(b"hello world1", sign_message, public_key) # 报错Verification failed |
| print(method) |
5.base64
| import base64 |
| |
| |
| res=base64.b64encode(b'hello world') |
| print(res) |
| |
| |
| res=base64.b64decode('aGVsbG8gd29ybGQ=') |
| print(res) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
2023-07-18 linux---python虚拟环境配置
2023-07-18 如何部署djiango项目