多重base64加密解密

单次base64编码不安全

base64只是一种编码,严格来说,不算是加密,只能"防君子不防小人",当然多次也一样的,虽然多次编码后不容易被一下子翻译成明文,但是仍然是不安全的,本篇只论 base 编码,不讨论加密。

多重随机加密

将需要编码的内容放入 src.txt 中,运行之后,会将编码后的内容保存在 magic.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('src.txt', 'r', encoding='UTF-8') as f:	#读取需要编码的内容
    s="".join(f.readlines()).encode('utf-8')  
    
for i in range(random.randint(3,10)):				#随机循环
    s=process[random.choice(['16','32','64'])](s)	#随机编码
    
with open('magic.txt','w', encoding='UTF-8') as f:	#保存编码后的结果
    f.write(str(s, 'utf-8'))    

解密

由于只用到了 base16 base32 base64 编码,只需要进行尝试解码就行了,无论加密时进行了多少次编码

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

s=''
with open('magic.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('result.txt','w', encoding='utf-8') as file:
    file.write(str(src,'utf-8'))
print("ok!")    

猜你喜欢

转载自blog.csdn.net/qq_35425070/article/details/89020942