注本人使用的是python2.7
直接上代码
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import base64
import random
#加密
def base64_encode(flag):
basencode = {
'16':lambda x:base64.b16encode(x),
'32':lambda x:base64.b32encode(x),
'64':lambda x:base64.b64encode(x)
}
pp=flag.encode('utf-8')
#加密过程
for i in range(10):
order=random.choice(['16','32','64'])
pp=basencode[order](pp)
pp=pp.decode('utf-8')
#将加密的数据存放到文件中
with open("ciphertext.txt",'w') as fp:
fp.write(pp)
return '###加密成功###'
#解密
def base64_decode(ciphertext):
basedecode={
'16':lambda x:base64.b16decode(x),
'32':lambda x:base64.b32decode(x),
'64':lambda x:base64.b64decode(x)
}
#打开解密的文件
with open(ciphertext+".txt",'r') as fp:
ciphertext=fp.read()
ciphertext=ciphertext.encode('utf-8')
for j in range(10):
try:
ciphertext=basedecode['16'](ciphertext)
except:
try:
ciphertext=basedecode['32'](ciphertext)
except:
ciphertext=basedecode['64'](ciphertext)
result=ciphertext.decode('utf-8')
print(result)
return '解密成功'
def main():
functions = 'BS'
print functions
if functions == 'AA':
#加密的内容
plaintext = 'ssss'
print base64_encode(plaintext)
if functions == 'BS':
plaintext = 'ciphertext'
print base64_decode(plaintext)
if __name__ == '__main__':
main()