失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python 凯撒加密 计算偏移量

python 凯撒加密 计算偏移量

时间:2023-04-23 13:26:45

相关推荐

python 凯撒加密 计算偏移量

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

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

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

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

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

import stringdef 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=0count1=0count2=0count3=0count4=0L = txt.__len__();for i in range(L):if txt[i]>='A' and txt[i]<='Z':count+=1elif txt[i]>='a' and txt[i]<='z':count1+=1elif txt[i]>='0' and txt[i]<='9':count2+=1elif txt[i]==" ":count3+=1else :count4+=1return (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 strdef cal_offset(key_word):sum = 0for i in key_word:sum+=ord(i)sum = sum%9return sumdef 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 wif __name__ == '__main__':while True:again = input("按任意键继续,结束请输入'no':")if again == 'no':breakfilename = 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>> 输入一个用于计算偏移量的单词:ecutUsjfe 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 .>> 按任意键继续,结束请输入'no':no'''

如果觉得《python 凯撒加密 计算偏移量》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。