Slanted Cipher
Recall from Week 2 that Caesar’s cipher, otherwise known as an affine cipher, encrypts messages by mapping each letter therein to a number (e.g., A to 0 and Z to 25), shifting each number by some number of places (otherwise known as a "key"), and mapping each number back to a letter.
Transposition ciphers, by contrast, encrypt messages by permuting (i.e., rearranging) the characters therein according to some pattern. For instance, consider the "slanted cipher," below, which relocates characters in a message as follows.
Given this message:
HELLO WORLD
Relocate every other character (even if it’s whitespace), starting with the second, just below its original position, yielding a "depth" of 2:
H L O W R D
E L O L
Notice the slant. Indeed, you can still read that message diagonally, left to right.
Next, relocate the slanted characters, in order, to the end of what was once the original message, back to a depth of 1:
HLOWRDEL OL
Once encrypted, then, HELLO WORLD
becomes HLOWRDEL OL
.
Alternatively, we could "slant" the same message to a depth of 3 instead of 2 as follows:
H L W L
E O O D
L R
And then relocate the characters at depth 2, followed by the characters at depth 3:
HLWLEOODL R
The depth to which you slant a message is thus the "key" via which you’ve encrypted the message.
Answer the below in slanted.md
and slanted.py
.
Questions
-
(2 points.) What does it mean for a cipher to be "secure"?
-
(2 points.) Why is Caesar’s cipher not very secure?
-
(2 points.) Why is this slanted cipher not very secure?
-
(1 point.) Why would it not be secure at all to slant a message with n characters to a depth of n?
-
(2 points.) Recall from Problem Set 8 that we didn’t encrypt users' passwords but "hashed" them instead using
generate_password_hash
. In what way is hashing a message different from encrypting a message? -
(6 points.) Complete the implementation of
slant
inslanted.py
in such a way that it encryptsmessage
usingdepth
, returning the resulting ciphertext as astr
. You might find the (optional) third parameter ofrange
to be of help!
Debrief
-
Which resources, if any, did you find helpful in answering this problem’s questions?
-
About how long, in minutes, did you spend on this problem’s questions?