凯撒加密和栅栏加密 python

来源:互联网 发布:网络电子游艺漏洞 编辑:程序博客网 时间:2024/04/29 21:10

凯撒加密

# lzdqwbrxdef 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/duanve = 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


0 0