0% found this document useful (0 votes)
16 views4 pages

PART3

Uploaded by

aubakiraknur45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

PART3

Uploaded by

aubakiraknur45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

PART3

First, I tried to install the pycryptodome package with the pip command. However, I
encountered an error because the environment was externally managed. The error message
suggested using apt to install the necessary package.
To solve this, I used the following command to install pycryptodome with apt:

I successfully installed the package, and the environment was ready to proceed.
Once the environment was ready, I created a Python script to decrypt the message. In the
script, I used the pycryptodome library to try each key from the provided key list. I did this by
writing a loop that went through each key and tried to decrypt the cipher text.

Then I created the decrypt_vigenere.py file using nano:

I wrote my code inside this file. With this code, I can decrypt the keys I have written.
In code:

First, I used the from itertools import cycle statement to import the cycle function. I
needed this to repeat the characters of the key continuously as I was decrypting the ciphertext.
The cycle function allows me to reuse the key over and over if it is shorter than the ciphertext.

Next, I created a function called decrypt_vigenere, which accepts two parameters: the
ciphertext (the message that needs to be decrypted) and the key (the key used for
decryption). Inside the function, I initialized an empty list decrypted_text where I would store
each decrypted character.

I used key_cycle = cycle(key) to make sure that even if the key is shorter than the
ciphertext, the characters of the key are repeated as needed.

Then, I looped through each character in the ciphertext using a for loop. For each
character, I checked if it was an alphabetic character using char.isalpha(). If it was, I
calculated the shift value using (ord(char) - ord(next(key_cycle))) % 26. This
calculates the difference between the ciphertext character and the corresponding character
in the key. The % 26 ensures the result stays within the range of the alphabet.

After calculating the shift, I converted it back into a letter using chr(shift + ord('a')) and
appended it to the decrypted_text list. If the character wasn't alphabetic, I just added it
directly without any changes.

Once all characters were processed, I joined the decrypted_text list into a single string
using ''.join(decrypted_text) and returned the final decrypted message.

In the main part of the program, I defined the ciphertext as "yrgzxeyiskmkxer". I then entered
a while True loop that keeps asking the user to input a key until they type 'exit'. For each key,
I passed it along with the ciphertext to the decrypt_vigenere function, and the decrypted
message was printed. If the user typed 'exit', the program printed a goodbye message and
stopped.

Then I run my written code.

It prompted me to enter a key. First, before entering the possible keys, I removed all the keys
containing numbers from the 10-million-line text file because the Vigenère cipher does not
include numbers, only letters.Then, by entering the remaining possible keys, I found the hidden
key, which turned out to be "adminaaa." After decrypting, the word "yourkeyishacker"
appeared. Once I found the meaningful word, I typed "exit" to stop the program. In this way, the
task was successfully completed.

You might also like