write-ups-challenges-2024-2025/budapest/encrypt_image.py
2024-11-25 22:28:19 +01:00

27 lines
728 B
Python

import hashlib
def encrypt_image():
print("Encrypting image...")
in_path = "budapest_flag.jpg"
out_path = "budapest_encrypted.jpg"
key_input = input("Enter the key to encrypt the image: ")
key_hash = hashlib.sha1(key_input.encode()).hexdigest()
key_file = open("key.txt", "wb")
key_file.write(key_hash.encode())
key = [(ord(c)) for c in key_input]
file = open(in_path, 'rb')
img = file.read()
file.close()
img_bytes = bytearray(img)
for index, values in enumerate(img_bytes):
img_bytes[index] = values ^ key[index % len(key)]
img = open(out_path, 'wb')
img.write(bytes(img_bytes))
img.close()
print("Image encrypted successfully")
encrypt_image()