from Crypto.Util.number import bytes_to_long from secret import flag import os
n = 6249734963373034215610144758924910630356277447014258270888329547267471837899275103421406467763122499270790512099702898939814547982931674247240623063334781529511973585977522269522704997379194673181703247780179146749499072297334876619475914747479522310651303344623434565831770309615574478274456549054332451773452773119453059618433160299319070430295124113199473337940505806777950838270849 e = 641747 m = bytes_to_long(flag)
flag = flag + os.urandom(n.bit_length() // 8 - len(flag) - 1) m = bytes_to_long(flag)
p = 91027438112295439314606669837102361953591324472804851543344131406676387779969 n = 6249734963373034215610144758924910630356277447014258270888329547267471837899275103421406467763122499270790512099702898939814547982931674247240623063334781529511973585977522269522704997379194673181703247780179146749499072297334876619475914747479522310651303344623434565831770309615574478274456549054332451773452773119453059618433160299319070430295124113199473337940505806777950838270849 e = 641747 c = 730024611795626517480532940587152891926416120514706825368440230330259913837764632826884065065554839415540061752397144140563698277864414584568812699048873820551131185796851863064509294123861487954267708318027370912496252338232193619491860340395824180108335802813022066531232025997349683725357024257420090981323217296019482516072036780365510855555146547481407283231721904830868033930943
res = Zmod(p^5)(c).nth_root(e, all=True) for i in res: temp = long_to_bytes(int(i)) if(b"flag"in temp): print(temp)
p = 0xf6e82946a9e7657cebcd14018a314a33c48b80552169f3069923d49c301f8dbfc6a1ca82902fc99a9e8aff92cef927e8695baeba694ad79b309af3b6a190514cb6bfa98bbda651f9dc8f80d8490a47e8b7b22ba32dd5f24fd7ee058b4f6659726b9ac50c8a7f97c3c4a48f830bc2767a15c16fe28a9b9f4ca3949ab6eb2e53c3 g = 5
dic = {} for i in product(table, repeat=pre_len): i = "".join(i) temp = bytes_to_long(i.encode()) tempc = c2 * invert(powmod(base_h_num,temp,p),p) % p dic[tempc] = i for j in product(table, repeat=suf_len): j = "".join(j) temp = bytes_to_long(j.encode()) tempc = powmod(base_l_num,temp,p) if(tempc in dic.keys()): print(dic[tempc]) print(j) exit()
p = 173383907346370188246634353442514171630882212643019826706575120637048836061602034776136960080336351252616860522273644431927909101923807914940397420063587913080793842100264484222211278105783220210128152062330954876427406484701993115395306434064667136148361558851998019806319799444970703714594938822660931343299 g = 5 c = 105956730578629949992232286714779776923846577007389446302378719229216496867835280661431342821159505656015790792811649783966417989318584221840008436316642333656736724414761508478750342102083967959048112859470526771487533503436337125728018422740023680376681927932966058904269005466550073181194896860353202252854
defcomplex_pow(c, exp, modulus): result = Complex(1, 0) while exp > 0: if exp & 1: result = complex_mult(result, c, modulus) c = complex_mult(c, c, modulus) exp >>= 1 return result
defrand_prime(nbits, kbits, share): whileTrue: p = (getrandbits(nbits) << kbits) + share if p % 4 == 3and isPrime(p): return p
defgen(): whileTrue: k = getrandbits(100) pp = getrandbits(400) << 100 qq = getrandbits(400) << 100 p = pp + k q = qq + k if isPrime(p) and isPrime(q): break if q > p: p, q = q, p
n = p * q lb = int(n ** 0.675) ub = int(n ** 0.70) d = randrange(lb, ub) e = inverse(d, (p * p - 1) * (q * q - 1)) sk = (p, q, d) pk = (n, e) return pk, sk
pk, sk = gen() n, e = pk withopen("flag.txt", "rb")as f: flag = f.read()
m = Complex(bytes_to_long(flag[:len(flag)//2]), bytes_to_long(flag[len(flag)//2:])) c = complex_pow(m, e, n) print(n) print(e) print(c)
from Pwn4Sage.pwn import * import itertools from gmpy2 import iroot from Crypto.Util.number import * from collections import namedtuple from tqdm import *
defcomplex_pow(c, exp, modulus): result = Complex(1, 0) while exp > 0: if exp & 1: result = complex_mult(result, c, modulus) c = complex_mult(c, c, modulus) exp >>= 1 return result defsmall_roots(e, m, t, X, Y,a1,a2,a3): PR = PolynomialRing(QQ, 'x,y', 2, order='lex') x, y = PR.gens() f = x*y^2 + a1*x*y + a2*x + a3
G_polys = [] for k inrange(m + 1): for i_1 inrange(k, m+1): for i_2 in [2*k, 2*k + 1]: G_polys.append(x**(i_1-k) * y**(i_2-2*k) * f**k * e**(m-k))
H_polys = [] for k inrange(m + 1): for i_2 inrange(2*k+2, 2*k+t+1): H_polys.append(y**(i_2-2*k) * f**k * e**(m-k))
polys = G_polys + H_polys monomials = [] for poly in polys: monomials.append(poly.lm()) dims1 = len(polys) dims2 = len(monomials) MM = matrix(QQ, dims1, dims2) for idx, poly inenumerate(polys): for idx_, monomial inenumerate(monomials): if monomial in poly.monomials(): MM[idx, idx_] = poly.monomial_coefficient(monomial) * monomial(X, Y) B = MM.LLL()
found_polynomials = False
for pol1_idx inrange(B.nrows()): for pol2_idx inrange(pol1_idx + 1, B.nrows()): P = PolynomialRing(QQ, 'a,b', 2) a, b = P.gens() pol1 = pol2 = 0 for idx_, monomial inenumerate(monomials): pol1 += monomial(a,b) * B[pol1_idx, idx_] / monomial(X, Y) pol2 += monomial(a,b) * B[pol2_idx, idx_] / monomial(X, Y)
# resultant rr = pol1.resultant(pol2) # are these good polynomials? if rr.is_zero() or rr.monomials() == [1]: continue else: print(f"found them, using vectors {pol1_idx}, {pol2_idx}") found_polynomials = True break if found_polynomials: break
ifnot found_polynomials: print("no independant vectors could be found. This should very rarely happen...")
#get data r = remote("node4.anna.nssctf.cn",28334) n = int(r.recvline().strip().decode()) e = int(r.recvline().strip().decode()) c = eval(r.recvline().strip().decode())
from Crypto.Util.number import * from Crypto.Cipher import AES from secrets import flag from math import ceil from binascii import unhexlify from os import urandom
defgenkey(bits): e = 65537 p = getPrime(bits) q = getPrime(bits) if p > q: p, q = q, p d = inverse(e, (p - 1) * (q - 1)) n = p * q u = inverse(q, p) dp = d % (p - 1) dq = d % (q - 1) sk = (n, e, d, p, q, dp, dq, u) pk = (n, e) return sk, pk
defrsa_encrypt(m, pk): n, e = pk m_padded = rsa_pad(m, ceil(n.bit_length() / 8)) c = pow(bytes_to_long(m_padded), e, n) return long_to_bytes(c)
defrsa_decrypt(c, sk): c = bytes_to_long(c) n, e, d, p, q, dp, dq, u = sk mp = pow(c, dp, p) mq = pow(c, dq, q) t = (mp - mq) % p h = (u * t) % p m = (h * q + mq) % n m = long_to_bytes(m) m = m.rjust(ceil(n.bit_length() / 8), b"\x00") return m[2:]
defencrypt_sk(sk, kM): n, e, d, p, q, dp, dq, u = sk lp = long_to_bytes(ceil(p.bit_length() / 8) * 8) lq = long_to_bytes(ceil(q.bit_length() / 8) * 8) ld = long_to_bytes(ceil(d.bit_length() / 8) * 8) lu = long_to_bytes(ceil(u.bit_length() / 8) * 8) s = lq + long_to_bytes(q) + lp + long_to_bytes(p) + ld + long_to_bytes(d) + lu + long_to_bytes(u) s = pad(s) return aes_encrypt(s, kM)
defrsa_decrypt(c, sk): c = bytes_to_long(c) n, e, d, p, q, dp, dq, u = sk mp = pow(c, dp, p) mq = pow(c, dq, q) t = (mp - mq) % p h = (u * t) % p m = (h * q + mq) % n m = long_to_bytes(m) m = m.rjust(ceil(n.bit_length() / 8), b"\x00") return m[2:]
defrsa_decrypt(c, sk): c = bytes_to_long(c) n, e, d, p, q, dp, dq, u = sk mp = pow(c, dp, p) mq = pow(c, dq, q) t = (mp - mq) % p h = (u * t) % p m = (h * q + mq) % n m = long_to_bytes(m) m = m.rjust(ceil(n.bit_length() / 8), b"\x00") return m[2:]
defgenkey(bits): e = 65537 p = getPrime(bits) q = getPrime(bits) if p > q: p, q = q, p d = inverse(e, (p - 1) * (q - 1)) n = p * q u = inverse(q, p) dp = d % (p - 1) dq = d % (q - 1) sk = (n, e, d, p, q, dp, dq, u) pk = (n, e) return sk, pk
defrsa_decrypt(c, sk): n, e, d, p, q, dp, dq, u = sk mp = pow(c, dp, p) mq = pow(c, dq, q) t = (mp - mq) % p h = (u * t) % p m = (h * q + mq) % n return m
sk, pk = genkey(1024) n, e, d, p, q, dp, dq, u = sk
p = getPrime(1024) dp = d %(p-1) sk = (n, e, d, p, q, dp, dq, u)