好友
阅读权限10
听众
最后登录1970-1-1
|
xor
1、先算出p和q
## p^^q
N = 0x964DD7ACEC6A6AA87F47E22C6FCA8EE5AD6DF00C8998EA43205CCE2099F8FC7BA563EC43C3787070013640DED0F3DE9179982BCBEEA7A984A275CF2CC27811CA207FA75A86EDC1C0C4BD2DF9AF4E5AA7EA269CF56AB9B230737C4F73F75A5C70E5717EDD106D46F9B94574D2A5F03513BBD16A1B3C97D3398CB40C79CE3C41D5C34D79AA5AA8E97AB1393B9E5677B35944DAA572B9B91F0B0185D311579843AEAC1337D772FC7F7A8C70A6C6C663D80C0D16664997DB442EEB8100161D5A2CDB2BE2FC1029EEFDCB57632E88D747C9FEC8DC18C0CA3807AA825C41DFD5BFCCFD52ADB3DDD0B679526FB580870D6C8A65EB209D178FDED947D7E292A2C7BE4AD3
gift = 81616793159567136422389758585591940662857576283302867011800445277592420866836093507411736040996742950676922327919824777893118631113867500244368612517429678572193116622664051815861481500355863477109385340706136160893878015686817479333596425572252594901263332045777914789662665019123717287533930696370434650226
PR.<x> = PolynomialRing(Zmod(N))
ok = False
def pq_xor(tp,tq,idx):
global ok
if ok:
return
if tp*tq>N:
return
if (tp+(2<<idx))*(tq+(2<<idx))<N:
return
if idx<=400:
try:
f = tp + x
rr = f.monic().small_roots(X=2^400, beta=0.4)
if rr != []:
print(rr)
print(tp)
print('p = ',f(rr[0]))
ok = True
return
except:
pass
return
idx -=1
b = (gift >>idx)&1
one = 1<<idx
if b==0:
pq_xor(tp,tq,idx)
pq_xor(tp+one,tq+one,idx)
else: #1
pq_xor(tp+one,tq,idx)
pq_xor(tp,tq+one,idx)
#N.nbits()=2048 gift.nbits()=1023 p,q的1024位为1
tp = 1<<1023
tq = 1<<1023
pq_xor(tp,tq,1023)
2、解rsa
from gmpy2 import *
from Crypto.Util.number import *
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
f2 = open('flag.enc','rb').read()
c = bytes_to_long(f2)
print(f'c = {f2}')
N = 0x964DD7ACEC6A6AA87F47E22C6FCA8EE5AD6DF00C8998EA43205CCE2099F8FC7BA563EC43C3787070013640DED0F3DE9179982BCBEEA7A984A275CF2CC27811CA207FA75A86EDC1C0C4BD2DF9AF4E5AA7EA269CF56AB9B230737C4F73F75A5C70E5717EDD106D46F9B94574D2A5F03513BBD16A1B3C97D3398CB40C79CE3C41D5C34D79AA5AA8E97AB1393B9E5677B35944DAA572B9B91F0B0185D311579843AEAC1337D772FC7F7A8C70A6C6C663D80C0D16664997DB442EEB8100161D5A2CDB2BE2FC1029EEFDCB57632E88D747C9FEC8DC18C0CA3807AA825C41DFD5BFCCFD52ADB3DDD0B679526FB580870D6C8A65EB209D178FDED947D7E292A2C7BE4AD3
p = 157194667273395078091690715359649373226005247729684564897233925620451263163534476559233007477874770144949074471296577678270820453203784261690001421933906316488224081275610737920296334697466445787310072301759253431687655435272535953996103257268055911344285357880348486067423090573148921357811044304231614285373
q = N//p
print(isPrime(q))
assert N == p*q
e = 65537
d = inverse(e, (p-1)*(q-1))
key = RSA.construct((N,e,d,p,q))
cipher = PKCS1_OAEP.new(key)
m = cipher.decrypt(f2)
print(m)
# flag{b8f20dd8-a985-41f4-ada3-0926bfc3a283}
|
|