weixin_41821141 2021-05-08 14:00 采纳率: 0%
浏览 114

JAVA AES对称加密为CBC MCRYPT_RIJNDAEL_128)对MD5加密 点晴API

#!/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 = "Ikongjian2017@360"
    API_SECRET = "c205b9ed6750bbabd38ba183cef79a79"  # 点睛提供
    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)
  • 写回答

2条回答 默认 最新

  • weixin_41821141 2021-05-08 14:00
    关注

    如何实现?

    评论

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功