Skip to content

Instantly share code, notes, and snippets.

@quandqn
Last active July 10, 2017 08:43
Show Gist options
  • Save quandqn/e7d874857b0e31c9a55274fa02d5c944 to your computer and use it in GitHub Desktop.
Save quandqn/e7d874857b0e31c9a55274fa02d5c944 to your computer and use it in GitHub Desktop.
from Crypto.Util.number import *
import random
from flag import FLAG
def byte_to_binary(st):
b = []
for char in st:
b+= [int(bit) for bit in list(bin(ord(char))[2:].zfill(8))]
return b
def sequence(n):
r = [random.randint(1, 2*n)]
for i in range(1, n):
r.append(random.randint(2 * r[i-1], 4 * r[i-1]))
return r
def generate(n):
r = sequence(n)
B = random.randint(4 * r[-1], 8 * r[-1])
A = random.randint(1, B)
while not GCD(A, B) != 1:
A = random.randint(1, B)
M = [A * ri % B for ri in r]
open("pubkey.txt", "w").write(str(M))
return M
def encrypt(msg, pubkey):
ciphertext = 0
for i in range(len(msg)):
ciphertext+= msg[i] * pubkey[i]
open("enc.txt", "w").write(str(ciphertext))
return ciphertext
FLAG = byte_to_binary(FLAG)
encrypt(FLAG, generate(len(FLAG)))
#Submit your flag as `MeePwnCTF{` + FLAG + `}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment