凯撒加密和栅栏加密 python

凯撒加密

 

# 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

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值