#!/usr/local/bin/python from random import getrandbits
p = 2**521 - 1 a = getrandbits(521) b = getrandbits(521) print("a =", a) print("b =", b)
try: x = int(input("enter your starting point: ")) y = int(input("alright, what's your guess? ")) except: print("?") exit(-1)
r = getrandbits(20)
for _ inrange(r): x = (x * a + b) % p
if x == y: print("wow, you are truly psychic! here, have a flag:", open("flag.txt").read()) else: print("sorry, you are not a true psychic... better luck next time")
sh.close() # b"enter your starting point: alright, what's your guess? wow, you are truly psychic! here, have a flag: corctf{r34l_psych1c5_d0nt_n33d_f1x3d_p01nt5_t0_tr1ck_th15_lcg!}\n"
hidE
1
This RSA encryption service is so secure we're not even going totell you how we encrypted it
#!/usr/local/bin/python import random import time import math import binascii from Crypto.Util.number import *
p, q = getPrime(512), getPrime(512) n = p * q phi = (p - 1) * (q - 1)
flag = open('./flag.txt').read().encode()
random.seed(int(time.time()))
defencrypt(msg): e = random.randint(1, n) while math.gcd(e, phi) != 1: e = random.randint(1, n) pt = bytes_to_long(msg) ct = pow(pt, e, n) return binascii.hexlify(long_to_bytes(ct)).decode()
defmain(): print('Secure Encryption Service') print('Your modulus is:', n) whileTrue: print('Options') print('-------') print('(1) Encrypt flag') print('(2) Encrypt message') print('(3) Quit') x = input('Choose an option: ') if x notin'123': print('Unrecognized option.') exit() elif x == '1': print('Here is your encrypted flag:', encrypt(flag)) elif x == '2': msg = input('Enter your message in hex: ') print('Here is your encrypted message:', encrypt(binascii.unhexlify(msg))) elif x == '3': print('Bye') exit()
defencrypt(msg): e = random.randint(1, n) while e % 2 == 0 : # an odd and an even number likely co-prime e = random.randint(1, n) pt = bytes_to_long(msg) ct = pow(pt, e, n) return binascii.hexlify(long_to_bytes(ct)).decode()
import time from pwn import * import random from Crypto.Util.number import * from gmpy2 import invert, gcd
defencrypt(msg): e = random.randint(1, n) while e % 2 == 0 : # an odd and an even number likely co-prime e = random.randint(1, n) pt = bytes_to_long(msg) ct = pow(pt, e, n) return binascii.hexlify(long_to_bytes(ct)).decode()
defCMA(n, c1, c2, e1, e2): defegcd(a, b): if b == 0: return a, 0 else: x, y = egcd(b, a % b) return y, x - (a // b) * y s = egcd(e1, e2) s1 = s[0] s2 = s[1]
if s1 < 0: s1 = - s1 c1 = invert(c1, n) elif s2 < 0: s2 = - s2 c2 = invert(c2, n) m = pow(c1, s1, n) * pow(c2, s2, n) % n return m
seed = int(time.time()) sh = remote("be.ax", 31124)
sh.recvuntil(b"Your modulus is: ") n = int(sh.recvline().strip()) print(f"n = {n}")
# if you run many seconds and the seed wasnt printed, you need retry this script sh.sendline(b"2") sh.sendline(b"41") # "A“ sh.recvuntil(b"Here is your encrypted message: ") cipher = sh.recvline().strip().decode() whileTrue: random.seed(seed) if encrypt(binascii.unhexlify(b"41")) == cipher: break else : seed += 1 print(f"seed = {seed}")
sh.sendline(b"1") sh.recvuntil(b"Here is your encrypted flag: ") flag1 = int(b"0x" + sh.recvline().strip(), 16) e1 = random.randint(1, n) while e1 % 2 == 0: e1 = random.randint(1, n)
whileTrue: sh.sendline(b"1") sh.recvuntil(b"Here is your encrypted flag: ") flag2 = int(b"0x" + sh.recvline().strip(), 16) e2 = random.randint(1, n) while e2 % 2 == 0: e2 = random.randint(1, n)
from Crypto.Util.number import * from Crypto.Cipher import AES from Crypto.Util.Padding import pad from hashlib import sha256 from secrets import randbelow
p = 142031099029600410074857132245225995042133907174773113428619183542435280521982827908693709967174895346639746117298434598064909317599742674575275028013832939859778024440938714958561951083471842387497181706195805000375824824688304388119038321175358608957437054475286727321806430701729130544065757189542110211847 a = randbelow(p) b = randbelow(p) s = randbelow(p)
print("p =", p) print("a =", a) print("b =", b) print("s =", s)
a_priv = randbelow(p) b_priv = randbelow(p)
deff(s): return (a * s + b) % p
defmult(s, n): for _ inrange(n): s = f(s) return s
flag = open("flag.txt", "rb").read() key = sha256(long_to_bytes(shared)).digest()[:16] iv = long_to_bytes(randint(0, 2**128)) cipher = AES.new(key, AES.MODE_CBC, iv=iv) print(iv.hex() + cipher.encrypt(pad(flag, 16)).hex())
# p = 142031099029600410074857132245225995042133907174773113428619183542435280521982827908693709967174895346639746117298434598064909317599742674575275028013832939859778024440938714958561951083471842387497181706195805000375824824688304388119038321175358608957437054475286727321806430701729130544065757189542110211847 # a = 118090659823726532118457015460393501353551257181901234830868805299366725758012165845638977878322282762929021570278435511082796994178870962500440332899721398426189888618654464380851733007647761349698218193871563040337609238025971961729401986114391957513108804134147523112841191971447906617102015540889276702905 # b = 57950149871006152434673020146375196555892205626959676251724410016184935825712508121123309360222777559827093965468965268147720027647842492655071706063669328135127202250040935414836416360350924218462798003878266563205893267635176851677889275076622582116735064397099811275094311855310291134721254402338711815917 # s = 35701581351111604654913348867007078339402691770410368133625030427202791057766853103510974089592411344065769957370802617378495161837442670157827768677411871042401500071366317439681461271483880858007469502453361706001973441902698612564888892738986839322028935932565866492285930239231621460094395437739108335763 # A = 27055699502555282613679205402426727304359886337822675232856463708560598772666004663660052528328692282077165590259495090388216629240053397041429587052611133163886938471164829537589711598253115270161090086180001501227164925199272064309777701514693535680247097233110602308486009083412543129797852747444605837628 # B = 132178320037112737009726468367471898242195923568158234871773607005424001152694338993978703689030147215843125095282272730052868843423659165019475476788785426513627877574198334376818205173785102362137159225281640301442638067549414775820844039938433118586793458501467811405967773962568614238426424346683176754273 # e0364f9f55fc27fc46f3ab1dc9db48fa482eae28750eaba12f4f76091b099b01fdb64212f66caa6f366934c3b9929bad37997b3f9d071ce3c74d3e36acb26d6efc9caa2508ed023828583a236400d64e
from gmpy2 import invert as inverse from sympy import discrete_log from hashlib import sha256 from Crypto.Util.number import * from Crypto.Cipher import AES
invert = lambda x,p: int(inverse(x,p))
p = ... a = ... b = ... s = ... A = ... B = ... cipher = "e0364f9f55fc27fc46f3ab1dc9db48fa482eae28750eaba12f4f76091b099b01fdb64212f66caa6f366934c3b9929bad37997b3f9d071ce3c74d3e36acb26d6efc9caa2508ed023828583a236400d64e"
a_k = (A*(1-a) - b)*invert(s - s*a -b, p) % p b_k = (B*(1-a) - b)*invert(s - s*a -b, p) % p a_private = discrete_log(p, a_k, a) b_private = discrete_log(p, b_k, a)