ex01_crypto
This commit is contained in:
1
doc/README.md
Normal file
1
doc/README.md
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Put your `report.pdf` here.
|
||||||
123
src/ex01_test.py
Normal file
123
src/ex01_test.py
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
'''
|
||||||
|
@author: Christian Wressnegger
|
||||||
|
'''
|
||||||
|
|
||||||
|
try:
|
||||||
|
# For evaluating the exercises we'll provide a similar but
|
||||||
|
# different configuration that contains alternative input
|
||||||
|
# values than those provided in the script that was handed
|
||||||
|
# out (nothing mean though). Develop your solution robust
|
||||||
|
# enough to work with various kinds and variations of input.
|
||||||
|
import ex01_testdata_lecturer as testdata # @UnresolvedImport @UnusedImport
|
||||||
|
|
||||||
|
except:
|
||||||
|
import ex01_testdata as testdata # @UnusedImport
|
||||||
|
|
||||||
|
|
||||||
|
import filecmp
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
unittest.TestLoader.sortTestMethodsUsing = None
|
||||||
|
PYTHON = "python3"
|
||||||
|
PYERROR = "For running your solutions we call '{}'.\nThe name might be different for your installation (e.g. on Windows)\n"
|
||||||
|
|
||||||
|
|
||||||
|
class Ex01(unittest.TestCase):
|
||||||
|
|
||||||
|
TASKS = 0
|
||||||
|
MY_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def call(tool, params):
|
||||||
|
script = os.path.join(Ex01.MY_DIR, tool)
|
||||||
|
cmd = '{} "{}" {}'.format(PYTHON, script, params)
|
||||||
|
|
||||||
|
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
|
||||||
|
out, _ = p.communicate()
|
||||||
|
|
||||||
|
if p.returncode != 0:
|
||||||
|
sys.stderr.write(PYERROR.format(PYTHON))
|
||||||
|
|
||||||
|
return out, p.returncode
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def tmpfile():
|
||||||
|
fd, fname = tempfile.mkstemp(dir=Ex01.MY_DIR)
|
||||||
|
os.close(fd)
|
||||||
|
return fname
|
||||||
|
|
||||||
|
def _check_encrypttool(self, tool, x, s=slice(0, None)):
|
||||||
|
for alphabet, a, b in testdata.TOOL_MAP[tool][x][s]:
|
||||||
|
fname = Ex01.tmpfile()
|
||||||
|
|
||||||
|
with open(fname, 'wb') as f:
|
||||||
|
f.write(a)
|
||||||
|
f.seek(0)
|
||||||
|
|
||||||
|
out, _ = Ex01.call(
|
||||||
|
tool, '--{} {} "{}"'.format(x, alphabet, fname))
|
||||||
|
|
||||||
|
os.remove(fname)
|
||||||
|
self.assertEqual(out.strip(), b, "wrong {}ion".format(x))
|
||||||
|
|
||||||
|
def _check_analysistool(self, tools, params=""):
|
||||||
|
for plaintext, ciphertext in testdata.TOOL_MAP[tools[0]]:
|
||||||
|
k, _ = Ex01.call(tools[0], '"{}" {}'.format(ciphertext, params))
|
||||||
|
k = k.strip().decode('ascii')
|
||||||
|
|
||||||
|
self.assertTrue(len(k) > 0, "No key extracted")
|
||||||
|
|
||||||
|
fname1, fname2 = Ex01.tmpfile(), Ex01.tmpfile()
|
||||||
|
|
||||||
|
Ex01.call(tools[1],
|
||||||
|
'--decrypt {} "{}" --out "{}"'.format(k, ciphertext, fname1))
|
||||||
|
|
||||||
|
Ex01.call("mono/mono.py",
|
||||||
|
'--encrypt abcdefghijklmnopqrstuvwxyz "{}" --out "{}"'.format(plaintext, fname2))
|
||||||
|
|
||||||
|
ok = os.path.getsize(fname1) > 0 and filecmp.cmp(fname1, fname2)
|
||||||
|
os.remove(fname1)
|
||||||
|
os.remove(fname2)
|
||||||
|
|
||||||
|
self.assertTrue(ok, "Incorrect key/ decryption :(")
|
||||||
|
|
||||||
|
def test_04_mono(self):
|
||||||
|
TOOL = "mono/mono.py"
|
||||||
|
self._check_encrypttool(TOOL, "encrypt", slice(0, 1))
|
||||||
|
self._check_encrypttool(TOOL, "encrypt", slice(1, None))
|
||||||
|
self._check_encrypttool(TOOL, "decrypt")
|
||||||
|
Ex01.TASKS += 1
|
||||||
|
|
||||||
|
def test_05_breakmono(self):
|
||||||
|
TOOLS = ("mono/break_mono.py", "mono/mono.py")
|
||||||
|
self._check_analysistool(TOOLS)
|
||||||
|
Ex01.TASKS += 1
|
||||||
|
|
||||||
|
def test_06_vig(self):
|
||||||
|
TOOL = "vigenere/vig.py"
|
||||||
|
self._check_encrypttool(TOOL, "encrypt")
|
||||||
|
self._check_encrypttool(TOOL, "decrypt")
|
||||||
|
Ex01.TASKS += 1
|
||||||
|
|
||||||
|
def test_07_breakvig(self):
|
||||||
|
TOOLS = ("vigenere/break_vig.py", "vigenere/vig.py")
|
||||||
|
self._check_analysistool(
|
||||||
|
TOOLS, "--keylen {}".format(testdata.BREAK_VIG_KEYLEN))
|
||||||
|
Ex01.TASKS += 1
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_XX(self):
|
||||||
|
if Ex01.TASKS > 0:
|
||||||
|
print("[*] {} out of {} exercises work flawlessly! 👍".format(Ex01.TASKS, 4))
|
||||||
|
else:
|
||||||
|
print("[*] Unfortunately, non of the exercises work as expected (yet 😉)")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
45
src/ex01_testdata.py
Normal file
45
src/ex01_testdata.py
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
'''
|
||||||
|
@author: Christian Wressnegger
|
||||||
|
'''
|
||||||
|
|
||||||
|
import os
|
||||||
|
MY_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
MONO = {
|
||||||
|
"encrypt": [
|
||||||
|
("abcdefghijklmnopqrstuvwxyz", b"Exercise 01", b"exercise"),
|
||||||
|
("vyjlnfubidxaqmshprwkgeoztc",
|
||||||
|
b"Security exercises are fun", b"wnjgriktnznrjiwnwvrnfgm")
|
||||||
|
],
|
||||||
|
"decrypt": [
|
||||||
|
("vyjlnfubidxaqmshprwkgeoztc",
|
||||||
|
b"wnjgriktnznrjiwnwvrnfgm", b"securityexercisesarefun")
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
BREAK_MONO = {
|
||||||
|
(os.path.join(MY_DIR, 'mono/plaintext.txt'),
|
||||||
|
os.path.join(MY_DIR, 'mono/ciphertext.txt'))
|
||||||
|
}
|
||||||
|
|
||||||
|
VIG = {
|
||||||
|
"encrypt": [
|
||||||
|
("abcdefghijklmnopqrstuvwxyz", b"Exercise 01", b"eygugnyl"),
|
||||||
|
("sectubs",
|
||||||
|
b"Security exercises are fun", b"kienljlqizxldakiutlfxmr")
|
||||||
|
],
|
||||||
|
"decrypt": [
|
||||||
|
("sectubs",
|
||||||
|
b"kienljlqizxldakiutlfxmr", b"securityexercisesarefun")
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
BREAK_VIG = {
|
||||||
|
(os.path.join(MY_DIR, 'vigenere/plaintext.txt'),
|
||||||
|
os.path.join(MY_DIR, 'vigenere/ciphertext.txt'))
|
||||||
|
}
|
||||||
|
|
||||||
|
BREAK_VIG_KEYLEN = 5
|
||||||
|
|
||||||
|
TOOL_MAP = {"mono/mono.py": MONO, "mono/break_mono.py": BREAK_MONO,
|
||||||
|
"vigenere/vig.py": VIG, "vigenere/break_vig.py": BREAK_VIG}
|
||||||
15
src/mono/ciphertext.txt
Normal file
15
src/mono/ciphertext.txt
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
gryticdettpjcjgtmtntajgryticdkrqstmkojgjgtmtrmjgtatnrgdpmatmjgcdn
|
||||||
|
rpmhgoqmatpetopzsoqqtmfcajgtoaeatrmrpmjgtfozdatnmcpjqotjgtinltrsc
|
||||||
|
fgdwrpmontrntedjktmckgrjktkrpjrpmktjgopskgrjktlqtrntgryticdqoytmj
|
||||||
|
gttultaotphtgryticdkojptnntmjgtlqrzdtltclqtwrsopzereotnncwtjowtnx
|
||||||
|
dnjjctnhrltopjgonqrpmcfhcwltjojocpjgthcwlrnnocponzcptitjktozpcatj
|
||||||
|
gtpttmirpmktsttlldngopzcpktsttlldngopzcpjgononxdnjrldpsachsncpzka
|
||||||
|
ojjtpfcajgtltclqtkgchrpnttncwtjgopznkacpzqostrpjnoprhcqcpiktmccda
|
||||||
|
ngratedjjgtatnncwrpicjgtafdhsopopnthjncdjjgtatrpmjgononxdnjrldpsa
|
||||||
|
chsncpzqostkcastanoprfrhjcaiktmccdangratedjjgtatnncwrpicjgtafdhso
|
||||||
|
pacecjncdjjgtatgryticdyonojtmjgtbdrzwoatgryticdnkrwopjgtngojjgtlr
|
||||||
|
ajihcpytpjocpnrpmjgtatrqlcqojosjgtfrhtnrqkrinmofftatpjjgtagtjcaoh
|
||||||
|
jgtnrwtedjktnkrqqckojrpmktnttpcjgopzhgrpztpcjgopzgrnhgrpztmjtpwoq
|
||||||
|
qocpmcqqrancprqcnopzhrwlrozpjktpjiwoqqocpnjrayopzrpmkaojgopzoplro
|
||||||
|
peoznjacpzltclqtdpkoqqopzjczoytnwrqqopyonocprpmltanlthjoytcptopfo
|
||||||
|
ytsomnetqckjgtlcytajiqoptcptlcldqrjocpadppopcdjcfjowt
|
||||||
1044
src/mono/common.txt
Normal file
1044
src/mono/common.txt
Normal file
File diff suppressed because it is too large
Load Diff
35
src/mono/plaintext.txt
Normal file
35
src/mono/plaintext.txt
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
have you been to the desert?
|
||||||
|
have you walked with the dead?
|
||||||
|
there's a hundred thousand children being killed for their bread
|
||||||
|
|
||||||
|
and the figures don't lie they speak of human disease
|
||||||
|
but we do what we want and we think what we please
|
||||||
|
|
||||||
|
have you lived the experience?
|
||||||
|
have you witnessed the plague?
|
||||||
|
people making babies sometimes just to escape
|
||||||
|
in this land of competition the compassion is gone
|
||||||
|
yet we ignore the needy and we keep pushing on
|
||||||
|
we keep pushing on
|
||||||
|
|
||||||
|
this is just a punk rock song
|
||||||
|
written for the people who can see something's wrong
|
||||||
|
like ants in a colony we do our share
|
||||||
|
but there's so many other fuckin' insects out there
|
||||||
|
and this is just a punk rock song
|
||||||
|
(like workers in a factory we do our share
|
||||||
|
but there's so many other fuckin' robots out there)
|
||||||
|
|
||||||
|
have you visited the quagmire?
|
||||||
|
have you swam in the shit?
|
||||||
|
the party conventions and the real politik
|
||||||
|
the faces always different, the rhetoric the same
|
||||||
|
but we swallow it, and we see nothing change
|
||||||
|
nothing has changed...
|
||||||
|
|
||||||
|
ten million dollars on a losing campaign
|
||||||
|
twenty million starving and writhing in pain
|
||||||
|
big strong people unwilling to give
|
||||||
|
small in vision and perspective
|
||||||
|
one in five kids below the poverty line
|
||||||
|
one population runnin' out of time
|
||||||
17
src/vigenere/ciphertext.txt
Normal file
17
src/vigenere/ciphertext.txt
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
zvwxgybcyyzsgymlhvjcssflctqmgpuorhyyhgdqzsayfogwxrnsfjyrhvnlmywhi
|
||||||
|
hoqpughqmgzqfzkhzsxckhvjbxcksgtukfrivhmclozqglssqhaghycxfwgjkopts
|
||||||
|
zwhyfghgxyxqoxkrshnrhifsgsucslgaopcghcfqzkvjlohtfjrgouyxhwreubbfp
|
||||||
|
gwgjkeuzfqyopttkammcgfhyfkbgtkkcbjqnciyqzvoyqcvoyrnsmlcztcwyrzhmc
|
||||||
|
esowqutvnrgbrwsttcwyrzhmcvwgxzxcyjzgbrxmtjvbfkfsigjozqrnswwkubsde
|
||||||
|
urcsrcsoqjqbcbngfoxgzwqrsywqnljigypeognrjsgypumgnryszkukzzxfukhmc
|
||||||
|
svcbgzggznvcgjbzcpjkagwhuxwhycttftkjsjtrocbsmzoaggzwcsluhttploajx
|
||||||
|
kfcucudzjyxsscnrcwycjhvjpkofjluhfnaqgiumafgqckjslmtbokgmvhfegwbxr
|
||||||
|
zvsryygounkozbcxsutltoynjrhvjqkjsspkqcwbjsoqkgyswcicfiqzvoyfgjsrm
|
||||||
|
xshmytcbjeucrxmtuhmcjwbtqgifxuozzxjukzdbososborcgcrwsactccsccwzqa
|
||||||
|
xmwrhaghksiywsemzoigsucslgpsyfkfsymcohhfzvskyrzdwcnwgymxwqrsywqnl
|
||||||
|
jigypehvwcktsjrobzfzxsoyyxslygtqhnmtbsacxtsqrycutmjwtdmahvnlqobdm
|
||||||
|
tsktsrrtjcrpoijemczyxsgfbrmanqzoyjlzvsygssvfqicajdufsamrihnmttihi
|
||||||
|
iczqsywcsiozzyfktwaccvoycbsfmyvdsscjhcyfkvosbyvopccvoycbsfmyvdssc
|
||||||
|
jhcicgzgsmubsbmazrgpkoybfghvfnvsbjbzcwsrkufnrewhxqzwzqrnsfjgzozby
|
||||||
|
egkfqlcfujgmwsesignapigyzkqozqkoanjrwcspkogtlykvdyrzrnlugozpykwqj
|
||||||
|
jws
|
||||||
51
src/vigenere/plaintext.txt
Normal file
51
src/vigenere/plaintext.txt
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
This is not a test of the Emergency Broadcast System. This is the real
|
||||||
|
thing
|
||||||
|
|
||||||
|
Kick back watch it crumble
|
||||||
|
See the drowning, watch the fall
|
||||||
|
I feel just terrible about it
|
||||||
|
That's sarcasm, let it burn
|
||||||
|
|
||||||
|
I'm gonna make a toast when it falls apart
|
||||||
|
I'm gonna raise my glass above my heart
|
||||||
|
Then someone shouts "That's what they get!"
|
||||||
|
|
||||||
|
For all the years of hit and run
|
||||||
|
For all the piss broke bands on VH1
|
||||||
|
Where did all, their money go?
|
||||||
|
Don't we all know
|
||||||
|
|
||||||
|
Parasitic music industry
|
||||||
|
As it destroys itself
|
||||||
|
We'll show them how it's supposed to be
|
||||||
|
|
||||||
|
Music written from devotion
|
||||||
|
Not ambition, not for fame
|
||||||
|
Zero people are exploited
|
||||||
|
There are no tricks, up our sleeve
|
||||||
|
|
||||||
|
Gonna fight against the mass appeal
|
||||||
|
We're gonna kill the seven record deal
|
||||||
|
Make records that have more than one good song
|
||||||
|
The dinosaurs will slowly die
|
||||||
|
And I do believe no one will cry
|
||||||
|
I'm just fucking glad I'm gonna be
|
||||||
|
There to watch the fall
|
||||||
|
|
||||||
|
Prehistoric music industry
|
||||||
|
Three feet in la brea tar
|
||||||
|
Extinction never felt so good
|
||||||
|
|
||||||
|
If you think anyone would feel badly
|
||||||
|
You are sadly, mistaken
|
||||||
|
The time has come for evolution
|
||||||
|
Fuck collusion, kill the five
|
||||||
|
|
||||||
|
Whatever happened to the handshake?
|
||||||
|
Whatever happened to deals no-one would break?
|
||||||
|
What happened to integrity?
|
||||||
|
It's still there it always was
|
||||||
|
For playing music just because
|
||||||
|
A million reasons why
|
||||||
|
|
||||||
|
(All) dinosaurs will die
|
||||||
117770
src/vigenere/words.txt
Normal file
117770
src/vigenere/words.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user