**360点睛平台,密码加密-python版**

该博客介绍了一个Python实现的加密类,用于使用MD5加密密码,并通过AES进行CBC模式的加密和解密。API_SECRET作为密钥和密钥向量,确保了数据的安全传输。代码中详细展示了加密和解密的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import hashlib
from Crypto.Cipher import AES
import base64
from binascii import b2a_hex

def create_md5_pwd(password):
    m = hashlib.md5()
    b = password.encode(encoding='utf-8')
    m.update(b)
    md5_pwd = m.hexdigest()
    return md5_pwd

class PrpCrypt(object):

    def __init__(self, API_SECRET):
        self.key = API_SECRET[:16].encode('gbk')  # 密匙
        self.iv = API_SECRET[16:].encode('gbk')  # 密匙向量

    def encrypt(self,text):
        # 加密
        mycipher = AES.new(self.key, AES.MODE_CBC, self.iv)
        # 加密的明文长度必须为16的倍数,如果长度不为16的倍数,则需要补足为16的倍数
        # 将iv(密钥向量)加到加密的密文开头,一起传输
        ciphertext = self.iv + mycipher.encrypt(text.encode())
        return ciphertext  # 加密

    def decrypt(self,text):
        # 解密
        mydecrypt = AES.new(self.key, AES.MODE_CBC, text[:16])
        decrypttext = mydecrypt.decrypt(text[16:])
        decrypt_pwd = decrypttext.decode()  # 解密后数据
        return decrypt_pwd


if __name__ == '__main__':
    password = '密码11111111'
    API_SECRET = "22222222222"  # 点睛提供
    text = create_md5_pwd(password)
    pc = PrpCrypt(API_SECRET)  # 初始化密匙
    ciphertext = pc.encrypt(text)
    e = b2a_hex(ciphertext)[32:].decode()
    d = pc.decrypt(ciphertext)
    print('加密后:' + e)
    print('解密后:' + d)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值