python 凯撒加密 计算偏移量

接收一个字符串method为参数,根据参数值调用不同函数实现对文本的加密和解密。

method 值为 '统计字符'时,统计并输出文本中不同类型字符的数量并输出。

method 值为 '统计单词'时,统计并输出文本中单词的数量。

method 值为 '首字符'时,将所有单词的首字符提取出来拼接为一个字符串输出。

method 值为 '凯撒加密'时,再输入一个单词做为密钥发生器,用于计算偏移量,对文件中的内容进行加密并输出。

import string


def judge(method, txt):
    if method == '1':
        classify = classify_char(text)
        print('大写字母{}个,小写字母{}个,数字{}个,空格{}个,其他{}个'.format(*classify))
    elif method == '2':
        word_lst = word_list(text)  # 单词的列表
        words_counts = number_of_words(word_lst)
        print(f'共有{words_counts}单词')
    elif method == '3':
        word_lst = word_list(text)  # 单词的列表
        new_str = first_letter(word_lst)
        print(f'拼接的字符串是{new_str}')
    elif method == '4':
        key = input('输入一个用于计算偏移量的单词:')  # 输入一个用于计算偏移量的单词
        offset = cal_offset(key)  # 计算偏移量
        print(caesar_cipher(text, offset))  # 加密
    else:
        print('输入错误')

def read_file(file):
    with open(f'D:\pycharm\PycharmProjects/{file}.txt', 'r', encoding='utf-8') as f:
        return f.read()  # 读取文件中的内容为一个字符串

def classify_char(txt):
    count=0
    count1=0
    count2=0
    count3=0
    count4=0
    L = txt.__len__();
    for i in range(L):
        if txt[i]>='A' and txt[i]<='Z':
            count+=1
        elif txt[i]>='a' and txt[i]<='z':
            count1+=1
        elif txt[i]>='0' and txt[i]<='9':
            count2+=1
        elif txt[i]==" ":
            count3+=1
        else :
            count4+=1

    return (count,count1,count2,count3,count4)

def word_list(txt):
    m = '!"#$%&()*+,-.:;<=>?@[\\]^_‘{|}~/'
    for f in txt:
        if f in m:
            txt = txt.replace(f, ' ')
    return txt.split()

def number_of_words(ls):

    return len(ls)


def first_letter(ls):
    str=''
    for i in ls:
        str+=i[0]
    return str


def cal_offset(key_word):
    sum = 0
    for i in key_word:
        sum+=ord(i)
    sum = sum%9
    return sum


def caesar_cipher(text, offset):
    s1='abcdefghijklmnopqrstuvwxyz'
    s2=s1.upper()
    s3=[0,1,2,3,4,5,6,7,8,9]
    s=[]
    for i in text:
        if i>='a' and i<='z':
            for k in range(s1.__len__()):
                if i==s1[k]:
                    s.append(s1[(k+offset)%25])
        elif i>='A' and i<='Z':
            for k in range(s2.__len__()):
                if i==s2[k]:
                    s.append(s2[(k+offset)%25])
        elif i>='0' and i<='9':
            for k in range(s3.__len__()):
                if int(i)==s3[k]:
                    s.append(str(s3[(k+offset)%10]))
        else:
            s.append(i)
    w = ''.join(s)
    return w




if __name__ == '__main__':

    while True:

        again = input("按任意键继续,结束请输入'no':")

        if again == 'no':
            break

        filename = input('请输入要处理的文件名:')  # 输入要处理的文件名

        methods = input("输入要进行的操作编号(1.'统计字符';2.'统计单词';3.'首字符';4.'凯撒加密'):")  # 输入要进行的操作

        text = read_file(filename)  # text为读文件获得的字符串

        judge(methods, text)  # 调用函数,输出处理结果

'''

示例如下:

>> 按任意键继续,结束请输入'no':

>> 请输入要处理的文件名:my

>> 输入要进行的操作编号(1.'统计字符';2.'统计单词';3.'首字符';4.'凯撒加密'):1

    大写字母16个,小写字母306个,数字11个,空格84个,其他17个

>> 按任意键继续,结束请输入'no':

>> 请输入要处理的文件名:my

>> 输入要进行的操作编号(1.'统计字符';2.'统计单词';3.'首字符';4.'凯撒加密'):2

    共有85单词

>> 按任意键继续,结束请输入'no':

>> 请输入要处理的文件名:my

>> 输入要进行的操作编号(1.'统计字符';2.'统计单词';3.'首字符';4.'凯撒加密'):3

    拼接的字符串是T3tfuAfIafHf1tAftdewtsmFtltIwtttcwwcttofcomcIajf3tGrIwsdattIwsfBItituMpw1amBIcnfagji1

>> 按任意键继续,结束请输入'no':

>> 请输入要处理的文件名:my

>> 输入要进行的操作编号(1.'统计字符';2.'统计单词';3.'首字符';4.'凯撒加密'):4

>> 输入一个用于计算偏移量的单词:ecut

    Usjfe 4 ujnft gps vojwfstjuz. Bmm gbjmfe.

    J bqqmjfe gps Ibswbse gps 21 ujnft. Bmm gbjmfe, uifz epo’u fwfo xbou up tff nf.

    Gps uif mbtu ujnf, J xfou up uif ufbdifst’ dpmmfhf xijdi xbt dpotjefsfe uif uijse ps gpvsui dmbtt pg nz djuz.

    J bqqmjfe kpct gps 41 ujnft. Hpu sfkfdufe.

    Ju xbt tp ejggjdvmu bu uibu ujnf, J xbt tp gsvtusbufe.

    Cfdbvtf J ubvhiu jo uif vojwfstjuz. Nz qbz xbt $21 b npoui.

    Cfdbvtf J dpvme opu gjoe b hppe kpc jo 2005.

>> 按任意键继续,结束请输入'no':no

'''

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值