Skip to content

Instantly share code, notes, and snippets.

@bchaber
Created November 7, 2024 22:01
Show Gist options
  • Save bchaber/732e6ac6f4c66934bb8559839ebf7bbe to your computer and use it in GitHub Desktop.
Save bchaber/732e6ac6f4c66934bb8559839ebf7bbe to your computer and use it in GitHub Desktop.
import os
import random
import string
ALPHABET = string.ascii_lowercase + string.digits
def encrypt(password, text):
message = "".join([c.lower() if c.isalnum() else "" for c in text])
plaintext = [ALPHABET.find(c.lower()) for c in message]
keystream = [ALPHABET.find(c.lower()) for c in password]
n = len(plaintext)
m = len(keystream)
ciphertext = [(plaintext[i] + keystream[i % m]) % len(ALPHABET) for i in range(n)]
return "".join([ALPHABET[c] for c in ciphertext])
def decrypt(password, text):
message = "".join([c.lower() if c.isalnum() else "" for c in text])
ciphertxt = [ALPHABET.find(c.lower()) for c in message]
keystream = [ALPHABET.find(c.lower()) for c in password]
n = len(ciphertxt)
m = len(keystream)
plaintext = [(ciphertxt[i] - keystream[i % m]) % len(ALPHABET) for i in range(n)]
return "".join([ALPHABET[c] for c in plaintext])
flag = "fa5ef1a9" # przykładowa flaga
text = "hello world" # przykładowa treść
key1 = "ab" # przykładowy klucz nr 1
key2 = "def" # przykładowy klucz nr 2
key3 = "uvxyz" # przykładowy klucz nr 3
text0 = text + ("pw" + "open" + flag + "close") + text
text1 = encrypt(key1, text0)
text2 = encrypt(key2, text1)
text3 = encrypt(key3, text2)
ciphertext = text3
print(decrypt(key1, decrypt(key2, decrypt(key3, ciphertext))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment