python-编写base64解码加密脚本(可多次加密或者解密)

这篇博客介绍了如何使用Python进行base64编码和解码。通过随机选择16进制、32进制或64进制编码方式,对1.txt文件的内容进行多次编码,并将结果写入2.txt。解密过程则是尝试使用b16decode、b32decode和b64decode逐一解码,直到成功,最后将原始内容保存到321.txt。
摘要由CSDN通过智能技术生成

base64加密

用到的python模块:random、base64
1.txt:需要编码的内容
2.txt:编码后的内容
直接上代码

# -*- coding:utf-8 -*-
import random
from base64 import *

process = {
    '16': lambda x: b16encode(x),
    '32': lambda x: b32encode(x),
    '64': lambda x: b64encode(x),
}
s = ""
with open('1.txt', 'r', encoding='UTF-8') as f:  # 读取需要编码的内容
    s = "".join(f.readlines()).encode('utf-8')

for i in range(random.randint(10, 20)):  # 随机循环
    s = process[random.choice(['16', '32', '64'])](s)  # 随机编码

with open('2.txt', 'w', encoding='UTF-8') as f:  # 保存编码后的结果
    f.write(str(s, 'utf-8'))

base64 解密

代码如下:

#-*- coding:utf-8 -*-
import base64

s=''
with open('easybase-4-现代.txt', 'r', encoding='UTF-8') as f:
    s="".join(f.readlines()).encode('utf-8')
src=s
while True:
    try:
        src=s
        s=base64.b16decode(s)
        str(s,'utf-8')
        continue
    except:
        pass
    try:
        src=s
        s=base64.b32decode(s)
        str(s,'utf-8')
        continue
    except:
        pass
    try:
        src=s
        s=base64.b64decode(s)
        str(s,'utf-8')
        continue
    except:
        pass
    break
with open('321.txt','w', encoding='utf-8') as file:
    file.write(str(src,'utf-8'))
print("ok!")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值