一、实验项目名称(实验题目):
凯撒加密法
二、实验目的与要求:
掌握凯撒加密法的原理和步骤,掌握for循环的使用。
三、实验内容:
1、运行凯撒加密法程序。

# Caesar Cipher
# http://inventwithpython.com/hacking (BSD Licensed)import pyperclip# the string to be encrypted/decrypted
message = 'This is my secret message.'# the encryption/decryption key
key = 13# tells the program to encrypt or decrypt
mode = 'encrypt' # set to 'encrypt' or 'decrypt'# every possible symbol that can be encrypted
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'# stores the encrypted/decrypted form of the message
translated = ''# capitalize the string in message
message = message.upper()# run the encryption/decryption code on each symbol in the message string
for symbol in message:if symbol in LETTERS:# get the encrypted (or decrypted) number for this symbolnum = LETTERS.find(symbol) # get the number of the symbolif mode == 'encrypt':num = num + keyelif mode == 'decrypt':num = num - key# handle the wrap-around if num is larger than the length of# LETTERS or less than 0if num >= len(LETTERS):num = num - len(LETTERS)elif num < 0:num = num + len(LETTERS)# add encrypted/decrypted number's symbol at the end of translatedtranslated = translated + LETTERS[num]else:# just add the symbol without encrypting/decryptingtranslated = translated + symbol# print the encrypted/decrypted string to the screen
print(translated)# copy the encrypted/decrypted string to the clipboard
pyperclip.copy(translated)

2、Using the cipher program, encrypt the following sentences with the given keys:
1.“‘You can show black is white by argument,’ said Filby, ‘but you will never convince me.’” with the key 8.
2.“1234567890” with key 21.
Using the cipher program, decrypt the following ciphertexts with the given keys:
3.“‘Kv uqwpfu rncwukdng gpqwij.’” with key 2.
4.“Xqp whh ahoa kb pda sknhz swo ejreoexha.” with key 22.
Using the cipher program, encrypt the following sentence with the key 0:
5.“This is still a silly example.”
Answers:
1.‘GWC KIV APWE JTIKS QA EPQBM JG IZOCUMVB,’ AIQL NQTJG, ‘JCB GWC EQTT VMDMZ KWVDQVKM UM.’
2.1234567890
3.‘IT SOUNDS PLAUSIBLE ENOUGH.’
4.BUT ALL ELSE OF THE WORLD WAS INVISIBLE.
5.THIS IS STILL A SILLY EXAMPLE.



3、练习upper()和lower()字符串方法 。

4、for循环语句、while循环。
5、练习
1.What Python instruction would import a module named watermelon.py?
2.The variable SPAM is a constant. Will this code cause an error?: SPAM = ‘hello’
What do the following pieces of code display on the screen?
3.for i in ‘hello’:
4. print(i)
5.print(‘Hello’.lower())
6.print(‘Hello’.upper())
7.print(‘Hello’.upper().lower().upper().lower())
BONUS: What does this program display on the screen? (Guess, and then type it in and run it to find out.)
spam = ‘foo’
for i in spam:
spam += i
print(spam)

Answers:
1.import watermelon (Note that there is no .py)
2.No, it will not cause an error. Constants are just regular variables and can have their values changed. But it is a generally accepted convention to not change the values in constants.
3.h
4.e
5.l
6.l
7.o
8.hello
9.HELLO
10.hello
BONUS: foofoo Even though the spam variable is having strings added to it, only the value of spam when the for loop first started is iterated over.

(1)What Python instruction would import a module named watermelon.py?

(2).The variable SPAM is a constant. Will this code cause an error?: SPAM = ‘hello’

(3).What do the following pieces of code display on the screen?

6、if语句、else 语句、elif 语句。




7、in和not in运算符。

8、find()字符串方法。

9、What do the following pieces of code display on the screen?

What do the following pieces of code display on the screen?
#1
if 10 < 5:print('Hello')
#2
if 10 < 5:print('Hello')
else:print('Goodbye')
#3
if 10 < 5:print('Hello')
elif 5 == 5:print('Alice')
else:print('Goodbye')
#4
if 10 < 5:print('Hello')
elif False:print('Alice')
else:print('Goodbye')
#5
if 10 < 5:print('Hello')
elif False:print('Alice')
elif 5 != 5:print('Bob')
else:print('Goodbye')
#6
if 10 < 5:print('Hello')
elif 5 == 5:print('Alice')
else:print('Goodbye')
#7
if 10 < 5:print('Hello')
else:print('Goodbye')
elif 5 == 5:print('Alice')
#8
print('f' in 'foo')
#9
print('f' not in 'foo')
#10
print('foo' in 'f')
#11
print('hello'.find('o'))
#12
print('hello'.find('oo'))

Answers:
1.Nothing.
2.Goodbye
3.Alice
4.Goodbye
5.Goodbye
6.Alice
7.This program crashes with an error because the else statement always comes at the end.
8.True
9.False
10.False
11.4
12.-1 (The integer -1 is returned if the string cannot be found.)


10、运行凯撒加密法破译程序。

# Caesar Cipher Hacker
# http://inventwithpython.com/hacking (BSD Licensed)message = 'GUVF VF ZL FRPERG ZRFFNTR.'
LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'# loop through every possible key
for key in range(len(LETTERS)):# It is important to set translated to the blank string so that the# previous iteration's value for translated is cleared.translated = ''# The rest of the program is the same as the original Caesar program:# run the encryption/decryption code on each symbol in the messagefor symbol in message:if symbol in LETTERS:num = LETTERS.find(symbol) # get the number of the symbolnum = num - key# handle the wrap-around if num is 26 or larger or less than 0if num < 0:num = num + len(LETTERS)# add number's symbol at the end of translatedtranslated = translated + LETTERS[num]else:# just add the symbol without encrypting/decryptingtranslated = translated + symbol# display the current key being tested, along with its decryptionprint('Key #%s: %s' % (key, translated))


11、range()函数 。
12、字符串格式化。
13、练习
Break the following ciphertexts:
1.R UXEN VH TRCCH,
2.FR DBMMR EHOXL FX,
3.CXPNCQNA FN’AN BX QJYYH,
4.OBR OZKOMG QOFSTFSS.
5.PDKQCD IU DAWZ DWO OQOLEYEKJO,
6.FTMF U WQQB GZPQD YK TMF,
7.AR ITMF YUSTF TMBBQZ,
8.DA D NCMVIF OJ OCZ NDUZ JA V MVO.
9.ZFBI. J’N QSFUUZ TVSF NZ DBU XPVME FBU NF.
Answers:
1.I LOVE MY KITTY,
2.MY KITTY LOVES ME,
3.TOGETHER WE’RE SO HAPPY,
4.AND ALWAYS CAREFREE.
5.THOUGH MY HEAD HAS SUSPICIONS,
6.THAT I KEEP UNDER MY HAT,
7.OF WHAT MIGHT HAPPEN,
8.IF I SHRANK TO THE SIZE OF A RAT.
YEAH. I’M PRETTY SURE MY CAT WOULD EAT ME.








【Python|密码学】凯撒加密法实验报告相关推荐

  1. python密码学凯撒密码_凯撒密码在Python

    python密码学凯撒密码 Hello everyone, in this tutorial you'll learn about Caesar cipher in Python. If you ha ...

  2. python凯撒加密实验报告_Python之凯撒加密

    凯撒加密介绍 在密码学中,恺撒密码是一种最简单且最广为人知的加密技术. 它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文. 例,当偏移量是3的时 ...

  3. 小亮学加密解密-----凯撒加密法

    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 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 ...

  4. Python 实现凯撒加解密

    凯撒加密法指的是两千年前由凯撒大帝使用的加密法,是一种最简单且最广为人知的加密技术.它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文.历史上,通 ...

  5. python 实现凯撒加密

    凯撒密码(Caesar)基本原理 字母表一共有26个英文字母,加密时,我们需要将某个明文字母做N位偏移得到密文,这个N最多为26,而且偏移为26时和偏移为0时一样,明文和密文对应相等,实际上可以说最大 ...

  6. python实现凯撒密码、凯撒加解密算法

    python实现凯撒密码.凯撒加解密算法 更多python视频教程请到菜鸟教程https://www.piaodoo.com/ 凯撒密码的原理:计算并输出偏移量为3的凯撒密码的结果 注意:密文是大写字 ...

  7. 密码学实验1 凯撒密码实验

    文章目录 一. 实验目的 二.实验内容 需要word文件请访问 http://daxs.top 站内搜索实验名称或者实验内容访问文章并且下载附件即可. 一. 实验目的 通过实验熟练掌握凯撒密码算法,学 ...

  8. Python之凯撒加密

    凯撒加密介绍 在密码学中,恺撒密码是一种最简单且最广为人知的加密技术. 它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文. 例,当偏移量是3的时 ...

  9. python中凯撒密码_python实现凯撒密码、凯撒加解密算法

    凯撒密码的原理:计算并输出偏移量为3的凯撒密码的结果 注意:密文是大写字母,在变换加密之前把明文字母都替换为大写字母 def casar(message): # *************begin* ...

最新文章

  1. html手机不能自动播放音乐,解决移动端浏览器 HTML 音频不能自动播放的三种方法...
  2. 清华大学第四届大数据开放日(Big Data Day)
  3. FPGA 控制 FLASH 之 Startup 原语使用相关链接
  4. matlab读取表格读成mat文件,MATLAB 视频读取 Excel读写 Excel2txt txt2mat 按文件夹读取文件...
  5. 软件架构设计_软件架构设计的三个维度,软件架构师需要知道的点,了解一下吧...
  6. 【课堂教学/课堂复习/课堂竞赛手段探析】给广大教师推荐一个期末课堂复习的最好办法
  7. Python 2.7.5 CentOS 6.4 编译 错误
  8. Arcgis中的空间插值
  9. android 支付宝快捷支付
  10. Your actions speak louder
  11. LeetCode——1900. 最佳运动员的比拼回合(The Earliest and Latest Rounds Where Players Compete)[困难]——分析及代码(Java)
  12. 功能模块图、业务流程图、处理流程图、ER图,数据库表图(概念模型和物理模型)画法...
  13. python绘制不同风格的分级统计图(Choropleth map)
  14. mapbox tippecanoe切矢量瓦片参数设置和注意事项
  15. 根据前序遍历和中序遍历创建二叉树
  16. [转载]历上最强的音乐播放器(jetAudio-8.0.5.320-Plus-VX
  17. 如何获取文件的后缀名?
  18. 普通计算机怎么改闹铃的音乐,怎么设置闹钟铃声为自己喜欢的音乐
  19. 贵金属解套11.19解锁黄金期货原油实时操作建议指导
  20. C++ 文件读写实战——2进制文件查看器(16进制显示)

热门文章

  1. GIS教程之在 R 中使用 Leaflet 的交互式地图
  2. 通俗案例剖析市场营销
  3. 三层架构(UI、BLL、DAL)
  4. vue高德多条轨迹导航+带途径节点标注+各路线颜色区别
  5. tomcat 没有service.bat、tomcat8.exe、tomcat8w.exe、tomcatX.exe文件,官网下载方法及地址
  6. DeleteFile()函数的正确使用(呼,解决了个小麻烦)
  7. 人脸识别微笑检测(基于卷积神经网络CNN)
  8. 业务中台 全渠道一盘货 基于微服务的订单管理系统OMS 开源
  9. 手机QQ2008聊天记录导入手机QQ2009 塞班第三版Python全代码 修改
  10. 对于vector中高效删除中间元素的技巧