This doth be a machine-wrought text which may contain errors!
Om kryptografioppgavene
These tasks be fashioned somewhat differently than the others upon Piggy, methinks ‘tis curious what ye do prefer! 😎
First shall come a portion of information concerning the themes, thereafter will follow some tasks anon!
The “levels” in this task be not wholly as those before, for here are things more divided into themes.
Hva er et Monoalfabetisk Chiffer?
Et monoalfabetisk chiffer er en type substitusjonschiffer hvor hver bokstav i klarteksten systematisk erstattes med en annen bokstav, tall eller symbol. Nøkkelen til chifferet er en mapping mellom klartekstens alfabet og chiffertekstens alfabet. For eksempel, hvis vi har et alfabet på 26 bokstaver, kan vi definere en nøkkel som erstatter ‘A’ med ‘D’, ‘B’ med ‘E’, ‘C’ med ‘F’, og så videre.
Eksempel
La oss si at vi har følgende nøkkel:
>
>
A -> Q
B -> W
C -> E
D -> R
E -> T
F -> Y
G -> U
H -> I
I -> O
J -> P
K -> A
L -> S
M -> D
N -> F
O -> G
P -> H
Q -> J
R -> K
S -> L
T -> Z
U -> X
V -> C
W -> V
X -> B
Y -> N
Z -> M
Hvis vi ønsker å kryptere ordet “HELL”, vil vi erstatte hver bokstav i henhold til nøkkelen:
- H -> I
- E -> T
- L -> S
- L -> S
Dermed vil “HELL” bli kryptert til “ITSS”.
Dekryptering
For å dekryptere en chiffertekst, må vi reversere prosessen. Vi bruker nøkkelen til å finne den opprinnelige bokstaven for hver bokstav i chifferteksten. I eksemplet ovenfor, for å dekryptere “ITSS”, vil vi:
- I -> H
- T -> E
- S -> L
- S -> L
Dermed vil “ITSS” bli dekryptert til “HELL”.
Frekvensanalyse
En enkel måte å bryte et monoalfabetisk chiffer på er ved å bruke frekvensanalyse. I de fleste språk forekommer visse bokstaver oftere enn andre. For eksempel er ‘E’ den vanligste bokstaven i engelsk. Ved å telle frekvensen av hver bokstav i chifferteksten, kan vi sammenligne den med den forventede frekvensen i klarteksten. Dette kan gi oss hint om hvilke bokstaver som er blitt erstattet med hvilke.
Viktige Bokstaver og Deres Frekvens i Engelsk
| Bokstav | Frekvens (%) |
|---|---|
| E | 12.70 |
| T | 9.06 |
| A | 8.17 |
| O | 7.51 |
| I | 6.97 |
| N | 6.75 |
| S | 6.33 |
| H | 6.09 |
| R | 6.02 |
| D | 4.25 |
| L | 4.03 |
| C | 2.78 |
| U | 2.76 |
| M | 2.43 |
| W | 2.36 |
| F | 2.23 |
| G | 2.02 |
| P | 1.93 |
| Y | 1.90 |
| B | 1.49 |
| V | 0.98 |
| K | 0.77 |
| X | 0.15 |
| J | 0.15 |
| Q | 0.10 |
| Z | 0.07 |
Oppgaver
- Krypter ordet “KRYPTERING” ved hjelp av nøkkelen gitt ovenfor.
- Dekrypter ordet “ITSSLE” ved hjelp av nøkkelen gitt ovenfor.
- Gitt en chiffertekst, prøv å dekryptere den ved hjelp av frekvensanalyse.
Part 1 - The Encrypting and Decrypting of Monoalphabetic Ciphers
What is a Monoalphabetic Cipher?
A monoalphabetic cipher is a type of substitution cipher wherein each letter in the plaintext is systematically replaced with another letter, number, or symbol. The key to the cipher is a mapping between the plaintext alphabet and the ciphertext alphabet. For example, if we have an alphabet of 26 letters, we can define a key that replaces ‘A’ with ‘D’, ‘B’ with ‘E’, ‘C’ with ‘F’, and so forth.
Example
Let us say that we have the following key:
>
>
A -> Q
B -> W
C -> E
D -> R
E -> T
F -> Y
G -> U
H -> I
I -> O
J -> P
K -> A
L -> S
M -> D
N -> F
O -> G
P -> H
Q -> J
R -> K
S -> L
T -> Z
U -> X
V -> C
W -> V
X -> B
Y -> N
Z -> M
If we wish to encrypt the word “HELL”, we shall replace each letter according to the key:
- H -> I
- E -> T
- L -> S
- L -> S
Thus, “HELL” shall be encrypted to “ITSS”.
Decryption
To decrypt a ciphertext, we must reverse the process. We use the key to find the original letter for each letter in the ciphertext. In the example above, to decrypt “ITSS”, we shall:
- I -> H
- T -> E
- S -> L
- S -> L
Thus, “ITSS” shall be decrypted to “HELL”.
Frequency Analysis
A simple way to break a monoalphabetic cipher is by using frequency analysis. In most languages, certain letters occur more often than others. For example, ‘E’ is the most common letter in English. By counting the frequency of each letter in the ciphertext, we can compare it with the expected frequency in the plaintext. This can give us hints as to which letters have been replaced with which.
Important Letters and Their Frequency in English
| Letter | Frequency (%) |
|---|---|
| E | 12.70 |
| T | 9.06 |
| A | 8.17 |
| O | 7.51 |
| I | 6.97 |
| N | 6.75 |
| S | 6.33 |
| H | 6.09 |
| R | 6.02 |
| D | 4.25 |
| L | 4.03 |
| C | 2.78 |
| U | 2.76 |
| M | 2.43 |
| W | 2.36 |
| F | 2.23 |
| G | 2.02 |
| P | 1.93 |
| Y | 1.90 |
| B | 1.49 |
| V | 0.98 |
| K | 0.77 |
| X | 0.15 |
| J | 0.15 |
| Q | 0.10 |
| Z | 0.07 |
Tasks
- Encrypt the word “KRYPTERING” using the key given above.
- Decrypt the word “ITSSLE” using the key given above.
- Given a ciphertext, attempt to decrypt it using frequency analysis.
What is a “Cipher”?
Hast thou e’er desired to pen a secret missive unto a friend, so that none others may comprehend it? Then needest thou a Cipher, or chiffer in the Norwegian tongue! A Cipher is, in sooth, a method which doth transform common text into “code” by exchanging characters (oft letters) with other characters. The result doth appear as nonsense to those who know not how the code doth function. The point is that only they who know the key (the rule for exchanging the letters) may render the code intelligible once more. In other words: Ciphers do make secret messages possible, be it the play of childhood with secret tongues, or true spies sending encrypted tidings. 😄
Visste du?
Knowest thou this? The word “cipher” doth in sooth spring from an Arabic tongue: the word sifr, which signifieth “naught.” Perchance for that the secret code did resemble nothing (no meaning!) when folk could not unravel it!
There doth exist many a cipher – some employ numbers, some symbols, and modern data encryption doth make use of most intricate algorithms. These complex algorithms do require most complex mathematics, thus may we gaze upon some simpler algorithms first!
Monoalphabetic Ciphers
Let us first behold some of the simplest (and most ancient) methods of coding that do exist: monoalphabetic ciphers.
Monoalphabetic doth perchance sound as a difficult word, yet may we divide it thus: mono doth signify “one,” and alphabetic doth concern the alphabet.
Thus, monoalphabetic ciphers be codes wherein one single “cipher alphabet” is employed for the whole message. That is to say, each letter in the original text is ever exchanged for the selfsame letter throughout the entire encrypted message.
For example, shouldst thou have determined that A shall be exchanged with X, then shall all A’s in the text be transformed into X.
Cæsar-Chiffer
The most classic example of a monoalphabetic cipher is the Cæsar-Chiffer (named for Julius Cæsar). ‘Tis, in sooth, a rule to “shift” all letters a certain number of places along the alphabet. ‘Tis said that Cæsar himself did employ a shift of 3 letters in his secret missives. It doth function such that A becometh D, B becometh E, C becometh F, and so forth through the alphabet. (When one passeth beyond Z, one doth begin anew at A.) A message which were ABC would thus become DEF, should we employ Cæsar’s method.
How the Cæsar Cipher Doth Work in Practice:
- Choose a Key: Determine a secret number (for example 3) which doth signify how many places each letter shall be shifted.
- Substitute Each Letter: For each letter in the original message, find the letter that lieth so many places after it in the alphabet (for key 3, A becometh D, B to E, and so forth – remember to return to A again after Z if need be). Perchance thou mayest also include Æ, Ø and Å, but this shall become somewhat more complicated.
- Encrypted Message: Replace the letters and write the new message with the “shifted” letters. Lo! – thou hast an unreadable, secret text which only those with the key can understand!
- To decrypt (that is, to render it into legible text again) one doth simply perform the opposite shift back. If thou knowest the key (e.g. 3), ‘tis as easy to read the message by shifting the letters 3 back in the alphabet.
Sikkerhet?
These codes are not overmuch secure in the long run. Forasmuch as the pattern (the substitution) is fixed, a person with sufficient patience or some cunning tricks may swiftly reveal the secret. For example, there are but a few possible shifts in the Caesar cipher, as many as the alphabet, so any man may try them all until the message doth make sense – or employ letter frequencies to guess his way forward. In other words, perchance do not use the Caesar cipher for most secret diary notes or state secrets 😉.
Monoalphabetic ciphers be a wondrous means to learn the principle behind encryption. Simple they are, and do demonstrate how a single rule (a key) may transform a text most understandable into something mystical and obscure – and back again. Thus, shouldst thou desire to send a friend a secret message, thou mayest employ the Caesar cipher! Perchance ye might devise your own variant of Caesar’s secret alphabet? 🔐✨
Tasks
Programming Tongues?
As erstwhiles, employ ye any programming tongue that doth please thee! The examples here shall be in Python.
Task 1.1 - Cæsar Cipher Encryption
Now shall we, in sooth, indite some code! We shall begin with simplicity, crafting the encryption, which, based upon the theory, should prove quite straightforward.
Implement the encryption with the Cæsar Cipher, by means of a function that doth receive text and a number which is the “key,” that is to say, how much the alphabet shall be rotated.
Tips til framgangsmåte.
- Create a function which doth bear the name of
caesar, that taketh as input the text to be encrypted, and a “shift”, that is to say, how many places in the alphabet the text shall be shifted. - Go through letter by letter in the text.
- We shall not “shift” other characters than letters: discover how to check if a character in the text is a letter.
- We shall “rotate” the letter by n places, that is to say, we must add the rotation: discover how to turn the text into numbers so that thou mayest add the shift. Hint: The
ord()function. - Hark! Here thou shalt receive diverse values based on whether thou hast small or large letters. Refer to ASCII Tabellen.
- After thou hast a value, it is as simple as adding the n value. But what doth happen if thou art at the end of the alphabet? We shall but have nonsense after the letter Z. How is this to be fixed? This requireth some thinking.
Fikse enkrypteringen.
To fix the encryption wholly requireth some thinking.
- The first step to ponder is the use of the modulus operator,
%. - Since the alphabet (in English) consisteth of 26 letters, we may take the modulus with
26. - But this doth not work wholly, seest thou the reason?
- Try to
printout the value of a character withord(), what dost thou receive? - For
athou shalt receive 97. If thou takest the modulus 26 with this, thou shalt receive 19. Remember that the modulus shall always give an answer between 0 and the number. - This may be fixed by storing the starting value of large and small letters, subtracting this from the letter, and then taking the modulus. Thus it becometh:
(ord(letter) - ord('a')) % 26 - To receive the right letter again, thou merely addeth the starting value again.
- After all this, thou mayest finally turn the number into a letter again. Here thou mayest use the
chr()function. - Now thou mayest finally add the letter to a result and return the encrypted text!
Løsning:
def caesar_cipher(text, shift):
result = ""
for char in text:
if char.isalpha():
# finn ut startpunktet basert på store og små bokstaver
start = ord('A') if char.isupper() else ord('a')
# Den vanskelige shifte-utregningen
result += chr((ord(char) - start + shift) % 26 + start)
else:
result += char
return result
Thus doth one resolve the riddle:
def caesar_cipher(text, shift): result = "" for char in text: if char.isalpha(): # finn ut startpunktet basert på store og små bokstaver start = ord('A') if char.isupper() else ord('a') # Den vanskelige shifte-utregningen result += chr((ord(char) - start + shift) % 26 + start) else: result += char return result
Task 1.2 - Cæsar Cipher Decryption
Decryption doth but reverse the reckoning of encryption. Thou dost subtract the offset, instead of adding thereto.
Tips til framgangsmåte.
Employ the function thou didst create in task one for this purpose. Simply take the selfsame function, but in reverse. This mayst thou achieve by shifting it about by 26 - shift.
Løsning:
def caesar_decrypt(text, shift):
return caesar_cipher(text, 26 - shift)
Verily, the remedy doth lie thus:
def caesar_decrypt(text, shift):
return caesar_cipher(text, 26 - shift)
Other Monoalphabetic Ciphers (ex. Atbash)
There do exist other monoalphabetic ciphers also! One of the simpler ones is that which is called the “Atbash” Cipher.
How Doth Atbash Work?
This is most simple, for instead of a rotation, letters are mapped unto the converse alphabet. Herein lieth a table to show the mapping:
| a | b | c | d | e | f | g | h | i | j | k | l | m | n | o | p | q | r | s | t | u | v | w | x | y | z |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| z | y | x | w | v | u | t | s | r | q | p | o | n | m | l | k | j | i | h | g | f | e | d | c | b | a |
Task 1.3 - Atbash Cipher Encryption and Decryption
’Tis a boon of the Atbash, that forasmuch as the encryption be a one-to-one transformation, it doth function directly in reverse. That is to say, shouldst thou have wrought the encryption, thou hast likewise, by the same token, fashioned the decryption.
How May This Be Done in Practice?
Thou mayest either subtract the letter in relation to Z, or fashion a “Look-up” table. That is to say, a table or dictionary which doth contain all the letters from a to z and what they shall become. This may be a good solution if ye wouldst create another sort of encryption.
Lookup-table implementasjon.
letters = {
'a': 'z'
'b': 'y'
'c': 'x'
'd': 'w'
# ... legg til resten av bokstavene nedover
}
Verily, a table of correspondence, ‘tis here displayed.
letters = { 'a': 'z' 'b': 'y' 'c': 'x' 'd': 'w' # ... legg til resten av bokstavene nedover }
By means of this table, thou mayest peruse letter by letter, then fetch the value per letter from the lookup table, and thereafter set it forth. What must thou do for great and small letters?
Part 2 - The Cryptanalysis of Monoalphabetic Ciphers
In this section, ye shall endeavour to craft an algorithm to “crack” a Caesar cipher, that is to say, to take an encrypted text, and thenceforth, to derive the original text without knowledge of the key.
This may be done somewhat manually, or thou mayest attempt to employ simple “cryptanalysis.” This is a concept which we shall examine more deeply anon, but for now, we shall but observe one of the simplest ways: Frequency Analysis. Thou canst read more of this concept here: Frequency Analysis or here Wikipedia - frequency analysis.
This method may be used in more than merely Caesar ciphers; it may be used in more complicated algorithms also, but the Caesar cipher is so simple that frequency analysis is trivial.
How doth frequency analysis function?
Frequency analysis, as the name doth hint, is a manner of checking the frequency of letters within a text. Wherefore may this be useful? Imagine thou hast a long text, let us conceive an English text, drawn from Wikipedia - frequency analysis:
In cryptanalysis, frequency analysis is the study of the frequency of letters or groups of letters in a ciphertext. The method is used as an aid to breaking classical ciphers.
Frequency analysis is based on the fact that, in any given stretch of written language, certain letters and combinations of letters occur with varying frequencies. Moreover, there is a characteristic distribution of letters that is roughly the same for almost all samples of that language. For instance, given a section of English language, E, T, A and O are the most common, while Z, Q, X and J are rare. Likewise, TH, ER, ON, and AN are the most common pairs of letters termed bigrams or digraphs), and SS, EE, TT, and FF are the most common repeats. The nonsense phrase ETAOIN SHRDLU represents the 12 most frequent letters in typical English language text.
In some ciphers, such properties of the natural language plaintext are preserved in the ciphertext, and these patterns have the potential to be exploited in a ciphertext-only attack.
Should we transform this text by the Caesar cipher (and remove commas, spaces, and other special characters), we shall obtain the following cipher-text:
xcrgneipcpanhxhugtfjtcrnpcpanhxhxhiwthijsnduiwtugtfjtcrnduatiitghdgvgdjehduatiitghxcprxewtgitmiiwtbtiwdsxhjhtsphpcpxsidqgtpzxcvraphhxrparxewtghugtfjtcrnpcpanhxhxhqphtsdciwtupriiwpixcpcnvxktchigtirwdulgxiitcapcvjpvtrtgipxcatiitghpcsrdbqxcpixdchduatiitghdrrjglxiwkpgnxcvugtfjtcrxthbdgtdktgiwtgtxhprwpgpritgxhixrsxhigxqjixdcduatiitghiwpixhgdjvwaniwthpbtudgpabdhipaahpbeathduiwpiapcvjpvtudgxchipcrtvxktcphtrixdcdutcvaxhwapcvjpvttippcsdpgtiwtbdhirdbbdclwxatofmpcsypgtgpgtaxztlxhtiwtgdcpcspcpgtiwtbdhirdbbdcepxghduatiitghitgbtsqxvgpbhdgsxvgpewhpcshhttiipcsuupgtiwtbdhirdbbdcgtetpihiwtcdchtchtewgphttipdxchwgsajgtegthtcihiwtbdhiugtfjtciatiitghxcinexrpatcvaxhwapcvjpvtitmixchdbtrxewtghhjrwegdetgixthduiwtcpijgpaapcvjpvteapxcitmipgtegthtgktsxciwtrxewtgitmipcsiwthtepiitgchwpktiwteditcixpaidqttmeadxitsxcprxewtgitmidcanpiiprz
This text doth seem impossible to “crack”, yet with the aid of “Frequency Analysis” it is not only possible, but easy.
This is a figure which doth show the distribution of letters in English. As we may see, the letter E is the most frequent letter, followed by T, A, and O.
This may be transformed into a table and then used to count and analyse a given cipher-text, so as to “crack” it. In the tasks below, thou shalt create a program which can “crack” the Caesar cipher on its own. ‘Tis true that the Caesar cipher is so simple that thou canst merely check all 26 possibilities manually, but here we shall discover the solution, wholly automatically.
Task 2.1 - To Craft a Frequency Table
In a Python scroll, do thou create a frequency table of the letters within the English tongue. Thou mayest seek this out for thyself, yet shouldst thou not incline to such labour, we shall understand it well!
Shouldst thou, in sooth, desire to find it thyself, thou mayest proceed as in Task 2.2, but upon a text of exceeding great length.
English Letter Frequency (Svaret)
english_letter_frequency = {
'E': 12.70,
'T': 9.06,
'A': 8.17,
'O': 7.51,
'I': 6.97,
'N': 6.75,
'S': 6.33,
'H': 6.09,
'R': 5.99,
'D': 4.25,
'L': 4.03,
'C': 2.78,
'U': 2.76,
'M': 2.41,
'W': 2.36,
'F': 2.23,
'G': 2.02,
'Y': 1.97,
'P': 1.93,
'B': 1.29,
'V': 0.98,
'K': 0.77,
'J': 0.15,
'X': 0.15,
'Q': 0.10,
'Z': 0.07
}
Lo, a reckoning of the English letters, and how oft each doth appear in the tongue of men. ‘Tis a most curious thing to behold, and may serve thee well in thy endeavours. Observe how ‘E’ doth lead the dance, and ‘Z’ doth bring up the rear, a humble follower indeed. These numbers, good sir, are the keys to unlocking the secrets of the written word.
Task 2.2 - To Count the Frequency of Letters in Text
Now shall we devise an algorithm which doth find the frequency of letters within a given text.
Tips til framgangsmåte
- Begin with a function that doth receive a text (it may be whatso doth please).
- Within the function, create a “dictionary” (Python Dictionaries), with entries for each letter of the alphabet, set to
0. ({'A' = 0, 'B' = 0, 'C' = 0, ..., 'Z' = 0}) - Traverse the whole of the text and count each and every letter (increase by one in the corresponding entry in the dictionary). Here shouldst thou likely ignore characters that are not letters, and remember also of both upper and lower case.
- Keep track of how many letters have been counted in total.
- When thou art finished with the counting, divide
/each value in the table by the length of the text and then multiply by 100, this shall give thee a percent frequency. (Thou mayest of course rather let thy table above be between 0 and 1). - Now shouldst thou have a frequency table for the text.
Task 2.3 - To Compare the Frequency of a Text with the True Frequency
When thou hast found the frequency of all letters within the text, thou mayest craft a function which doth find the “distance.” What say ye? What is meant thereby?
Thou canst envision that the frequency of, for example, E within the text shall be a number. Thou mayest find the “distance” this hath with the actual frequency, which is 12.70. Example: The frequency is 9.63, what is the distance? The distance shall be the absolute value (negative numbers become positive) betwixt these two values: \(12.70 - 9.63 = 3.07\).
Craft a function which doth pass through each of the letters and find the distance. Thereafter, add all distances together to a “total” distance.
Mattefunksjon?
An if thou dost enquire how the mathematical function for this doth present itself, ‘tis thusly fashioned:
\(\sum_{n=0}^{N} \lvert a - b\rvert\)
Tips til framgangsmåte
- Employ a
for-loop to traverse the entirety of the frequency table. - For each letter within the frequency table, discover the absolute value when compared with the true frequency. Make use of the
abs()function in Python for this purpose. - Sum all the values, and thou shalt obtain a final result.
Task 2.4 - To Unravel Caesar’s Cipher
Now shall we at last conjoin all that we have wrought hitherto! Now shall we “crack” a Caesar’s cipher.
Fashion a program which doth “crack” a Caesar’s cipher! Without the influence of the user, thou shalt be able to cast in an encrypted text and retrieve the decrypted text without need of a key.
Test data
Lo, here doth lie some test-data for thee to employ, pray tell, what sayest thou of these?
| Test-data |
|---|
cqrbvnbbjpnrbjenahbnlancxwnqxynoduuhhxdjanjkuncxmnlxmnrclxvyuncnuhjwmqnanjanbxvnfxamboaxvxdaojexarcnsnmrqnuuxcqnanrcbxenajwjtrwrqjencqnqrpqpaxdwmhxdfnanarpqccqnwnpxcrjcrxwbfnanbqxac |
lsaizivxlmwqiwwekimwuymxiwlsvxwsmxqmklxrsxasvoewibtigxihlsaizivmjmxhsiwksshnsf |
bmtxymjwjsfdfsxbjwrjxyfsifsizsktqidtzwxjqkqtslqnajymjpnslgfwsfwitmjdtzhtrjrtxyhfwjkzqqdzutsdtzwmtzwynxstbxywzhpybjqajljyymjjytgjikwfshnxhtktwymnxwjqnjkrzhmymfspxynxgnyyjwhtqifsinfrxnhpfymjfwymfajdtzmfivznjylzfwistyfrtzxjxynwwnslbjqqlttisnlmynkdtzitrjjymtwfyntfsirfwhjqqzxymjwnafqxtkrdbfyhmgniymjrrfpjmfxyj |
zwkyvivrivrepzuzfkjzekyviffdnzcckyvpgcvrjvjkreulgjrzukyvjritrjkztkvrtyvirwkvircfexjzcvetvfevwivjydreifjvkfyzjwvvkefnkyvedzjkvinypufpfltfejzuvipflijvcwrezuzfkzehlzivukyvkvrtyvinzkyrjevvinvccrtklrccpzufekjrzukyvjkluvekslkzyrkvkfjvvpfljkreuzexlgkyvivrccsppflijvcw |
uwwilxchaniuffehiqhfuqmizupcuncihnbylycmhiqusuvyymbiofxvyuvfynizfscnmqchamulyniimguffniayncnmzunfcnnfyvixsizznbyaliohxnbyvyyizwiolmyzfcymuhsqusvywuomyvyymxihnwulyqbunboguhmnbchecmcgjimmcvfysyffiqvfuwesyffiqvfuwesyffiqvfuwesyffiqvfuweiibvfuweuhxsyffiqfynmmbueycnojufcnnfyvullsvlyuezumncmlyuxswigcha |
Tips til framgangsmåte
- Begin with the crafting of a function that doth receive text as input.
- Employ the decryption function for the Caesar cipher with rotation N upon the text, N commencing at 0.
- Create a frequency table of the result.
- Discover the distance of the result in relation to the actual frequency table.
- Either: a) keep track of the distance in a list, or b) keep track of the least value and the rotation (this shall be the key).
- Increase the rotation by 1 and repeat steps 2 to 6 until N doth reach 26 (full rotation).
- Return the decrypted text, for the least distance is the rightful key.

