#!/usr/bin/env python
# -*- coding=utf-8 -*-
"""
AES加密解密工具类
数据块128位
key 为16位
iv 为16位,且与key相等
字符集utf-8
输出为base64
AES加密模式 为cbc
填充 pkcs7padding
"""
from Crypto.Cipher import AES
from Crypto.Cipher import DES
from pyDes import des, CBC, PAD_PKCS5
import binascii
import struct
import base64
import random
import sys
from pyDes import *
from binascii import b2a_hex, a2b_hex
def pkcs7padding(text):
    """
    明文使用PKCS7填充
    最终调用AES加密方法时,传入的是一个byte数组,要求是16的整数倍,因此需要对明文进行处理
    :param text: 待加密内容(明文)
    :return:
    """
    bs = AES.block_size  # 16
    length = len(text)
    bytes_length = len(bytes(text, encoding='utf-8'))
    # tips:utf-8编码时,英文占1个byte,而中文占3个byte
    padding_size = length if(bytes_length == length) else bytes_length
    padding = bs - padding_size % bs
    # tips:chr(padding)看与其它语言的约定,有的会使用'\0'
    padding_text = chr(padding) * padding
    return text + padding_text
def pkcs7unpadding(text):
    """
    处理使用PKCS7填充过的数据
    :param text: 解密后的字符串
    :return:
    """
    length = len(text)
    unpadding = ord(text[length-1])
    return text[0:length-unpadding]


def encrypt(key, content):
    """
    AES加密
    key,iv使用同一个
    模式cbc
    填充pkcs7
    :param key: 密钥
    :param content: 加密内容
    :return:
    """
    key_bytes = bytes(key, encoding='utf-8')
    iv = key_bytes
    cipher = AES.new(key_bytes, AES.MODE_CBC, iv)
    # 处理明文
    content_padding = pkcs7padding(content)
    # 加密
    encrypt_bytes = cipher.encrypt(bytes(content_padding, encoding='utf-8'))
    # 重新编码
    result = str(base64.b64encode(encrypt_bytes), encoding='utf-8')
    by = bytes(result, 'UTF-8')  # 先将输入的字符串转化成字节码
    hexstring = by.hex()  # 得到16进制字符串,不带0x
    return hexstring



def decrypt(key, content):
    key_bytes = bytes(key, encoding='utf-8')
    iv = key_bytes
    cipher = AES.new(key_bytes, AES.MODE_CBC, iv)
    # base64解码
    encrypt_bytes = base64.b64decode(content)
    # 解密
    decrypt_bytes = cipher.decrypt(encrypt_bytes)
    # 重新编码
    result = str(decrypt_bytes, encoding='utf-8')
    # 去除填充内容
    result = pkcs7unpadding(result)
    return result



def des_encrypt(key,content):
  """
  DES 加密
  :param s: 原始字符串
  :return: 加密后字符串,16进制
  """
  key_bytes = bytes(key, encoding='utf-8')
  iv = key_bytes
  cipher = DES.new(key_bytes, DES.MODE_CBC, iv)
  # 处理明文
  content_padding = pkcs7padding(content)
  # 加密
  encrypt_bytes = cipher.encrypt(bytes(content_padding, encoding='utf-8'))
  # 重新编码
  result = str(base64.b64encode(encrypt_bytes), encoding='utf-8')
  by = bytes(result, 'UTF-8')  # 先将输入的字符串转化成字节码
  hexstring = by.hex()  # 得到16进制字符串,不带0x
  return hexstring
  return result





count=0
while (count<1000000):
    count = count + 1
    def get_source(n):
        """
        获取明文
        """
        c_length = int(n)
        source = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
        length = len(source) - 1
        result = ''
        for i in range(c_length):
            result += source[random.randint(0, length)]
        return result
    def get_key(n):
        """
        获取密钥 n 密钥长度
        :return:
        """
        c_length = int(n)
        source = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
        length = len(source) - 1
        result = ''
        for i in range(c_length):
            result += source[random.randint(0, length)]
        return result
    '''number = random.randint(1, 100)'''
    result=get_key(8)
    source_en = get_source(16)


    '''encrypt_en = encrypt(result, source_en)
    print(encrypt_en)
    file = open("E:\AES.txt", "a")
    file.write(encrypt_en)
    file.write('\n')'''



    desencrypten=des_encrypt(result,source_en)
    print(desencrypten)
    file = open("E:\DES.txt", "a")
    file.write(desencrypten)
    file.write('\n')

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐