My First Encryption
~# cat Question
I just learnt about encrypting important texts! I always saw that encrypted texts came in a bunch of random numbers and bytes. So, I decided to make my own encryption algorithm. This is so secure, the cipher text looks so random!!! Nobody can figure out my encryption!
FILE: dist.zip
cipher.txt
encrypt.py
Understanding the encrypt script
The script hashes each character of the plaintext (flag) using SHA-1
, and then uses the resulting hash values to XOR the plaintext.
Hashing: For each character in the plaintext, the script calculates the SHA-1 hash value of that character. This results in a list of hash values, one for each character in the plaintext.
Key Generation: It then selects the first four hash values from the list (
key = [hashy[i] for i in range(4)]
) to use as the XOR keys.XOR Encryption: It then iterates through all the hash values (
hashy
list) and XORs each hash value with one of the selected keys. This XOR operation changes the value of each hash, encrypting the plaintext.
Decrypting the cipher text
Reading the File: The script reads the contents of
cipher.txt
into theraw
variable.Extracting Encrypted Hashes: It then processes the
raw
data byte by byte to extract the encrypted hash values. It ignores any newline characters (\r
) and groups consecutive non-newline bytes together, assuming they form an encrypted hash.Key Generation: It selects the first four extracted hash values (
key = [raw2[i] for i in range(4)]
) to use as keys for XOR decryption.XOR Decryption: It iterates through all the extracted hash values (
raw2
list) and XORs each hash value with one of the selected keys. This XOR operation should result in the original SHA-1 hash values.Hash Value Decoding: For each decrypted hash value, it iterates through all possible ASCII characters (0-255) and calculates the SHA-1 hash of each character. If the calculated hash matches the decrypted hash value, we can assume that the character was the original plaintext character used to generate the hash.
Reconstructing Plaintext: It constructs the original plaintext by concatenating all the identified characters together.
In short, the script extracts the encrypted hash values, decrypts them using the provided keys, and converts the decrypted hash values back to the original plaintext.
Flag: flag{h45h_&_x0r_1s_nO7_S3cuRE}
Last updated