凱撒加密和柵欄加密 python
阿新 • • 發佈:2019-01-27
凱撒加密
# lzdqwbrx def casearDecrypt(ciphertext, source_char, destination_char, list_all): if list_all == True: for offset in range(1, 27): convertChar(offset) else: offset = ord(destination_char) - ord(source_char) convertChar(offset) def convertChar(offset): chars = "abcdefghijklmnopqrstuvwxyz" for char in ciphertext: is_upper_flag = 0 if char.isupper(): char = char.lower() is_upper_flag = 1 if char not in chars: outputChar(is_upper_flag, char) continue tempchar_ascii = ord(char) + offset tempchar =chr(tempchar_ascii) if tempchar not in chars: if offset < 0: tempchar_ascii += len(chars) else: tempchar_ascii -= len(chars) tempchar = chr(tempchar_ascii) outputChar(is_upper_flag, tempchar) print("") def outputChar(is_upper_flag, char): if is_upper_flag == 1: print (char.upper()), else: print (char), ciphertext = raw_input("Please input ciphertext:\n") while True: operation = raw_input("List all results?y/n:") if operation == 'y' or operation == 'Y': casearDecrypt(ciphertext, '', '', True) break elif operation == 'n' or operation == 'N': des_char = raw_input("Please input destination_char:\n") sors_char = raw_input("Please input source_char:\n") casearDecrypt(ciphertext, sors_char, des_char, False) break else: print("Input error! Please input y/n:")
柵欄加密
#!/usr/bin/env python # -*- coding: gbk -*- # -*- coding: utf_8 -*- # Author: 蔚藍行 # http://www.cnblogs.com/duanv e = raw_input('請輸入要解密的字串\n') elen = len(e) field=[] for i in range(2,elen): if(elen%i==0): field.append(i) for f in field: b = elen / f result = {x:'' for x in range(b)} for i in range(elen): a = i % b; result.update({a:result[a] + e[i]}) d = '' for i in range(b): d = d + result[i] print '分為\t'+str(f)+'\t'+'欄時,解密結果為: '+d