python 实现aes256 rsa 混合加解密

本文提供了使用Python实现AES256和RSA两种加密算法混合使用的加解密方法。代码需要在同目录下创建特定格式的文本文件,并依赖Python的相关库。加密过程涉及AES256加密后的密文再用RSA加密,解密则是逆序操作。注意,为了程序运行,可能需要调整Python编译器的版本或处理特定的报错设置。
摘要由CSDN通过智能技术生成

新版链接:(31条消息) python 实现aes256 rsa 混合加解密_陆明295的博客-CSDN博客

声明:本代码可能会删除(或改写)某些文件(或文件夹),请小心。但删除的文件(或文件夹)一定在存放代码的py文件的同目录下。本代码需要安装python的库。所以,如果报错,可以尝试安装一下python库。需要在同目录创建 联系人+'(加密).txt'文件和需要加密的文件(此文件不会被删或改下)(加密的话)联系人+'(加密).txt'的格式为:

n(对方的n)

e(对方的e)

n(自己的n)

d(自己的d)

如果解密的话创建 联系人+'(解密).txt' 格式为

n(自己的n)

d(自己的d)

n(对方的n)

e(对方的e)

注:4500>n的位数(十进制)(双方的n的最小值)>5000(加密文字)

200>n的位数(十进制)(双方的n最小值)>5000(加密文件)

此说明可能不是很详细,看不懂的话可以自己看代码

编译器使用python3.11.0或以上的都可以,如果python3.11.0以下的话可以以试一试能不能运行。如果sys.set_int_max_str_digits(5000)报错,就把这行删了(程序中出现的都删了)

加密代码:

import random
import sys
import time
import pyperclip
import struct
import os
import py7zr
from Crypto.Cipher import AES
import operator
import base64

def xc(month_rank_dir):
 
 with py7zr.SevenZipFile(month_rank_dir+'.7z', 'w') as z:
    z.writeall(month_rank_dir)
def base36_encode(number):
    num_str = '0123456789abcdefghijklmnopqrstuvwxyz'
    if number == 0:
        return '0'

    base36 = []
    while number != 0:
        number, i = divmod(number, 36)    # 返回 number// 36 , number%36
        base36.append(num_str[i])
    return ''.join(reversed(base36))
a=[]
bj=[]
AES_BLOCK_SIZE = AES.block_size  # AES 加密数据块大小, 只能是16
AES_KEY_SIZE = 32
key=''
for i in range(32):
    v=random.randint(0,255)
    bn=v
    v=hex(v)
    v=v[2:]
    if(len(v)==1):
        v='0'+v
    key=key+v
    a.append(bn)

iv=''
for i in range(16):
    v=random.randint(0,255)
    bn=v
    v=hex(v)
    v=v[2:]
    if(len(v)==1):
        v='0'+v
    iv=iv+v
    bj.append(bn)
    
#加密内容需要可以被16整除,所以进行空格拼接



def PKCS_zero(text):
    newbytes = '0'
    if len(text) % 32:
        add = 32 - (len(text) % 32)
        
    else:
        add = 0
    text = text + newbytes * add
    return text
    
# 加密函数
def vio(key,iv,text):
    print("AES_CBC_encrypt")
    print(" key  :", key, type(key))
    print(" iv   :", iv, type(iv))
    #print("plain :", text, type(text))

    mode = AES.MODE_CBC
    text = PKCS_zero(text)
    text = bytes.fromhex(text)
    #print("plain :", bytes.hex(text),type(text))
    key = bytes.fromhex(key)
    iv = bytes.fromhex(iv)

    cryptos = AES.new(key, mode, iv)
    cipher_text = bytes.hex(cryptos.encrypt(text))
    # 因为AES加密后的字符串不一定是ascii字符集的,输出保存可能存在问题,所以这里转为16进制字符串

    #print("cipher:", cipher_text, type(cipher_text))
    print("************************************************")
    return cipher_text
    
    


    
def hx (p):
    global key
    global iv
    

    f=open(p,'rb')
    size = os.path.getsize(p)
    g='1'
    cpp=f.read()
    cpp=list(cpp)
    for i in range(size):
        c=str(bin(cpp[i]).replace('0b',''))
        while len(c)!=8:
            c='0'+c
        g=g+c
    po=vio(key,iv,g)
    return po

def oo():
 sys.set_int_max_str_digits(5000)
 ooo=input('请输入联系人:')
 ccc=ooo+"(加密).txt"
 ff=open(ccc)
 print("请输入要加密的数据:",end='')
 o=input('')
 df = pyperclip.paste()
 while 1:
    test = "哦74人工会3非用246骨灰级の|}{54657326482356765432792以698398i天弘基金ijijihi急急急ii几句hi基金i户籍ijiu及hi就ihi ihi就ihiiji及hi九九iji及uu948958959949958怕怕’、!@#%……&……%¥¥¥###¥##看看风景给;提开发开放吗看见没iffjgokigfuhiojsok34596789ij8igidki;f;.,l.;f,gl.l"
    pyperclip.copy(test)
    io=input('')
    if(io=='哦74人工会3非用246骨灰级の|}{54657326482356765432792以698398i天弘基金ijijihi急急急ii几句hi基金i户籍ijiu及hi就ihi ihi就ihiiji及hi九九iji及uu948958959949958怕怕’、!@#%……&……%¥¥¥###¥##看看风景给;提开发开放吗看见没iffjgokigfuhiojsok34596789ij8igidki;f;.,l.;f,gl.l'):
        break
    o+='''
'''
    o+=io
 print("输入完成")
 pyperclip.copy(df)

 mmm=time.time()
 n1=ff.readline()
 n1=int(n1)
 e=ff.readline()
 e=int(e)
 n2=ff.readline()
 n2=int(n2)
 d=ff.readline()
 d=int(d)
 m=200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值