1. 程式人生 > >培根密碼加解密

培根密碼加解密

0x00 介紹

培根密碼實際上就是一種替換密碼,根據所給表一一對應轉換即可加密解密
它的特殊之處在於:可以通過不明顯的特徵來隱藏密碼資訊,比如大小寫、正斜體等,只要兩個不同的屬性,密碼即可隱藏

0x01 程式碼實現

指令碼很簡單,就是建立對應關係,對密文,或者明文進行相應的替換即可
需要注意的是輸入的都應該是全小寫字母或全大寫字母,在腳本里也有說明
python指令碼如下:

# coding:utf8

import re

alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q'
,'r','s','t','u','v','w','x','y','z'] first_cipher = ["aaaaa","aaaab","aaaba","aaabb","aabaa","aabab","aabba","aabbb","abaaa","abaab","ababa","ababb","abbaa","abbab","abbba","abbbb","baaaa","baaab","baaba","baabb","babaa","babab","babba","babbb","bbaaa","bbaab"] second_cipher = ["aaaaa","aaaab","aaaba"
,"aaabb","aabaa","aabab","aabba","aabbb","abaaa","abaaa","abaab","ababa","ababb","abbaa","abbab","abbba","abbbb","baaaa","baaab","baaba","baabb","baabb","babaa","babab","babba","babbb"] def encode(): upper_flag = False # 用於判斷輸入是否為大寫 string = raw_input("please input string to encode:\n") if
string.isupper(): upper_flag = True string = string.lower() e_string1 = "" e_string2 = "" for index in string: for i in range(0,26): if index == alphabet[i]: e_string1 += first_cipher[i] e_string2 += second_cipher[i] break if upper_flag: e_string1 = e_string1.upper() e_string2 = e_string2.upper() print "first encode method result is:\n"+e_string1 print "second encode method result is:\n"+e_string2 return def decode(): upper_flag = False # 用於判斷輸入是否為大寫 e_string = raw_input("please input string to decode:\n") if e_string.isupper(): upper_flag = True e_string = e_string.lower() e_array = re.findall(".{5}",e_string) d_string1 = "" d_string2 = "" for index in e_array: for i in range(0,26): if index == first_cipher[i]: d_string1 += alphabet[i] if index == second_cipher[i]: d_string2 += alphabet[i] if upper_flag: d_string1 = d_string1.upper() d_string2 = d_string2.upper() print "first decode method result is:\n"+d_string1 print "second decode method result is:\n"+d_string2 return if __name__ == '__main__': print "\t\tcoding by qux" while True: print "\t*******Bacon Encode_Decode System*******" print "input should be only lowercase or uppercase,cipher just include a,b(or A,B)" print "1.encode\n2.decode\n3.exit" s_number = raw_input("please input number to choose\n") if s_number == "1": encode() raw_input() elif s_number == "2": decode() raw_input() elif s_number == "3": break else: continue

0x02 不足

指令碼的提示是英語,如果想改的話,在每個輸出的地方把輸出的字串編碼轉一下就可以了,比如:
print("你好".decode("utf-8").encode("gbk"))
感覺沒什麼必要……