Cryptography Programming Workshop, Project 1: Submission Instructions
Cryptography Programming Workshop, Project 1: Submission Instructions
This project will work with binary strings. (Note that in class we discussed
character strings but the requirements of the project are different to make it
simpler.)
You are required to implement the programs in Python. (If you plan on
using special modules in Python then ask us before using them, to make sure
that we allow it.) You can use any python version higher than 3.6.
For each program, you should also write a short description of the
algorithm that you implemented.
Prepare your answer alone. If you use some resource on the web
(Wikipedia, github, stackoverflow, etc.) then clearly describe how
you used that resource.
Submission Instructions
The submission should contain a single file (‘.py’, see below). The first line of
your file must be a comment with:
⟨f irstname⟩⟨comma⟩⟨lastname⟩⟨comma⟩⟨ID⟩
The second line of your file must be a comment:
⟨P ython V ersion⟩
Example:
#I s r a e l , I s r a e l i , 1 2 3 4 5 6 7 8 9
#Python 3 . 8
Attach this file to an email with TITLE: “Sadna Crypto Ex1 ⟨ID⟩” No need
to add text to the email. Send the email to ‘[email protected]’. A submission
that does not follow the above format might be skipped (without being checked
and graded).
Python
Submit a file named ex1.py with the implementation of the following functions:
• my xor(lhs, rhs)
• original1(encrypted)
1
• original2(encrypted)
• repeated xor(plaintext, key)
• break vigenere(source path, target path)
1 Fixed XOR
Write a function that takes two equal-length binary strings (‘bytes‘) and returns
a bytes string which is the XOR of the two input strings. For example, if the
first ASCII character of the first string is represented by 0x11 and the first
character in the second string is represented by 0x22, then the first char of the
output is 0x33. (You can try this by the python command ”hex(0x11ˆ0x22)”;
the ‘ˆ’ symbol in python represents the XOR operation).
The required function signature is:
my xor(lhs, rhs)
1.1 Example
r e s = my xor ( b”\ x11 \ x22 ” , b”\ x22 \ x11 ” )
Input (represented in hex):
lhs=”1122”, rhs=”2211”
Output:
res=”3333”
2
2. In the second variant, buf is an arbitrary English text with spaces, upper
case letters, digits, and the punctuation marks ,.:!?. The required func-
tion signature is:
original2(encrypted)
We will test your programs using a random English text that matches the
above requirements.
Note that if you have a method to identify if a certain string is an English
text then it is easy to identify the right key. The program should work well
with an input text of about 100 characters. Bonus points will be awarded to a
program which works well with inputs of about 40 characters.
2.1 Example
Text with only lower case English:
thebaldeagle
The plaintext is:
b”thebaldeagle”
We choose the key ‘L’:
b”L”
By XORing the text with the key we get (hexlified) encrypted:
3824292e2d2028292d2b2029
plaintext =
o r i g i n a l h e x 1 ( b”\ x38 \ x24 \ x29 \ x2e \ x2d \ x20 \ x28 \ x29 \ x2d \ x2b \ x20 \ x29 ” )
Input:
encrypted=b”\x38\x24\x29\x2e\x2d\x20\x28\x29\x2d\x2b\x20\x29”
Output:
plaintext=b”\x74\x68\x65\x62\x61\x6c\x64\x65\x61\x67\x6c\x65”
3
3.1 Example
Text
thebaldeagle
The plaintext is:
b”\x74\x68\x65\x62\x61\x6c\x64\x65\x61\x67\x6c\x65”
We choose the key “LMN”:
b”\x4C\x4D\x4E”
By XORing the text with the key we get encrypted:
b”\x38\x25\x2b\x2e\x2c\x22\x28\x28\x2f\x2b\x21\x2b”
res =
r e p e a t e d x o r ( b”\ x74 \ x68 \ x65 \ x62 \ x61 \ x6c \ x64 \ x65 \ x61 \ x67 \ x6c \ x65 ” ,
”\x4C\x4D\x4E ” )
Input:
plaintext=b”\x74\x68
\x65\x62\x61\x6c\x64\x65\x61\x67\x6c\x65”, key=”\x4C\x4D\x4E”
Output:
res= b”\x38\x25\x2b\x2e\x2c\x22\x28\x28\x2f\x2b\x21\x2b”
4.1 Example
Assume we have the following plaintext
thebaldeagle
Which, in hex representation is:
4
74686562616C646561676C65
We choose the key “LMN”:
4C4D4E
By XORing the text with the key we get the ciphertext:
38252B2E2C2228282F2B212B
So the file ‘<source path>’ contains the bytes with hex representation:
38252B2E2C2228282F2B212B
5
• The smallest t value which results in a substantially higher frequency of
zeroes is probably the key length.
A more involved method could compute the sum-of-squares of the differences
between the observed distribution of characters in XOR, and the uniform dis-
tribution. This value should be maximal when we find the correct value of t.
Another method could compute the sum-of-squares of the differences between
the observed distribution of characters in XOR, and the distribution of the XOR
of two English texts. This value should be minimal when we find the correct
value of t.
6
Try to design a function which will work with the shortest possible input
length.