Encrypt the following sentences with the given keys:
With key 4: "AMBIDEXTROUS: Able to pick with equal skill a right-hand pocket or a left."
ANSWER: EQFMHIbXVSYW:AEfpiAxsAtmgoA1mxlAiuyepAwomppAeAvmklx-lerhAtsgoixAsvAeApijxD
With key 17: "GUILLOTINE: A machine which makes a Frenchman shrug his shoulders with good reason."
ANSWER: bpdggjodiZ:RVR8vx349zRD34x3R8v6z.RvRa?z9x38v9R.3?B2R34.R.30B7yz?.RD4A3R200yR?zv.09U
With key 21: "IMPIETY: Your irreverence toward my deity."
ANSWER: dhkdZot:Rt0B?R4??zCz?z9xzRA0Dv?yR8FRyz4AFU
Decrypt the following ciphertexts with the given keys:
With key 15: "ZXAI: P RDHIJBT HDBTIXBTH LDGC QN HRDIRWBTC XC PBTGXRP PCS PBTGXRPCH XC HRDIAPCS."
ANSWER: KILT: A costume sometimes worn by Scotchmen in America and Americans in Scotland.
With key 4: "MQTSWXSV: E VMZEP EWTMVERX XS TYFPMG LSRSVW."
ANSWER: IMPOSTOR: A rival aspirant to public honors.
Encrypt the following sentence with the key 0:
"This is a silly example."
ANSWER: This is a silly example.
Here are some words and their encryptions. Which key was used for each word?
ROSEBUD – LIMYVOX
ANSWER: 20
YAMAMOTO – PRDRDFKF
ANSWER: 17
ASTRONOMY – HZAYVUVTF
ANSWER: 19
What does this sentence encrypted with key 8 decrypt to with key 9? "UMMSVMAA: Cvkwuuwv xibqmvkm qv xtivvqvo i zmdmvom bpib qa ewzbp epqtm."
ANSWER: LDDJMDRR: TMBNLLNM OZSHDMBD HM OKZMMHMF Z QDUDMFD SGZS HR VNQSG VGHKD. (Decrypting with the incorrect key results in nonsense text.)
Which is the operator for division, / or \ ?
ANSWER: / (The backslash \ is used for escape characters in strings, described in Chapter 3.)
Which of the following is an integer value, and which is a floating-point value?
42
3.141592
ANSWER: 42
is an integer value, 3.141592
is a floating-point value.
Which of the following lines are not expressions?
4 x 10 + 2
3 * 7 + 1
2 +
42
2 + 2
spam = 42
ANSWER: 4 x 10 + 2 (x is not the multiplication operator, * is), 2 + (this expression is incomplete), and spam = 42 (this is an assignment statement, not an expression)
If you enter the following lines of code into the interactive shell, what do lines (1) and (2) print?
spam = 20
spam + 20 # (1)
SPAM = 30
spam # (2)
ANSWER: Line 1 prints 40
, line 2 prints 20
. (spam
and SPAM
are two different variables.)
If you assign spam = 'Cats' , what do the following lines print?
spam + spam + spam
ANSWER: 'CatsCatsCats'
spam * 3
ANSWER: 'CatsCatsCats'
What do the following lines print?
print("Dear Alice,\nHow are you?\nSincerely,\nBob")
ANSWER: Dear Alice,
How are you?
Sincerely,
Bob
print('Hello' + 'Hello')
ANSWER: HelloHello
If you assign spam = 'Four score and seven years is eighty seven years.'
, what would each of the following lines print?
print(spam[5])
ANSWER: s
print(spam[-3])
ANSWER: r
print(spam[0:4] + spam[5])
ANSWER: Fours
print(spam[-3:-1])
ANSWER: rs
print(spam[:10])
ANSWER: Four score
print(spam[-5:])
ANSWER: ears.
print(spam[:])
ANSWER: Four score and seven years is eighty seven years.
Which window displays the >>> prompt, the interactive shell or the file editor?
ANSWER: The interactive shell.
What does the following line print?
#print('Hello, world!')
ANSWER: It prints nothing.
What does the following piece of code print to the screen?
print(len('Hello') + len('Hello'))
ANSWER: 10
(Both calls to len()
return integer values, and the 5
s add to the integer value 10
, which is printed on the screen.)
What does this code print?
i = 0 while i < 3: print('Hello') i = i + 1
ANSWER: Hello
Hello
Hello
How about this code?
i = 0 spam = 'Hello' while i < 5: spam = spam + spam[i] i = i + 1 print(spam)
ANSWER: HelloHello
And this?
i = 0 while i < 4: while i < 6: i = i + 2 print(i)
ANSWER: 2 4 6
Using caesarCipher.py, encrypt the following sentences with the given keys:
'"You can show black is white by argument," said Filby, "but you will never convince me."'
with key 8
ANSWER: "gw3EkivE1pw5EjtiksEq1E5pq2mEj7Eizo3umv2,"E1iqlENqtj7,E"j32E7w3E5qttEvm4mzEkwv4qvkmEumH"
'1234567890'
with key 21
ANSWER: HIJKLMNOPQ
Using caesarCipher.py, decrypt the following ciphertexts with the given keys:
'Kv?uqwpfu?rncwukdng?gpqwijB'
with key 2
ANSWER: It sounds plausible enough.
'XCBSw88S18A1S 2SB41SE .8zSEwAS50D5A5x81V'
with key 22
ANSWER: But all else of the world was invisible.
Which Python instruction would import a module named watermelon.py?
ANSWER: import watermelon
What do the following pieces of code display on the screen?
spam = 'foo' for i in spam: spam = spam + i print(spam)
ANSWER: foofoo
if 10 < 5: print('Hello') elif False: print('Alice') elif 5 != 5: print('Bob') else: print('Goodbye')
ANSWER: Goodbye
print('f' not in 'foo')
ANSWER: False
print('foo' in 'f')
ANSWER: False
print('hello'.find('oo'))
ANSWER: -1 (The find() method returns -1 when it can't find the string.)
Break the following ciphertext, decrypting one line at a time because each line has a different key. Remember to escape any quote characters:
qeFIP?eGSeECNNS, 5coOMXXcoPSZIWoQI, avnl1olyD4l'ylDohww6DhzDjhuDil, z.GM?.cEQc. 70c.7KcKMKHA9AGFK, ?MFYp2pPJJUpZSIJWpRdpMFY, ZqH8sl5HtqHTH4s3lyvH5zH5spH4t pHzqHlH3l5K Zfbi,!tif!xpvme!qspcbcmz!fbu!nfA
ANSWER:
I love my kitty,
My kitty loves me,
Together we're 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.
Yeah, she would probably eat me.
With paper and pencil, encrypt the following messages with the key 9 using the transposition cipher. The number of characters has been provided for your convenience.
Underneath a huge oak tree there was of swine a huge company, (61 characters)
ANSWER: Uhot on ahoamdakef pe r harhtesunnur wgyegewie,aeean t sec
That grunted as they crunched the mast: For that was ripe and fell full fast. (77 characters)
ANSWER: Tteeshiefheydtaplaad :telst ct t arhFwaf.gsueoanur n rsdlutcm lnhhatrf
Then they trotted away for the wind grew high: One acorn they left, and no more might you spy. (94 characters)
ANSWER: T atg:renishtwhr nfogperaeeO t hynoy wnt,mt. t w eh o ttfih earyheoniayneoedrdgc d uy hol m
In the following program, is each spam a global or local variable?
spam = 42 def foo(): global spam spam = 99 print(spam)
ANSWER: They are all global. (The global statement makes the spam variables in foo() global.)
What value does each of the following expressions evaluate to?
[0, 1, 2, 3, 4][2]
ANSWER: 2
[[1, 2], [3, 4]][0]
ANSWER: [1, 2]
[[1, 2], [3, 4]][0][1]
ANSWER: 2
['hello'][0][1]
ANSWER: 'e'
[2, 4, 6, 8, 10][1:3]
ANSWER: [4, 6]
list('Hello world!')
ANSWER: ['H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']
list(range(10))[2]
ANSWER: 2
What value does each of the following expressions evaluate to?
len([2, 4])
ANSWER: 2
len([])
ANSWER: 0
len(['', '', ''])
ANSWER: 3
[4, 5, 6] + [1, 2, 3]
ANSWER: [4, 5, 6, 1, 2, 3]
3 * [1, 2, 3] + [9]
ANSWER: [1, 2, 3, 1, 2, 3, 1, 2, 3, 9]
42 in [41, 42, 42, 42]
ANSWER: True
What are the four augmented assignment operators?
ANSWER: +=
, -=
, *=
, /=
Using paper and pencil, decrypt the following messages with the key 9. The _ marks a single space. The total number of characters has been counted for you.
H_cb__irhdeuousBdi___prrtyevdgp_nir__eerit_eatoreechadihf_paken_ge_b_te_dih_aoa.da_tts_tn (89 characters)
ANSWER: He picked up the acorn and buried it straight By the side of a river both deep and great.
A_b__drottthawa_nwar_eci_t_nlel_ktShw_leec,hheat_.na__e_soogmah_a__ateniAcgakh_dmnor__ (86 characters)
ANSWER: At length he came back, and with him a She And the acorn was grown to a tall oak tree.
Bmmsrl_dpnaua!toeboo’ktn_uknrwos._yaregonr_w_nd,tu__oiady_hgtRwt___A_hhanhhasthtev__e_t_e__eo (93 characters)
ANSWER: But with many a hem! and a sturdy stroke, At length he brought down the poor Raven’s own oak.
When you enter the following code into the interactive shell, what does each line print?
>>> math.ceil(3.0)
ANSWER: 3
>>> math.floor(3.1)
ANSWER: 3
>>> round(3.1)
ANSWER: 3
>>> round(3.5)
ANSWER: 4
>>> False and False
ANSWER: False
>>> False or False
ANSWER: False
>>> not not True
ANSWER: True
Draw the complete truth tables for the and, or, and not operators.
and Operator | ||||
A | and | B | == | Evaluates to |
True | and | True | == | True |
True | and | False | == | False |
False | and | True | == | False |
False | and | False | == | False |
or Operator | ||||
A | and | B | == | Evaluates to |
True | or | True | == | True |
True | or | False | == | True |
False | or | True | == | True |
False | or | False | == | False |
not Operator | |||
not | A | == | Evaluates to |
not | True | == | False |
not | False | == | True |
Which of the following is correct?
if __name__ == '__main__':
if __main__ == '__name__':
if _name_ == '_main_':
if _main_ == '_name_':
ANSWER: if __name__ == '__main__':
If you ran the following program and it printed the number 8 , what would it print the next time you ran it?
import random random.seed(9) print(random.randint(1, 10))
ANSWER: 8
(Setting the seed to the same number will cause the same random numbers to be generated.)
What does the following program print?
spam = [1, 2, 3] eggs = spam ham = eggs ham[0] = 99 print(ham == spam)
ANSWER: True
Which module contains the deepcopy() function?
ANSWER: The copy
module.
What does the following program print?
import copy spam = [1, 2, 3] eggs = copy.deepcopy(spam) ham = copy.deepcopy(eggs) ham[0] = 99 print(ham == spam)
ANSWER: False
Which is correct: os.exists()
or os.path.exists()
?
ANSWER: os.path.exists()
When is the Unix Epoch?
ANSWER: January 1st, 1970 at midnight, GMT
What do the following expressions evaluate to?
'Foobar'.startswith('Foo')
ANSWER: True
'Foo'.startswith('Foobar')
ANSWER: False
'Foobar'.startswith('foo')
ANSWER: False
'bar'.endswith('Foobar')
ANSWER: False
'Foobar'.endswith('bar')
ANSWER: True
'The quick brown fox jumped over the yellow lazy dog.'.title()
ANSWER: 'The Quick Brown Fox Jumped Over The Yellow Lazy Dog.'
What does the following code print?
spam = {'name': 'Al'} print(spam['name'])
ANSWER: 'Al'
What does this code print?
spam = {'eggs': 'bacon'} print('bacon' in spam)
ANSWER: False
What for
loop code would print the values in the following spam dictionary?
spam = {'name': 'Zophie', 'species':'cat', 'age':8}
ANSWER: spam = {'name': 'Zophie', 'species':'cat', 'age':8}
for key in spam:
print(spam[val])
What does the following line print?
print('Hello, world!'.split())
['Hello,', 'world!']
What will the following code print?
def spam(eggs=42):
print(eggs)
spam()
spam('Hello')
ANSWER: 42
Hello
What percentage of words in this sentence are valid English words?
"Whether it's flobulllar in the mind to quarfalog the slings and arrows of outrageous guuuuuuuuur."
ANSWER: 80% (12 out of 15 words is 12 / 15 == 0.8, or 80%)
What does this expression evaluate to?
' Hello world'.strip()
ANSWER: 'Hello world'
Which characters are whitespace characters?
ANSWER: space, tab, and newline
Why does 'Hello world'.strip('o')
evaluate to a string that still has Os in it?
ANSWER: Because strip()
will only remove 'o'
from the left or right side of the string.
Why does 'xxxHelloxxx'.strip('X')
evaluate to a string that still has Xs in it?
ANSWER: Because strip('X')
will only remove 'X'
, not 'x'
.
What do the following expressions evaluate to?
17 % 1000
ANSWER: 17
5 % 5
ANSWER: 0
What is the GCD of 10 and 15?
ANSWER: 5 (Because 5 is the largest number that evenly divides both 10 and 15.)
What does spam
contain after executing spam, eggs = 'hello', 'world'
?
ANSWER: 'hello'
The GCD of 17 and 31 is 1. Are 17 and 31 relatively prime?
ANSWER: Yes. (The definition of relatively prime is having a GCD of 1.)
Why aren’t 6 and 8 relatively prime?
ANSWER: Because their GCD is 2, not 1.
What is the formula for the modular inverse of A mod C?
ANSWER: The modular inverse i is where (A * i) % C == 1
The affine cipher is the combination of which two other ciphers?
ANSWER: The multiplicative and shift (or Caesar) cipher.
What is a tuple? How is a tuple different from a list?
ANSWER: A tuple is a data type that can contain multiple values like a list. Unlike a list, it's values are immutable and cannot be changed.
If Key A is 1, why does it make the affine cipher weak?
ANSWER: Because multiplying any number by 1 results in that number, and this means the affine cipher encrypts a letter to that same letter.
If Key B is 0, why does it make the affine cipher weak?
ANSWER: Because adding 0 to a number results in that number, and this means the affine cipher encrypts a letter to that same letter.
What does 2 ** 5
evaluate to?
ANSWER: 32
What does 6 ** 2
evaluate to?
ANSWER: 36
What does the following code print?
for i in range(5): if i == 2: continue print(i)
ANSWER:
0 1 3 4
Does the main() function of affineHacker.py get called if another program runs import affineHacker?
ANSWER: No. (This is because when a program is imported, the __name__
variable is set to 'affineHacker'
so main()
isn't called.)
Why can’t a brute-force attack be used against a simple substitution cipher, even with a powerful supercomputer?
ANSWER: There are too many possible keys, even for the most powerful computer.
What does the spam variable contain after running this code?
spam = [4, 6, 2, 8] spam.sort()
ANSWER: [2, 4, 6, 8]
What is a wrapper function?
ANSWER: A wrapper function calls another function, passing its arguments to the function and returning what the function returns.
What does 'hello'.islower() evaluate to?
ANSWER: True
What does 'HELLO 123'.isupper() evaluate to?
ANSWER: True
What does '123'.islower() evaluate to?
ANSWER: False
(All letters must be lowercase and the string must have at least one letter for islower()
to return True
What is the word pattern for the word hello?
ANSWER: '0.1.2.2.3'
Do mammoth and goggles have the same word pattern?
ANSWER: Yes. ('0.1.0.0.2.3.4'
)
Which word could be the possible plaintext word for the cipherword PYYACAO? Alleged, efficiently, or poodle?
ANSWER: Alleged (The word pattern for both "PYYACAO" and "Alleged" is 0.1.1.2.3.2.4)
Which cipher is the Vigenère cipher similar to, except that the Vigenère cipher uses multiple keys instead of just one key?
ANSWER: The Vigenère cipher is similar to the Caesar cipher. (If you use a key that is one character long for the Vigenère cipher, it becomes identical to the Caesar cipher.)
How many possible keys are there for a Vigenère key with a key length
of 10? a. Hundreds b. Thousands c. Millions d. More than a trillionANSWER: d. More than a trillion. (Technically, there are 141,167,095,653,376.)
What kind of cipher is the Vigenère cipher?
ANSWER: The Vigenère cipher is known as a polyalphabetic shift cipher because it is a shift cipher (like the Caesar cipher) that uses multiple sets of substitutions.
What is frequency analysis?
ANSWER: Counting the frequency of letters in a ciphertext as part of cryptanalysis.
What are the six most commonly used letters in English?
ANSWER: ETAOIN
What does the spam variable contain after you run the following code?
spam = [4, 6, 2, 8] spam.sort(reverse=True)
ANSWER: [8, 6, 4, 2]
If the spam
variable contains a dictionary, how can you get a list value of the keys in the dictionary?
ANSWER: list(spam.keys())
What is a dictionary attack?
ANSWER: A dictionary attack is a brute force attack using the words in an English dictionary for the possible keys.
What does Kasiski examination of a ciphertext reveal?
ANSWER: Kasiski examination can reveal the length of the Vigenère key used for a ciphertext.
What two changes happen when converting a list value to a set value with the set() function?
ANSWER: Duplicate values are removed and the order of the values is lost (unlike lists, values in sets do not have an order)
If the spam variable contains ['cat', 'dog', 'mouse', 'dog']
, this list has four items in it. How many items does the list returned from list(set(spam)) have?
ANSWER: 3 (The duplicate 'dog'
value is removed.)
What does the following code print?
print('Hello', end='') print('World')
ANSWER: HelloWorld (on one line)
Why isn’t a one-time pad program presented in this chapter?
ANSWER: Because using the Vigenère program with a random key that is as long as the message is the same thing as a one-time pad.
Which cipher is the two-time pad equivalent to?
ANSWER: Re-using a one-time pad twice makes the ciphertext equivalent to being encrypted with the Vigenère cipher.
Would using a key twice as long as the plaintext message make the one-time pad twice as secure?
ANSWER: No. It is equally secure as a regular one-time pad since the additional letters in the key aren't used.
How many prime numbers are there?
ANSWER: There are an infinite number of prime numbers. There is no "largest" prime number.
What are integers that are not prime called?
ANSWER: Composite numbers
What are two algorithms for finding prime numbers?
ANSWER: Any two of: Trial division, Rabin-Miller, and the Sieve of Eratosthenes.
What is the difference between a symmetric cipher and an asymmetric cipher?
ANSWER: A symmetric cipher uses the same key for encrypting and decrypting. An asymmetric cipher uses two different keys for encrypting and decrypting.
Alice generates a public key and a private key. Unfortunately, she later loses her private key.
a. Will other people be able to send her encrypted messages?
ANSWER: Yes. Other people will use Alice's public key to send encrypted messages to Alice.
b. Will she be able to decrypt messages previously sent to her?
ANSWER: No. Alice needs her private key to decrypt these encrypted messages.
c. Will she be able to digitally sign documents?
ANSWER: No. Alice needs her private key to digitially sign documents.
d. Will other people be able to verify her previously signed documents?
ANSWER: Yes. Other people will use Alice's public key to verify any documents she signed before she lost her private key.
What are authentication and confidentiality? How are they different?
ANSWER: Authentication is verifying a person's identity; that they are who they say they are. Confidentiality is keeping information secret and readable by the intended recipient.
What is non-repudiation?
ANSWER: Non-repudiation is the property of digital signatures that a person cannot later claim they didn't sign it.