南京邮电大学《密码学》实验(一)转轮机的模拟实现(python版)(仅参考)

实验名称:转轮机的模拟实现

一、课题内容和要求

       转轮机由一个键盘和一系列转轮组成,每个转轮是26个字母的任意组合。转轮被齿轮连接起来,当一个转轮转动时,可以将一个字母转换成另一个字母。照此传递下去,当最后一个转轮处理完毕时,就可以得到加密后的字母。

       本实验即是通过编写程序模拟转轮机的实现

二、实现分析

转轮机的功能框架图如图1所示。

图1 转轮机功能框架

图 2 转轮加密算法初态

(1)首先定义了六个数组,即三组轮子,分别为慢轮左、慢轮右、中轮左、中轮右、快轮左、快轮右,用于存储转轮的初始状态。

(2)接着将密文的字符挨个输入,如果判断是字母则进行加密处理,同时计数。转轮的转动情况便是基于字符输入的次数,如果达到26的倍数,则中轮转动,如果达到26*26的倍数,则慢轮转动

三、概要设计

1 主要存储结构

#慢轮子
Slow_L = [24,25,26,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
Slow_R = [
21,3,15,1,19,10,14,26,20,8,16,7,22,4,11,5,17,9,12,23,18,2,25,6,24,13]               

#中轮子

Mid_L = [26,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
Mid_R = [
20,1,6,4,15,3,14,12,23,5,16,2,22,19,11,18,25,24,13,7,10,8,21,9,26,17]               

#快轮子
Fast_L = [26,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
Fast_R =[
14,8,18,26,17,20,22,10,3,13,11,4,23,5,24,9,12,25,16,19,6,15,21,2,7,1

2 主要函数流程

(1)转动函数:用于实现对快、中、慢三个轮子的转动,即实现表示轮子的列表的内部元素的交换。

def rotate_wheel(wheels):      #轮子的转动
        temp=wheels[25]
        wheels[
1:26]=wheels[0:25]
        wheels[
0]=temp

(2)查找函数:分为字母查找函数和数字查找函数,字母查找函数即使对于输入的字符串的每一个字母在Alphabet列表中找到对应字母的下标;数字查找函数则是通过数字找到所在轮子的右轮子的对应数字的下标。

def search_element(wheels,element):
        ele =element
        length =
len(wheels)
       
for i in range(length):
               
if wheels[i] == ele:
                      
return i
               
elif wheels[i] != ele :
                       
continue
                else
:
                       
print('查询失败')
                        return -1

def search_number(wheels,number):
        num =number
        length =
len(wheels)
       
for i in range(length):
                        if wheels[i] == num:
                              
return i
                       
elif wheels[i] != num:
                               
continue
                        else
:

                                print('查询失败')
                               
return -1

四、源程序代码

#慢轮子
Slow_L = [24,25,26,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
Slow_R = [21,3,15,1,19,10,14,26,20,8,16,7,22,4,11,5,17,9,12,23,18,2,25,6,24,13]
#中轮子
Mid_L = [26,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
Mid_R = [20,1,6,4,15,3,14,12,23,5,16,2,22,19,11,18,25,24,13,7,10,8,21,9,26,17]
# 快轮子
Fast_L = [26,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
Fast_R = [14,8,18,26,17,20,22,10,3,13,11,4,23,5,24,9,12,25,16,19,6,15,21,2,7,1]                                    
#将带转化明文转化为数字的参照表
Alphabet = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' ]
def rotate_wheel(wheels):      #轮子的转动
        temp=wheels[25]
        wheels[1:26]=wheels[0:25]
        wheels[0]=temp
def search_element(wheels,element):
        ele =element
        length = len(wheels)
        for i in range(length):
                if wheels[i] == ele:
                       return i
                elif wheels[i] != ele :
                        continue
                else:
                        print('查询失败')
                        return -1
def search_number(wheels,number):
        num =number
        length = len(wheels)
        for i in range(length):
                if wheels[i] == num:
                       return i
                elif wheels[i] != num:
                        continue
                else:
                        print('查询失败')
                        return -1
def encrypt_function(message):
        length=len(message)
        print('加密后的结果为:')
        m=0
        for i in range(length):
                if message[i]==' 'and message[i+1]==' ':
                        m=1
                if message[i]==' ':
                        print('',end='')
                else:
                        index1=search_element(Alphabet,message[i])
                        index2=search_number(Slow_R,Slow_L[index1])
                        index3=search_number(Mid_R,Mid_L[index2])
                        index4=search_number(Fast_R, Fast_L[index3])
                        if m == 1 :
                                print('')
                                print(Alphabet[index4], end='')
                                m = 0
                        else:
                                print(Alphabet[index4], end='')
                        count1=0
                        count1+=1
                        count2=0
                        rotate_wheel(Fast_L)
                        rotate_wheel(Fast_R)
                        if count1%26==0 and count1!=0:
                                rotate_wheel(Mid_L)
                                rotate_wheel(Mid_R)
                                count2+=1
                        if count2%26==0 and count2!=0:
                                rotate_wheel(Slow_L)
                                rotate_wheel(Slow_R)

def decode_function(ciphertext):
        length = len(ciphertext)
        print('解密后的结果为:')
        c=0
        for i in range(length):
                if ciphertext[i]==' 'and ciphertext[i+1]==' ':
                        c=1
                if ciphertext[i] == ' ':
                        print('',end='')
                else:
                        index1=search_element(Alphabet, ciphertext[i])
                        index2=search_number(Fast_L, Fast_R[index1])
                        index3=search_number(Mid_L, Mid_R[index2])
                        index4=search_number(Slow_L, Slow_R[index3])
                        if c==1 or i==0:
                                print('')
                                print(Alphabet[index4], end='')
                                c=0
                        else:
                                print(Alphabet[index4].lower(), end='')
                        count1 =0
                        count1+= 1
                        count2 =0
                        rotate_wheel(Fast_L)
                        rotate_wheel(Fast_R)
                        if count1 % 26 == 0 and count1 != 0:
                                rotate_wheel(Mid_L)
                                rotate_wheel(Mid_R)
                                count2 += 1
                        if count2 % 26 == 0 and count2 != 0:
                                rotate_wheel(Slow_L)
                                rotate_wheel(Slow_R)

while True:
        a=input('''请输入你要进行的操作:
加密输入0,解密输入1\n''')
        a=int(a)
        match a:
          case 0:
                Message=input("请输入想要加密的明文:\n" )
                Message=Message.upper()
                encrypt_function(Message)
                break
          case 1:
                Ciphertext = input("请输入想要解密的密文:\n")
                Ciphertext=Ciphertext.upper()
                decode_function(Ciphertext)
                break
          case _:
                print("请重新输入!")
                continue

五、测试数据及其结果分析

明文:

Cryptology science concerned with data communication and storage in secure and usually secret form 

It encompasses both cryptography and cryptanalysis 

Cryptography was originally the study of the principles and techniques by which information could be concealed in ciphers and later revealed by legitimate users employing the secret key 

It now encompasses the whole area of key controlled transformations of information into forms that are either impossible or computationally infeasible for unauthorized persons to duplicate or undo 

Cryptanalysis from the Greek kryptos and analyein to loose or to untie is the science and art of recovering or forging cryptographically secured information without knowledge of the key 

Cryptology is often and mistakenly considered a synonym for cryptography and occasionally for cryptanalysis but specialists in the field have for years adopted the convention that cryptology is the more inclusive term encompassing both cryptography and cryptanalysis 

Cryptography was initially only concerned with providing secrecy for written messages especially in times of war  

Its principles apply equally well however to securing data flowing between computers or data stored in them to encrypting facsimile and television signals to verifying the identity of participants in electronic commerce e commerce and providing legally acceptable records of those transactions 

Because of this broadened interpretation of cryptography the field of cryptanalysis has also been enlarged 

This article discusses the basic elements of cryptology delineating the principal systems and techniques of cryptography as well as the general types and procedures of cryptanalysis 

It also provides a concise historical survey of the development of cryptosystems and cryptodevices 

A brief introduction is also given to the revolution in cryptology brought on by the information age e commerce and the Internet 

For additional information on the encoding and encryption of facsimile and television signals and of computer data see telecommunications system and information processing

密文:

TIPMYMZIPXRMSCQAKOECLURMMTWZDCDVROSDBZMGTAPKKZKBDYEJVIXYGNJQIQCOTYQHXZTHHGGJMEKXDROZO

GFSQMUTLMBZDSWAWVUPYWDTEZQWOXIHFHARTKJCGGGJFV

TIPMYMWSXHPRBYISMVVOKBEUNJLXEBKIFIBIPVXBTTYVGNRMSMWJGDZLPRLCYRWSOJBKSMHZYVSGFRUDLPAIACSDQMTYYNLVSWURXOHVEOKJNSYSQSDVZDPHWJQBIEWHIBHXQHGZGCHHFWIJQXVADSRJROONGV

WBBMBCWQXZXYICKZUDRYXZXLEIHDOLKCZQXHABNWFEBXYBDJGRKCXBPMJQGLYHHKAUPKKZKEDGCWVIAHYWQPXGAVSZGNMVWFQMKHYCGBRVOQNAFKNXUGWWFJKCSUSJFIRXTTCBJOKIPWDJFNLIDVXADJORNSCJPFQPIDJ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OLPUXLDFTKJIXQTFTAKZ

JMYEZIFXHILAKFRGGVYAZDISNVCBKQPGSLKXBTABRGGVYHKBHOVFQZILOXCYCJDTBDFPOSYHHKAUPKKZKBCOMEVCAWCFYOWPNETJQLKWSBM

PPXRTNZDQOEQJLVOWDTWLSFSZKQXOMXTBKQBOQYUSQMDSLLSFSZSPSDEOPZOWNEXPIBANSLTSIZKKKBEJRXNBTVOQNAFSJQZZWCKEUBHUIZIGDOWVNSDIWTRRNZPUPMBOKPPXIBHZLLUZVXITRXRA

六、调试过程中的问题

(1)字符串中存在空格,直接调用查找函数会报错,用if语句进行选择,如果为空格直接跳过,继续加密下一个字符,同时也可以保证解密时得到正确结果。

(2)字母和数字的查找函数需要定义两个,同时调用一个时,因数据类型不同会报错,但函数体基本相同。

七、课程总结

       本次实验主要是转轮机的模拟实现,通过编写程序,实现转轮加密算法也让我更加清楚的了解了转轮机的工作原理,对于转轮机如何加密和解密操作也更加熟悉,当然编写的程序与实际情况仍然存在一些差别,有些地方可能还不够完善,但可以基本实现转轮机的功能。

  • 18
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鲤鱼Louis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值