# Lab2 ## Table of contents [Task 1: Frequency analysis](#Task-1) [Task 2: Encryption using different ciphers and modes](#Task-2) [Task 3: Encryption mode](#Task-3) [Task 4: Padding](#Task-4) [Task 5: Error propagation](#Task-5) [Task 6: Initial Vector and common mistakes](#Task-6) [Task 6.1 ... ](#61) [Task 6.2 ... ](#62) [Task 6.3 ... ](#63) [Task 7: Programming using the crypto library](Task-7) ## Task 1 ### Our method to decrypt the file We started by writing a program to count the frequency of the letters. With the information we got we compared it to the most frequently occuring letters when writing english (according to [wikipedia](https://en.wikipedia.org/wiki/Frequency_analysis#/media/File:English_letter_frequency_(alphabetic).svg)). This made a few letters easy to detect. **Program to plot the frequency of letters in the ciphertext** ```python import sys import matplotlib.pyplot as plt f = sys.argv[1] h = {"a": 0, "b": 0, "c": 0, "d": 0, "e": 0, "f": 0, "g": 0, "h": 0, "i": 0, "j": 0, "k": 0, "l": 0, "m": 0, "n": 0, "o": 0, "p": 0, "q": 0, "r": 0, "s": 0, "t": 0, "u": 0, "v": 0, "w": 0, "x": 0, "y": 0, "z": 0, } with open(f, "r") as f: data = f.read() for c in data: try: h[c] += 1 except: pass h = dict(sorted(dict(h).items(), key=lambda x: x[1], reverse=True)) x = h.keys() y = h.values() plt.bar(x, y) plt.show() ``` ![Output from frequency-counter](https://i.imgur.com/LmNDccQ.png) > The frequency of letters in the encrypted text From the graph we can easily read that the most frequent letter in the given ciphertext is the letter `n`. Which according to [wikipedia](https://en.wikipedia.org/wiki/Frequency_analysis) should match with `e`, this tells us that most certaintly `n` is the encrypted `e`'s. But trying to make a direct replace (maybe through `tr`) does not work because the difference in frequency does not differ very much between some of the letters. For the rest of the decryption we choose to write a program that will replace the letters so we could speed up the workflow instead of using the `tr` command all the time. The program will take "arguments" in the form of a dictionary (updated by us before running the program), which will be all the letters that will be replaced and what to replace them with. In this example we choose to only leave `n -> e` as an example. We fix the problem with mixing between replaced letters (new letters) and already existing letters (cipered letters). This to avoid confusion in the scenario where we first apply the substitution `n -> e` and then apply the substitution `e -> w`. With this order of substitutions we want to convert all existing ciphered `e` and `n` to `w` and `e` respectively. Since our program naively did not separate between letters replaced by the program and naturally occuring letters we had to implement that functionality. **The program for replacement** ```python import sys from collections import defaultdict # All letters and what to change to. In from:to format change = { "n": "e" } f = sys.argv[1] with open(f, "r") as f: data = f.read() data = [x for x in data] d = defaultdict(list) # Append all indexes for each letter for index, c in enumerate(data): d[c].append(index) # Replace for key, value in change.items(): for i in d[key]: data[i] = value with open("decrypted", "w") as fh: fh.write(''.join(data)) ``` > Output will appear as a semi decrypted file that only decrypts the letters given in the dictionary. Then with the help of the program we started to look for patterns and words. This was a time consuming process but after a couple of minutes the whole text was decrypted. The workflow was that when finding a new letter that matches in a word we added it to the dictionary, ran the program again and start finding new patterns. ### The decrypted file ``` the oscars turn on sunday which seems about right after this long strange awards trip the bagger feels like a nonagenarian too the awards race was bookended by the demise of harvey weinstein at its outset and the apparent implosion of his film company at the end and it was shaped by the emergence of metoo times up blackgown politics armcandy activism and a national conversation as brief and mad as a fever dream about whether there ought to be a president winfrey the season didnt just seem extra long it was extra long because the oscars were moved to the first weekend in march to avoid conflicting with the closing ceremony of the winter olympics thanks pyeongchang one big question surrounding this years academy awards is how or if the ceremony will address metoo especially after the golden globes which became a jubilant comingout party for times up the movement spearheaded by powerful hollywood women who helped raise millions of dollars to fight sexual harassment around the country signaling their support golden globes attendees swathed themselves in black sported lapel pins and sounded off about sexist power imbalances from the red carpet and the stage on the air e was called out about pay inequity after its former anchor catt sadler quit once she learned that she was making far less than a male cohost and during the ceremony natalie portman took a blunt and satisfying dig at the allmale roster of nominated directors how could that be topped as it turns out at least in terms of the oscars it probably wont be women involved in times up said that although the globes signified the initiatives launch they never intended it to be just an awards season campaign or one that became associated only with redcarpet actions instead a spokeswoman said the group is working behind closed doors and has since amassed million for its legal defense fund which after the globes was flooded with thousands of donations of or less from people in some countries no call to wear black gowns went out in advance of the oscars though the movement will almost certainly be referenced before and during the ceremony especially since vocal metoo supporters like ashley judd laura dern and nicole kidman are scheduled presenters another feature of this season no one really knows who is going to win best picture arguably this happens a lot of the time inarguably the nailbiter narrative only serves the awards hype machine but often the people forecasting the race socalled oscarologists can make only educated guesses the way the academy tabulates the big winner doesnt help in every other category the nominee with the most votes wins but in the best picture category voters are asked to list their top movies in preferential order if a movie gets more than percent of the firstplace votes it wins when no movie manages that the one with the fewest firstplace votes is eliminated and its votes are redistributed to the movies that garnered the eliminated ballots secondplace votes and this continues until a winner emerges it is all terribly confusing but apparently the consensus favorite comes out ahead in the end this means that endofseason awards chatter invariably involves tortured speculation about which film would most likely be voters second or third favorite and then equally tortured conclusions about which film might prevail in it was a tossup between boyhood and the eventual winner birdman in with lots of experts betting on the revenant or the big short the prize went to spotlight last year nearly all the forecasters declared la la land the presumptive winner and for two and a half minutes they were correct before an envelope snafu was revealed and the rightful winner moonlight was crowned this year awards watchers are unequally divided between three billboards outside ebbing missouri the favorite and the shape of water which is the baggers prediction with a few forecasting a hail mary win for get out but all of those films have historical oscarvoting patterns against them the shape of water has nominations more than any other film and was also named the years best by the producers and directors guilds yet it was not nominated for a screen actors guild award for best ensemble and no film has won best picture without previously landing at least the actors nomination since braveheart in this year the best ensemble sag ended up going to three billboards which is significant because actors make up the academys largest branch that film while divisive also won the best drama golden globe and the bafta but its filmmaker martin mcdonagh was not nominated for best director and apart from argo movies that land best picture without also earning best director nominations are few and far between ``` KEY = `cfmypvbrlqxwiejdsgkhnazotu` ## Task 2 Task two was very straight forward: ![](https://i.imgur.com/TTCNe4b.png) > Trying three different encryption algorithms > ![](https://i.imgur.com/u6LZf4p.png) > Given picture. Worked really well woop woop ![](https://i.imgur.com/fZ0OAc9.png) > Result of our own picture. Did not work very well *sad woop woop* ![](https://i.imgur.com/n6ftNJ3.png) > How the picture looked