Lab ready

This commit is contained in:
Chris Proctor 2025-01-15 19:51:15 -05:00
parent 6b5efac4cb
commit a0581341c7
10 changed files with 306 additions and 0 deletions

10
.commit_template Normal file
View File

@ -0,0 +1,10 @@
# -----------------------------------------------------------------
# Checkpoint 1: This is one of the first times you are being asked
# to write your own code to help you solve a problem. Describe your
# experience. Did writing code help you understand the problem better?
# explain.
#
# Checkpoint 2: Describe the strategy you used to find the polyalphabetic
# secret word, and then to decrypt the secret message.

1
.gitignore vendored
View File

@ -1 +1,2 @@
.DS_Store
__pycache__

25
answers.md Normal file
View File

@ -0,0 +1,25 @@
# Encryption lab answers
## Checkpoint 1
0. `secrets/secret0.txt` is encrypted using a Caesar Cipher. What is
its secret number?
1. `secrets/secret1.txt` is encrypted using a Caesar Cipher. What is
its secret number?
2. `secrets/secret2.txt` is encrypted using a Caesar Cipher. What is
its secret number?
3. `secrets/secret3.txt` is encrypted using a Caesar Cipher. What is
its secret number?
4. `secrets/secret4.txt` is encrypted using a Caesar Cipher. What is
its secret number?
## Checkpoint 2
5. What is the polyalphabetic secret word?
6. Decrypt this message, which was encrypted using the same secret word:
"EbZhdaV[h^bTpchhQnhig]X[VmhhRP]ftXVnRfjVY]fgtO_X]("

55
ciphers/caesar.py Normal file
View File

@ -0,0 +1,55 @@
from easybits import Bits
class CaesarCipher:
"""Implements the caeser cipher.
In the caeser cipher, each character is converted into a number,
then a secret number is added to each (if the result is too large, we
loop it back around), and the result is converted back into another
character.
int_min and int_max represent the lowest and highest allowed int values
of characters. They are set to include all the ASCII printable
characters (https://en.wikipedia.org/wiki/ASCII#Printable_character_table)
ASCII values outside this range (for example, '\n', the newline character),
just get passed through unencrypted.
"""
int_min = 32
int_max = 127
def __init__(self, secret):
self.secret = secret
def encrypt(self, plaintext):
"Converts a plaintext message into an encrypted ciphertext"
ciphertext = []
for char in plaintext:
plain_int = Bits(char, encoding='ascii').int
if self.int_min <= plain_int and plain_int < self.int_max:
cipher_int = self.rotate(self.secret, plain_int)
ciphertext.append(Bits(cipher_int, length=8).ascii)
else:
ciphertext.append(char)
return ''.join(ciphertext)
def decrypt(self, ciphertext):
"Converts an encrypted ciphertext into a plaintext message"
plaintext = []
for char in ciphertext:
cipher_int = Bits(char, encoding='ascii').int
if self.int_min <= cipher_int and cipher_int < self.int_max:
plain_int = self.rotate(-self.secret, cipher_int)
plaintext.append(Bits(plain_int, length=8).ascii)
else:
plaintext.append(char)
return ''.join(plaintext)
def rotate(self, secret, x):
"""Adds a secret number to x.
The modulo operator (%) is used to ensure that the result
is greater than equal to int_min and less than int_max.
"""
range_size = self.int_max - self.int_min
return (x + secret - self.int_min) % range_size + self.int_min

69
ciphers/poly.py Normal file
View File

@ -0,0 +1,69 @@
from itertools import cycle
from easybits import Bits
class PolyCipher:
"""Implements a polyalphabetic cipher.
The polyalphabetic cipher is like a Caesar cipher except that
the secret number changes for each character to be encrypted or
decrypted. This makes frequency analysis much harder, because
each plaintext space can be encrypted as a different character.
int_min and int_max represent the lowest and highest allowed int values
of characters. They are set to include all the ASCII printable
characters (https://en.wikipedia.org/wiki/ASCII#Printable_character_table)
ASCII values outside this range (for example, '\n', the newline character),
just get passed through unencrypted.
"""
int_min = 32
int_max = 127
def __init__(self, secret):
self.secret = secret
def encrypt(self, plaintext):
"Converts a plaintext message into an encrypted ciphertext"
ciphertext = []
for char, secret_char in zip(plaintext, cycle(self.secret)):
plain_int = Bits(char, encoding='ascii').int
if self.int_min <= plain_int and plain_int < self.int_max:
secret_int = self.get_int(secret_char)
cipher_int = self.rotate(secret_int, plain_int)
ciphertext.append(Bits(cipher_int, length=8).ascii)
else:
ciphertext.append(char)
return ''.join(ciphertext)
def decrypt(self, ciphertext):
"Converts an encrypted ciphertext into a plaintext message"
plaintext = []
for char, secret_char in zip(ciphertext, cycle(self.secret)):
cipher_int = Bits(char, encoding='ascii').int
if self.int_min <= cipher_int and cipher_int < self.int_max:
secret_int = self.get_int(secret_char)
plain_int = self.rotate(-secret_int, cipher_int)
plaintext.append(Bits(plain_int, length=8).ascii)
else:
plaintext.append(char)
return ''.join(plaintext)
def rotate(self, secret, x):
"""Adds a secret number to x.
The modulo operator (%) is used to ensure that the result
is greater than equal to int_min and less than int_max.
"""
range_size = self.int_max - self.int_min
return (x + secret - self.int_min) % range_size + self.int_min
def get_int(self, secret_char):
"""Converts an int or a single-character string into an int.
When `secret_char` is an int, we just return it. Otherwise we
return the character's ASCII value.
"""
if isinstance(secret_char, int):
return secret_char
else:
return Bits(secret_char, encoding='ascii').int

29
secrets/secret0.txt Normal file
View File

@ -0,0 +1,29 @@
ChVTanChVTaznQda]X]VnQaXVWczn
8]ncWTnU^aTbcbn^UncWTn]XVWc*n
FWPcnX\\^acP[nWP]Sn^anThTzn
2^d[SnUaP\TncWhnUTPaUd[nbh\\Tcah.
8]nfWPcnSXbcP]cnSTT_bn^anbZXTb|n
1da]cncWTnUXaTn^UncWX]TnThTb.
>]nfWPcnfX]VbnSPaTnWTnPb_XaT.
FWPcncWTnWP]SznSPaTnbTXiTncWTnUXaT.
0]SnfWPcnbW^d[STazntnfWPcnPacz
2^d[SncfXbcncWTnbX]Tfbn^UncWhnWTPac.
0]SnfWT]ncWhnWTPacnQTVP]nc^nQTPc|
FWPcnSaTPSnWP]S.ntnfWPcnSaTPSnUTTc.
FWPcncWTnWP\\Ta.nfWPcncWTnRWPX]z
8]nfWPcnUda]PRTnfPbncWhnQaPX].
FWPcncWTnP]eX[.nfWPcnSaTPSnVaPb_|
3PaTnXcbnSTPS[hncTaa^abnR[Pb_.
FWT]ncWTnbcPabncWaTfnS^f]ncWTXanb_TPabn
0]SnfPcTauSnWTPeT]nfXcWncWTXancTPab)
3XSnWTnb\X[TnWXbnf^aZnc^nbTT.
3XSnWTnfW^n\PSTncWTn;P\Qn\PZTncWTT.
ChVTanChVTanQda]X]VnQaXVWcz
8]ncWTnU^aTbcbn^UncWTn]XVWc)
FWPcnX\\^acP[nWP]Sn^anThTz
3PaTnUaP\TncWhnUTPaUd[nbh\\Tcah.

19
secrets/secret1.txt Normal file
View File

@ -0,0 +1,19 @@
Pof!nvtu!ibwf!b!njoe!pg!xjoufs
Up!sfhbse!uif!gsptu!boe!uif!cpvhit
Pg!uif!qjof.usfft!dsvtufe!xjui!topx<
Boe!ibwf!cffo!dpme!b!mpoh!ujnf
Up!cfipme!uif!kvojqfst!tibhhfe!xjui!jdf-
Uif!tqsvdft!spvhi!jo!uif!ejtubou!hmjuufs
Pg!uif!Kbovbsz!tvo<!boe!opu!up!uijol
Pg!boz!njtfsz!jo!uif!tpvoe!pg!uif!xjoe-
Jo!uif!tpvoe!pg!b!gfx!mfbwft-
Xijdi!jt!uif!tpvoe!pg!uif!mboe
Gvmm!pg!uif!tbnf!xjoe
Uibu!jt!cmpxjoh!jo!uif!tbnf!cbsf!qmbdf
Gps!uif!mjtufofs-!xip!mjtufot!jo!uif!topx-
Boe-!opuijoh!ijntfmg-!cfipmet
Opuijoh!uibu!jt!opu!uifsf!boe!uif!opuijoh!uibu!jt/

59
secrets/secret2.txt Normal file
View File

@ -0,0 +1,59 @@
z<DL.@LuLD.@LF<B;4L.;1L2.@FLB;12?LA52L.==92L/<B45@
m/<BALA52L969A6;4L5<B@2L.;1L5.==FL.@LA52L4?.@@LD.@L4?22;X
LLLLL!52L;645AL./<C2LA52L16;492L@A.??FX
LLLLLLLLLL!6:2L92AL:2L5.69L.;1L096:/
LLLLLs<912;L6;LA52L52F1.F@L<3L56@L2F2@X
m;1L5<;<B?21L.:<;4LD.4<;@LuLD.@L=?6;02L<3LA52L.==92LA<D;@
m;1L<;02L/29<DL.LA6:2LuL9<?19FL5.1LA52LA?22@L.;1L92.C2@
LLLLLLLLLL!?.69LD6A5L1.6@62@L.;1L/.?92F
LLLLLp<D;LA52L?6C2?@L<3LA52LD6;13.99L9645AZ
m;1L.@LuLD.@L4?22;L.;1L0.?23?22XL3.:<B@L.:<;4LA52L/.?;@
m/<BALA52L5.==FLF.?1L.;1L@6;46;4L.@LA52L3.?:LD.@L5<:2X
LLLLLu;LA52L@B;LA5.AL6@LF<B;4L<;02L<;9FX
LLLLLLLLLL!6:2L92AL:2L=9.FL.;1L/2
LLLLLs<912;L6;LA52L:2?0FL<3L56@L:2.;@X
m;1L4?22;L.;1L4<912;LuLD.@L5B;A@:.;L.;1L52?1@:.;XLA52L0.9C2@
.;4LA<L:FL5<?;XLA52L3<E2@L<;LA52L5699@L/.?821L092.?L.;1L0<91X
LLLLLLLLLLm;1LA52L@.//.A5L?.;4L@9<D9F
LLLLLu;LA52L=2//92@L<3LA52L5<9FL@A?2.:@Z
m99LA52L@B;L9<;4L6ALD.@L?B;;6;4XL6ALD.@L9<C29FXLA52L5.F
r6291@L5645L.@LA52L5<B@2XLA52LAB;2@L3?<:LA52L056:;2F@XL6ALD.@L.6?
LLLLLm;1L=9.F6;4XL9<C29FL.;1LD.A2?F
LLLLLLLLLLm;1L36?2L4?22;L.@L4?.@@Z
LLLLLm;1L;645A9FLB;12?LA52L@6:=92L@A.?@
m@LuL?<12LA<L@922=LA52L<D9@LD2?2L/2.?6;4LA52L3.?:L.D.FX
m99LA52L:<<;L9<;4LuL52.?1XL/92@@21L.:<;4L@A./92@XLA52L;645A7.?@
LLLLLr9F6;4LD6A5LA52L?608@XL.;1LA52L5<?@2@
LLLLLLLLLLr9.@56;4L6;A<LA52L1.?8Z
m;1LA52;LA<L.D.82XL.;1LA52L3.?:XL9682L.LD.;12?2?LD56A2
$6A5LA52L12DXL0<:2L/.08XLA52L0<08L<;L56@L@5<B912?fL6ALD.@L.99
LLLLL 56;6;4XL6ALD.@Lm1.:L.;1L:.612;X
LLLLLLLLLL!52L@8FL4.A52?21L.4.6;
LLLLLm;1LA52L@B;L4?2DL?<B;1LA5.ALC2?FL1.FZ
<L6AL:B@AL5.C2L/22;L.3A2?LA52L/6?A5L<3LA52L@6:=92L9645A
u;LA52L36?@AXL@=6;;6;4L=9.02XLA52L@=299/<B;1L5<?@2@LD.986;4LD.?:
LLLLL{BAL<3LA52LD56;;F6;4L4?22;L@A./92
LLLLLLLLLL{;LA<LA52L36291@L<3L=?.6@2Z
m;1L5<;<B?21L.:<;4L3<E2@L.;1L=52.@.;A@L/FLA52L4.FL5<B@2
";12?LA52L;2DL:.12L09<B1@L.;1L5.==FL.@LA52L52.?ALD.@L9<;4X
LLLLLu;LA52L@B;L/<?;L<C2?L.;1L<C2?X
LLLLLLLLLLuL?.;L:FL522192@@LD.F@X
LLLLLyFLD6@52@L?.021LA5?<B45LA52L5<B@2L5645L5.F
m;1L;<A56;4LuL0.?21XL.AL:FL@8FL/9B2LA?.12@XLA5.ALA6:2L.99<D@
u;L.99L56@LAB;23B9LAB?;6;4L@<L32DL.;1L@B05L:<?;6;4L@<;4@
LLLLLn23<?2LA52L05691?2;L4?22;L.;1L4<912;
LLLLLLLLLLr<99<DL56:L<BAL<3L4?.02X
z<A56;4LuL0.?21XL6;LA52L9.:/LD56A2L1.F@XLA5.ALA6:2LD<B91LA.82L:2
"=LA<LA52L@D.99<DLA5?<;421L9<3AL/FLA52L@5.1<DL<3L:FL5.;1X
LLLLLu;LA52L:<<;LA5.AL6@L.9D.F@L?6@6;4X
LLLLLLLLLLz<?LA5.AL?616;4LA<L@922=
LLLLLuL@5<B91L52.?L56:L39FLD6A5LA52L5645L36291@
m;1LD.82LA<LA52L3.?:L3<?2C2?L3921L3?<:LA52L0569192@@L9.;1Z
{5L.@LuLD.@LF<B;4L.;1L2.@FL6;LA52L:2?0FL<3L56@L:2.;@X
LLLLLLLLLL!6:2L5291L:2L4?22;L.;1L1F6;4
LLLLL!5<B45LuL@.;4L6;L:FL05.6;@L9682LA52L@2.Z

14
secrets/secret3.txt Normal file
View File

@ -0,0 +1,14 @@
3DAJ[PK[PDA[OAOOEKJO[KB[OSAAP[OEHAJP[PDKQCDP
%[OQIIKJ[QL[NAIAI>N=J?A[KB[PDEJCO[L=OPg
%[OECD[PDA[H=?G[KB[I=JU[=[PDEJC[%[OKQCDPg
|J@[SEPD[KH@[SKAO[JAS[S=EH[IU[@A=N[PEIAbO[S=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@0/&4L@"/%@0/@501@#*(@%"3,@#-0#4@#63/&%
l*,&@"@1-"5&@0'@&:&4N@o63@)"/%4@8&3&@1&11&3&%
w*5)@5)03/@13*$,4L@063@1"-.4@45*$,:@"4@b-6&#&"3%G4N
w&@)0"3%&%@5)&@'3&4)@#&33*&4@*/@5)&@#:3&N
b65@8)&/@5)&@#"5)@8"4@'*--&%@8&@'06/%@"@'63L
a@3"5M(3&:@'6/(64L@(-655*/(@0/@063@$"$)&N
t)&@+6*$&@8"4@45*/,*/(@500N@o/$&@0''@5)&@#64)
t)&@'36*5@'&3.&/5&%L@5)&@48&&5@'-&4)@806-%@563/@4063N
i@"-8":4@'&-5@-*,&@$3:*/(N@i5@8"4/G5@'"*3
t)"5@"--@5)&@-07&-:@$"/'6-4@4.&-5@0'@305N
e"$)@:&"3@i@)01&%@5)&:G%@,&&1L@,/&8@5)&:@806-%@/05N