一、实验项目名称(实验题目):
换位加密法
二、实验目的与要求:
掌握换位加密法的原理和流程,熟悉布尔运算。
三、实验内容:
1、练习。
With paper and pencil, encrypt the following messages with the key 9 using the transposition cipher. The (s) parts mark a single space. The number of characters has been provided for your convenience.
1.Underneath(s)a(s)huge(s)oak(s)tree(s)there(s)was(s)of(s)swine(s)a(s)huge(s)company, (61 characters)
2.That(s)grunted(s)as(s)they(s)crunched(s)the(s)mast:(s)(s)For(s)that(s)was(s)ripe,(s)and(s)fell(s)full(s)fast. (79 characters)
3.Then(s)they(s)trotted(s)away,(s)for(s)the(s)wind(s)grew(s)high:(s)(s)One(s)acorn(s)they(s)left,(s)and(s)no(s)more(s)might(s)you(s)spy. (96 characters)
Answers:
The (s) parts mark a single space:
1.Uhot(s)(s)on(s)ahoamdakef(s)pe(s)(s)r(s)harhtesunnur(s)wgyegewie,aeean(s)t(s)(s)sec
2.Tteestr(s)lheydthif(s)ad(s)(s):apeft(s)ct(s)tela(s)arh(s)(s),lsgsueFw(s)(s)tr(s)n(s)oaaf.utcmrsnunhha(s)(s)dl
3.T(s)a(s)(s)hc(s)d(s)uhtwtg:ol(s)m(s)erahr(s)renisnoyee(s)nfogp(s)t,(s)wO(s)t(s)hytt(s)w(s)nt,mt.hefiheh(s)o(s)edoni(s)earyy(s)rdgayneo
2、运行换位加密法加密程序。
# Transposition Cipher Encryption
# http://inventwithpython.com/hacking (BSD Licensed)
import pyperclip
def main():
myMessage = 'Common sense is not so common.'
myKey = 8
ciphertext = encryptMessage(myKey, myMessage)
# Print the encrypted string in ciphertext to the screen, with
# a | (called "pipe" character) after it in case there are spaces at
# the end of the encrypted message.
print(ciphertext + '|')
# Copy the encrypted string in ciphertext to the clipboard.
pyperclip.copy(ciphertext)
def encryptMessage(key, message):
# Each string in ciphertext represents a column in the grid.
ciphertext = [''] * key
# Loop through each column in ciphertext.
for col in range(key):
pointer = col
# Keep looping until pointer goes past the length of the message.
while pointer < len(message):
# Place the character at pointer in message at the end of the
# current column in the ciphertext list.
ciphertext[col] += message[pointer]
# move pointer over
pointer += key
# Convert the ciphertext list into a single string value and return it.
return ''.join(ciphertext)
# If transpositionEncrypt.py is run (instead of imported as a module) call
# the main() function.
if __name__ == '__main__':
main()
3、def语句。
4、形参、对形参的修改。
5、global 语句 。
6、练习
In the following programs, is spam a global or local variable? Or are there both a global and local variables named spam?
1.spam = 42
2.def foo():
3. spam = 42
4.spam = 42
5.def foo():
4. spam = 42
7.spam = 42
8.def foo():
5. print(spam)
10.spam = 42
11.def foo():
6. global spam
7. print(spam)
14.spam = 42
15.def foo():
8. global spam
9. spam = 99
10. print(spam)
19.spam = 42
20.def foo():
11. spam = 99
12. print(spam)
Answers:
1.Global
2.Local
3.Two variables, one global and one local.
4.Global
5.Global
6.Global
7.Two variables, one global and one local.
7、列表数据类型 。
8、list()函数。
9、重新赋值。
10、列表的列表。
11、练习
What value do each of the following expressions evaluate to?
1.[0, 1, 2, 3, 4][2]
2.[1, 2, 3, 4][2]
3.[[1, 2], [3, 4]][0]
4.[[1, 2], [3, 4]][1]
5.[[1, 2], [3, 4]][0][1]
6.[[1, 2], [3, 4]][1][1]
7.[‘hello’][0]
8.[‘hello’][0][1]
9.[2, 4, 6, 8, 10][1:3]
10.list(‘Hello world!’)
11.list(range(10))
12.list(range(10))[2]
12、len() in(),+,*运算符。
13、练习。
What value do each of the following expressions evaluate to?
(1)len([2, 4])
(2)len([])
(3)len([’’, ‘’, ‘’])
(4)[4, 5, 6] + [1, 2, 3]
(5)3 * [1, 2, 3] + [9]
(6)42 in [1, 2, 3]
(7)42 in [41, 42, 42, 42]
14、增强赋值运算符。
15、join()字符串。
16、练习。
What do the following pieces of code display on the screen?
(2)spam = ‘hello’
(3)spam += ‘world’
(4)print(spam)
(5)spam = 3
(6)spam += 3
(7)spam += 3
(8)print(spam)
(9)’’.join([‘hello’, ‘world’])
(10)’ '.join([‘my’, ‘very’, ‘special’, ‘mudder’])
17、练习 。
With paper and pencil, decrypt the following messages with the key 9. The (s) parts mark a single space. The total number of characters is already counted for you.
(1)H(s)cb(s)(s)irhdeuousBdi(s)(s)(s)prrtyevdgp(s)nir(s)(s)eerit(s)eatoreechadihf(s)paken(s)ge(s)b(s)te(s)dih(s)aoa.da(s)tts(s)tn (89 characters)
(2)A(s)b(s)(s)drottthawa(s)nwar(s)eci(s)t(s)nlel(s)ktShw(s)leec,hheat(s).na(s)(s)e(s)soogmah(s)a(s)(s)ateniAcgakh(s)dmnor(s)(s) (86 characters)
(3)Bmmsrl(s)dpnaua!toeboo’ktn(s)uknrwos.(s)yaregonr(s)w(s)nd,tu(s)(s)oiady(s)hgtRwt(s)(s)(s)A(s)hhanhhasthtev(s)(s)e(s)t(s)e(s)(s)eo (93 characters)
1.①89/9=9.8888上取整10 10列9行的网格
②最右边一列需要90-98=1个格子涂灰
He picked up the acorn and buried it straight By the side of a river both deep and great.
2.①86/9=9.5555上取整10 10列9行的网格
②最右边一列需要90-86=4个格子涂灰
At length he came back, and with him a She And the acorn was grown to a tall oak tree.
3.①93/9=10.3333上取整为11 11列9行的网格
②最右边一列需要99-93=6个格子涂灰
But with many a hem! And a sturdy stroke, At length he brought down the poor Raven’s own oak
18、先加密后解密,明文、密钥自选。
19、math.ceil()、math.floor()和 round()函数。
20、and ,or布尔运算。
21、练习。
1.>>> math.ceil(3.1)
13. >>> math.ceil(3.0)
14. >>> math.floor(3.1)
15. >>> math.floor(3.0)
16. >>> round(3.1)
17. >>> round(3.0)
18. >>> round(3.5)
19. >>> True and True
20. >>> True and False
21. >>> False and True
22. >>> False and False
23. >>> True or True
24. >>> True or False
25. >>> False or True
26. >>> False or False
27. >>> not True
28. >>> not not True
22、练习。
- Draw out the complete truth tables for the and, or, and not operators.
- Which is correct?
o if name == ‘main’:
o if main == ‘name’:
o if name == ‘main’:
o if main == ‘name’:
(1)
(2)
(3)
(4)if name == ‘main’:



