密码学实验——凯撒密码

一、实验目的

1.理解移位密码学加解密过程,并编程实现

2.理解暴力破解攻击思想,并编程实现

二、实验内容与设计思想

1.Caesar密码是传统的代替加密法,当没发生加密(即没有移位)前,其置换表如下图:

a

b

c

d

e

f

g

h

i

j

k

l

m

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

N

O

P

Q

R

S

T

U

V

W

X

Y

Z

加密时每一个字母向前推移k位,例如当k=5时,置换表如下图:

a

b

c

d

e

f

g

h

i

j

k

l

m

F

G

H

I

J

K

L

M

N

O

P

Q

R

n

o

p

q

r

s

t

u

v

w

x

y

z

S

T

U

V

W

X

Y

Z

A

B

C

D

E

于是对于明文:data security has evolved rapidly

经过加密后就可以得到密文:IFYF XJHZWNYD MFX JATQAJI WFUNIQD
    若令26个字母分别对应整数 0 ~ 25,如表所示。

a

b

c

d

e

f

g

h

i

j

k

l

m

0

1

2

3

4

5

6

7

8

9

10

11

12

n

o

p

q

r

s

t

u

v

w

x

y

z

13

14

15

16

17

18

19

20

21

22

23

24

25

则Caesar加密变换实际上是:

    c = (m + k) mod 26

其中m是明文对应的数据,c是与明文对应的密文数据,k是加密用的参数,也称为密钥。

    很容易得到相应的Caesar解密变换是:

m = D(c) = (c – k) mod 26

例如明文:data security 对应的数据序列: 3  0  19  0  18  4  2  20  17  8  19  24

k = 5时经过加密变换得到密文序列:    8  5  24  5  23  9  7  25  22  13  24  3

对应的密文为:

I  F  Y  F  X  J  H  Z  W  N  Y  D

三、实验步骤和调试过程(建议画出程序流程图)

1.手动完成Caesar密码

当密钥k=时,对应明文:data security has evolved rapidly的密文:                     

mjcj bnldarch qjb nexuenm ajyrmuh

2.Caesar加密

编码实现“Caesar密码”。在明文输入区输入明文:data security has evolved rapidly。将密钥k调节到 9,查看相应的密文,并与你手动加密的密文进行比较。

源代码

#1 -* - coding: utf-8 -* -

letter _list='ABCDEFGHIJKLMNOPQRSTUVWXYZ' :

#加密函数

def Encrypt(plaintext, key) : ciphertext=':

for ch in plaintext:

#遍历明文

    if ch.isalpha() :

#明文是否为字母,如果是,判断大小写,分别进行加密

        if ch. isupper ():

            ciphertext+=letter_listl (ord (ch) -65+key) & 26]

        else:

            ciphertext+=letter_listl (ord(ch) -97+key) & 26]. lower ()

    else:

#如果不为字母,直接添加到密文字符里

        ciphertext+=ch return ciphertext

#解密函数

def Decrypt (ciphertext, key) :

plaintext= for ch in ciphertext:

    if ch.isalpha():

        if ch.isupper():

            plaintext+=letter_list| (ord(ch) -65-key) & 26]

        else:

            plaintext+=letter_list[ (ord(ch) -97-key) & 26]. lower ()I

    else:

        plaintext+=ch return plaintext user_input=input('加密请按1,解密请按2:”);

while (user _input!='1' and user_input!='2'):

    user_input=input('输入有误,请重新输入:)

    key=input('请输入密钥:)

while (int (key isdigit ()==0)):

    key=input('输入有误,密钥为数字,请重新输入:)

    if user input =='1':

        plaintext=input('请输入明文:')

        ciphertext-Encrypt (plaintext, int (key) )

        print ('**7 : ins ciphertext )

    else: ciphertext=input('请输入密文:)

        plaintext-Decrypt (ciphertext, int (key) )

        print('明文为:ns\n'% plaintext )
  • 结果截图
  • 密文:  mjcj bnldarch qjb nexuenm ajyrmuh                             

3. 破解下面密文,由于每一行使用了不同的密钥,因此,请一次只对一行进行解密:qeFIP?eGSeECNNS,
5coOMXXcoPSZIWoQI,
avnl1olyD4l'ylDohww6DhzDjhuDil,
z.GM?.cEQc. 70c.7KcKMKHA9AGFK,
?MFYp2pPJJUpZSIJWpRdpMFY,
ZqH8sl5HtqHTH4s3lyvH5zH5spH4t pHzqHlH3l5K
Zfbi,!tif!xpvme!qspcbcmz!fbu!nfA    此时,明文空间为'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?.'

  • 源代码
# -*- coding: utf-8 -*-

# Extended character set for plaintext

charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?.' charset_len = len(charset)

# Encryption function

def Encrypt(plaintext, key):

ciphertext = '' for ch in plaintext:

 if ch in charset:

idx = charset.index(ch)

encrypted_idx = (idx + key) % charset_len

ciphertext += charset[encrypted_idx]

else: ciphertext += ch

return ciphertext

 # Decryption function

def Decrypt(ciphertext, key):

plaintext = '' for ch in ciphertext:

 if ch in charset: idx = charset.index(ch)

decrypted_idx = (idx - key) % charset_len

plaintext += charset[decrypted_idx]

 else: plaintext += ch return plaintext

# Automatically try all possible keys and print meaningful plaintext

def crack_ciphertext(ciphertext):

print(f"Cracking ciphertext: '{ciphertext}'")

print("Possible plaintexts:")

for key in range(charset_len):

plaintext = Decrypt(ciphertext, key)

 print(f"Key {key}: {plaintext}")

# List of ciphertexts to be cracked

ciphertexts = [ "qeFIP?eGSeECNNS,", "5coOMXXcoPSZIWoQI,", "avnl1olyD4l'ylDohww6DhzDjhuDil,", "z.GM?.cEQc. 70c.7KcKMKHA9AGFK,", "?MFYp2pPJJUpZSIJWpRdpMFY,", "ZqH8sl5HtqHTH4s3lyvH5zH5spH4t pHzqHlH3l5K", "Zfbi,!tif!xpvme!qspcbcmz!fbu!nfA" ]

# Crack each line of ciphertext

for i, ciphertext in enumerate(ciphertexts):

print(f"\nCracking ciphertext {i+1}:") crack_ciphertext(ciphertext)

print("-" * 50)
  • 结果截图

  • 密文:              I love my Kitty,My kitty loves me,Together wtre happy as can be, Though my head has suspicions,That I keep under my hat,Of what if I shrank to the size of a rat,Year,she would probably eat me.               

实验小(30字以上)


本次实验围绕凯撒密码展开,深入学习其加解密原理与暴力破解方法。通过文档可知,凯撒密码作为单表替代密码,核心是将明文字母按固定密钥 \(k\) 移位,加密公式为 \(c=(m+k)\mod26\),解密则为 \(m=(c-k)\mod26\)。其密钥空间仅26种可能,安全性极低,易被暴力破解。 在实践环节,编写并运行加解密代码,输入明文“data security has evolved rapidly”与密钥9(学号末位),成功得到密文“mjcj bnldarch qjb nexuenm ajyrmuh”,与手动加密结果一致,验证了算法的正确性。暴力破解部分,通过遍历所有可能密钥(0-65,包含扩展字符集),从密文中还原出有意义的明文,如密钥34对应“I love my kitty”,密钥7对应“Together we're happy as can be”,直观展示了凯撒密码的脆弱性。 安全性方面,凯撒密码因密钥空间极小,即便扩展字符集后(密钥范围0-65),仍可通过穷举快速破解。实验中还发现,代码支持大小写字母及特殊字符,增强了实用性,但未改变其本质安全缺陷。 通过本次实验,不仅掌握了凯撒密码的编程实现,更深刻理解了古典密码的设计局限——密钥空间大小直接决定抗攻击能力。暴力破解的实践演示,为后续学习现代密码的复杂度理论提供了对比基础,也警示实际应用中需采用更安全的加密机制(如多表密码或现代加密算法),避免使用此类简单替代密码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值