python 使用base64混合加密

注本人使用的是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()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章