【Python|密码学】凯撒加密法实验报告

一、实验项目名称(实验题目):
凯撒加密法
二、实验目的与要求:
掌握凯撒加密法的原理和步骤,掌握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 symbol
        num = LETTERS.find(symbol) # get the number of the symbol
        if mode == 'encrypt':
            num = num + key
        elif mode == 'decrypt':
            num = num - key

        # handle the wrap-around if num is larger than the length of
        # LETTERS or less than 0
        if num >= len(LETTERS):
            num = num - len(LETTERS)
        elif num < 0:
            num = num + len(LETTERS)

        # add encrypted/decrypted number's symbol at the end of translated
        translated = translated + LETTERS[num]

    else:
        # just add the symbol without encrypting/decrypting
        translated = 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()字符串方法 。
【Python|密码学】凯撒加密法实验报告_第1张图片
4、for循环语句、while循环。【Python|密码学】凯撒加密法实验报告_第2张图片
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’
【Python|密码学】凯撒加密法实验报告_第3张图片
(3).What do the following pieces of code display on the screen?
【Python|密码学】凯撒加密法实验报告_第4张图片
6、if语句、else 语句、elif 语句。
【Python|密码学】凯撒加密法实验报告_第5张图片
【Python|密码学】凯撒加密法实验报告_第6张图片
【Python|密码学】凯撒加密法实验报告_第7张图片
【Python|密码学】凯撒加密法实验报告_第8张图片
7、in和not in运算符。
【Python|密码学】凯撒加密法实验报告_第9张图片
8、find()字符串方法。
【Python|密码学】凯撒加密法实验报告_第10张图片
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.)【Python|密码学】凯撒加密法实验报告_第11张图片
【Python|密码学】凯撒加密法实验报告_第12张图片
【Python|密码学】凯撒加密法实验报告_第13张图片
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 message
    for symbol in message:
        if symbol in LETTERS:
            num = LETTERS.find(symbol) # get the number of the symbol
            num = num - key

            # handle the wrap-around if num is 26 or larger or less than 0
            if num < 0:
                num = num + len(LETTERS)

            # add number's symbol at the end of translated
            translated = translated + LETTERS[num]

        else:
            # just add the symbol without encrypting/decrypting
            translated = translated + symbol

    # display the current key being tested, along with its decryption
    print('Key #%s: %s' % (key, translated))

【Python|密码学】凯撒加密法实验报告_第14张图片
11、range()函数 。【Python|密码学】凯撒加密法实验报告_第15张图片
12、字符串格式化。【Python|密码学】凯撒加密法实验报告_第16张图片
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|密码学】凯撒加密法实验报告_第17张图片
【Python|密码学】凯撒加密法实验报告_第18张图片
【Python|密码学】凯撒加密法实验报告_第19张图片
【Python|密码学】凯撒加密法实验报告_第20张图片
【Python|密码学】凯撒加密法实验报告_第21张图片
【Python|密码学】凯撒加密法实验报告_第22张图片
【Python|密码学】凯撒加密法实验报告_第23张图片
【Python|密码学】凯撒加密法实验报告_第24张图片
【Python|密码学】凯撒加密法实验报告_第25张图片

你可能感兴趣的:(Python,python)