2022-04-10

task

#!/usr/bin/env python3.9
# -*- coding: utf-8 -*-
import gmpy2
from Crypto.Util.number import getPrime, isPrime, bytes_to_long
from secret import FLAG, E1, E2, P, Q1, Q2


def next_prime(num: int) -> int:
    num = num + 2 if num % 2 else num + 1
    while not isPrime(num):
        num += 2
    return num


p = getPrime(1024)
q = next_prime(13 * p + 52433)
n = p * q
c = pow(E1, 65537, n)
print(f'n = {n}')
print(f'c = {c}')
# n = 259890247443804761118228016219136684924167917522422806784955644506207888351304889509903433463527585637662916733749092132376942374314732903500147772464631303079816368071397816148840032140257741234623942972389330658394009752410215122103809627221978841602901343785314357668760003964914166778126289222020290468903953247175457923739210505739006832916267484268910832870071479071917094394577029957202256240184604809173087617931324803860474150820931470601204798293536446102918790547726389486804819530604731780327035091686886029998993701502986883473430288066966414564995833243607159289786503640253962387954613582807541606942749
# c = 56440807794784873155975554526282639269465966474628218727704063243557056715418468759474870838438499918826893792546655715745526027857276713541238255702873551647125573694638955842404342457788336135938834876470872953749978996773098030211241264236735693391584657468679440790473717213665228349818831369116042003502072082694693222429663702695116191467500411709133178801495474342969042325471712020201636143305265100439673243718028002788717655896374089009854112149946761782391009483256739835432971090640363725440908313218581221815215679758846253486316393841855585127055921605329511990315967829323041426287669986012868743294383

assert E2.bit_length() == 68
ns = [getPrime(1024) * getPrime(1024) for _ in range(3)]
cs = [pow(E2, 89, n) for n in ns]
print(f'ns = {ns}')
print(f'cs = {cs}')
# ns = [14768877816717539611731045629979591214811330868162202565624664129912032603118216104003405398241174001521124840138476411692247747689580245229633834241354343663803512914327061439026544199607661340809522611714979050746642474298514984319799503942592454459033529230661408387428307100152240359931113582007834771064836814506679488489884326758202775879875295915804661697151797257882534759047427072076935608038544138918082424231665777438859097733700757281503798641211223630919282623055222757291265697646847664236117770057816055708821059139836629062435882101110861519241590121890490749372041394827106099992940775405355963537093, 19666545323134160617662045141655785833630626383149539459109151988125669512345207825350175718697646952110980014593295830440913639941294759366636907406022093593567085060130487695457293050838339751589656086715810761617034895110908921222238432080750232780959480802362123627948837433700702719795982826451033532941818523986557921585705333167085832750810602154544395282520714531925732606981719148520396524986802623258451137156083339557903763787258971153135598507990986538670962098805822026704693511638643139987774524413005609600478133862229864877539603414684417042041304917739974092129299946247802874434223813699963768959763, 15779368467163552829155887858552042780948412851484383573815791940301167449302309346542230278336893665527037182849622548454420128882466125704119621262910314425821276866328941773658802929134316373368772517587582142657657900715575360467459524840118867966716907874327634603692474154990530124677535030772815052167989006001122992617640466985679436530404398438789489536785124944395405233412010429769188201394928826199790856798037225834203918546557553132305324936899974409559438594918135814322834671435422502955119706510092210390004609239572208028155476305714319440640836664426079307167151133383912378800774498518280438182203]
# cs = [567850780510774217643896612655688253669214849388311742718787427972718595963863911452371929027639644307000739560965604527599743560884685911816686998038866122672741996289320331846282807112765589075393537405844508645186580219935681309551863862838005818102040526577638711724084442111910507832270265866651971011054177521418891278722648745957602559922271453142590901732965224467398990543233797146010537073758529331487664292226361777579207274132983906773032552051857982429265573985911417593730675378625055722840026395255063109465505900223512922055483597989949853680781366266645807749448493048852769413781512590488755978697, 16628689839738879696100069757848926944243292698323350637649584538310327633442975226999529525456259644570646951204781799076562002069453482286619645490158456560690652510541536353482676298819618268940054219537327611925439186331762973027774866201153493437368183695220271473169270116128825534852722159311320980174393841950205399254670211089683984121742072363442124572477542374398931013755766208246311751050272325010037196256536013007563620011493948752002272261333679256550076724906019636720113375326020525053147012787943894857133740100766017749473241441986776405175439625904183358501475568051587218891671961057999591001668, 7423561249867066194530577825751519067130148169119196474389962513177470795034282161787340397726773451715094904236471232577160713265828264263562089259120152501642969714248564032095513372662818544778929805546621303928734411268330553516461133100124009496405027437585651547170169428109291156528057270539229674840678343567274741287724466921000898325604505576001240065875934111729300326280297815704004629408909170232221133608585206749960618344234298494521288440446897752115014786315122220398755923018428341634901663783340643124065995812457742638126684083971978356298818106529467920958965081076230810503321379529620885195317]

qq = getPrime(1024)
nn = P * qq
qqq = qq >> 460 << 460
print(f'nn = {nn}')
print(f'qqq = {qqq}')
# nn = 23771565097692613805925074947762277776248175039904858640393515573404187252654078983280079053221178407495942053339065277458665885747681910079114703240305875633863359892288520067708424052894526852549504476844606883063864442920259420950734334491426028395052970793501884544454476339394388681877266173301424526574691645472879318102691079950543768832010711509846115011175415348311434230971982003766118635224304817289494045882942006710195248414232575795509048370893046873209854272475037633188006029149253662072051929871584657148706162114224833446325630812536670801454942386364791171210124128405736320131739365476538566297221
# qqq = 133032422062763199415874751118944045012894101492725125544603031449033342774705002029942162253414972567936917624806763857304579798126496989113467512098796380998642424465450876083699444199323535712213201610265804758481994796046002002888732353517685661856315914283134231856793005918816096507197913746788533141504

assert len(FLAG) == 41
n1 = P * Q1
n2 = P * Q2
c1 = pow(bytes_to_long(FLAG), E1, n1)
c2 = pow(bytes_to_long(FLAG), E2, n2)
print(f'n1 = {n1}')
print(f'n2 = {n2}')
print(f'c1 = {c1}')
print(f'c2 = {c2}')
# n1 = 18615819728233110617885488070170280058678037220922758497798317613309446913882382218988276336640291907575211969819046709233017427083675321319838324128807023798454132857376509073136982503941694071901508081576711033903943161653983689801633273077510931811744081084936611086209603707614306074106350610895121051037503666820027946473646362175767823115112774958590804061001365734472031264785538167765588332991014359407442463788578662889696722061503294486117727781690597648065733866183443407041195995273979020907657344919680556433317183235313361749388077888127521789307610579244550742677827830100382943563044931371158281194163
# n2 = 22235180820309259706502497391595616630317652707229408232984221118221138397072915888161096649178543569952298675317455055054711067738814936900669274866964814475704409045609134050327687101928407393899730040553995551707206684121719794241712780730733754614935418331338491945060410407827204848804476741560106663626242515362482874211450679398078802410658500152741970056159174742779223140126949953245841551474643122777751158488054087594348467274510239721892662634242570010409232517423384460511916732209632178418225020386222389749581055404458956827746631748723546337995180200182270159027360019493521907846599393898139341839193
# c1 = 1525820431343041210301379575059913906569418054637665661643410281713022846489115293334487842528337439323978772411684711118057475068210699113700871606346215679469029155294879483210586505221025352889601315289266265230768668926522950865792439010944831832053020211211241375983851101045291043503255291515859151541329786361770243582913033586488891223478458652727336603808554667537786901236803690264785625510500540325622082291536124402060606751637583269840534796600068310534190180959390351021831168200565432749224552316357935660465975429052194600195556583114196651347453503456007077856687598779890075785752654683709014445698
# c2 = 2509634403856675946927680634000452005161577113423040711542069655740930931736584736517738770425013518679008580546264171800386267636606636241188186970317732380596679813848641163469535812260252128213739824439191581905567154377358056451969275213354169650531184684070609988553508927041905357399891406475138462092327366942092124805804648351539744975570980563068923730546229390872219181219152034725766967223049116158569712493601760712914100477130371451141171141672295748388053527312512026450202897398153439868652905337399339851546705444294075524110306269211294801860205973030097992067908247427581382455091564823866204734549

4部分,挨个来

第一部分

#!/usr/bin/env python3.9
# -*- coding: utf-8 -*-
import gmpy2
from Crypto.Util.number import getPrime, isPrime, bytes_to_long
from secret import FLAG, E1, E2, P, Q1, Q2


def next_prime(num: int) -> int:
    num = num + 2 if num % 2 else num + 1
    while not isPrime(num):
        num += 2
    return num


p = getPrime(1024)
q = next_prime(13 * p + 52433)
n = p * q
c = pow(E1, 65537, n)
print(f'n = {n}')
print(f'c = {c}')
# n = 259890247443804761118228016219136684924167917522422806784955644506207888351304889509903433463527585637662916733749092132376942374314732903500147772464631303079816368071397816148840032140257741234623942972389330658394009752410215122103809627221978841602901343785314357668760003964914166778126289222020290468903953247175457923739210505739006832916267484268910832870071479071917094394577029957202256240184604809173087617931324803860474150820931470601204798293536446102918790547726389486804819530604731780327035091686886029998993701502986883473430288066966414564995833243607159289786503640253962387954613582807541606942749
# c = 56440807794784873155975554526282639269465966474628218727704063243557056715418468759474870838438499918826893792546655715745526027857276713541238255702873551647125573694638955842404342457788336135938834876470872953749978996773098030211241264236735693391584657468679440790473717213665228349818831369116042003502072082694693222429663702695116191467500411709133178801495474342969042325471712020201636143305265100439673243718028002788717655896374089009854112149946761782391009483256739835432971090640363725440908313218581221815215679758846253486316393841855585127055921605329511990315967829323041426287669986012868743294383

题解
这部分的考点是求p、q

myR = RealField(1000)
x = polygen(myR)
f = x * (13*x + 52433) - n
p = int(f.roots()[1][0])
print(p)
while n % p != 0:
    p = p - 1

q = n // p
print(f"p = {p}")
print(f"q = {q}")

p = 141391504326381962845029163873315655483417604464172202180652203058720396929243977701615563494745361664962558259222455930121854846927988608078930179666780598410088884935498332830675322176877906391644672116065621237839177912363246941651122325418526963474765060101510943921138491622493146723107316152849953911719
q = 1838089556242965516985379130353103521284428858034238628348478639763365160080171710121002325431689701644513257369891927091584113010063851905026092335668147779331155504161478326798779188299412783091380737508853076091909312860722210241464590230440850525171945781319642270974800391092410907400395109987049400905371
import gmpy2
from Crypto.Util.number import long_to_bytes 

phi = (q-1) * (p-1)
d = gmpy2.invert(e,phi)
m = gmpy2.powmod(c,d,n)

print(m)
#E1 = 257368768118473120755

第二部分

assert E2.bit_length() == 68
ns = [getPrime(1024) * getPrime(1024) for _ in range(3)]
cs = [pow(E2, 89, n) for n in ns]
print(f'ns = {ns}')
print(f'cs = {cs}')
# ns = [14768877816717539611731045629979591214811330868162202565624664129912032603118216104003405398241174001521124840138476411692247747689580245229633834241354343663803512914327061439026544199607661340809522611714979050746642474298514984319799503942592454459033529230661408387428307100152240359931113582007834771064836814506679488489884326758202775879875295915804661697151797257882534759047427072076935608038544138918082424231665777438859097733700757281503798641211223630919282623055222757291265697646847664236117770057816055708821059139836629062435882101110861519241590121890490749372041394827106099992940775405355963537093, 19666545323134160617662045141655785833630626383149539459109151988125669512345207825350175718697646952110980014593295830440913639941294759366636907406022093593567085060130487695457293050838339751589656086715810761617034895110908921222238432080750232780959480802362123627948837433700702719795982826451033532941818523986557921585705333167085832750810602154544395282520714531925732606981719148520396524986802623258451137156083339557903763787258971153135598507990986538670962098805822026704693511638643139987774524413005609600478133862229864877539603414684417042041304917739974092129299946247802874434223813699963768959763, 15779368467163552829155887858552042780948412851484383573815791940301167449302309346542230278336893665527037182849622548454420128882466125704119621262910314425821276866328941773658802929134316373368772517587582142657657900715575360467459524840118867966716907874327634603692474154990530124677535030772815052167989006001122992617640466985679436530404398438789489536785124944395405233412010429769188201394928826199790856798037225834203918546557553132305324936899974409559438594918135814322834671435422502955119706510092210390004609239572208028155476305714319440640836664426079307167151133383912378800774498518280438182203]
# cs = [567850780510774217643896612655688253669214849388311742718787427972718595963863911452371929027639644307000739560965604527599743560884685911816686998038866122672741996289320331846282807112765589075393537405844508645186580219935681309551863862838005818102040526577638711724084442111910507832270265866651971011054177521418891278722648745957602559922271453142590901732965224467398990543233797146010537073758529331487664292226361777579207274132983906773032552051857982429265573985911417593730675378625055722840026395255063109465505900223512922055483597989949853680781366266645807749448493048852769413781512590488755978697, 16628689839738879696100069757848926944243292698323350637649584538310327633442975226999529525456259644570646951204781799076562002069453482286619645490158456560690652510541536353482676298819618268940054219537327611925439186331762973027774866201153493437368183695220271473169270116128825534852722159311320980174393841950205399254670211089683984121742072363442124572477542374398931013755766208246311751050272325010037196256536013007563620011493948752002272261333679256550076724906019636720113375326020525053147012787943894857133740100766017749473241441986776405175439625904183358501475568051587218891671961057999591001668, 7423561249867066194530577825751519067130148169119196474389962513177470795034282161787340397726773451715094904236471232577160713265828264263562089259120152501642969714248564032095513372662818544778929805546621303928734411268330553516461133100124009496405027437585651547170169428109291156528057270539229674840678343567274741287724466921000898325604505576001240065875934111729300326280297815704004629408909170232221133608585206749960618344234298494521288440446897752115014786315122220398755923018428341634901663783340643124065995812457742638126684083971978356298818106529467920958965081076230810503321379529620885195317]

题解
这部分的考点是中国剩余定理

from gmpy2 import *
from Crypto.Util.number import *
from functools import reduce

N = [14768877816717539611731045629979591214811330868162202565624664129912032603118216104003405398241174001521124840138476411692247747689580245229633834241354343663803512914327061439026544199607661340809522611714979050746642474298514984319799503942592454459033529230661408387428307100152240359931113582007834771064836814506679488489884326758202775879875295915804661697151797257882534759047427072076935608038544138918082424231665777438859097733700757281503798641211223630919282623055222757291265697646847664236117770057816055708821059139836629062435882101110861519241590121890490749372041394827106099992940775405355963537093, 19666545323134160617662045141655785833630626383149539459109151988125669512345207825350175718697646952110980014593295830440913639941294759366636907406022093593567085060130487695457293050838339751589656086715810761617034895110908921222238432080750232780959480802362123627948837433700702719795982826451033532941818523986557921585705333167085832750810602154544395282520714531925732606981719148520396524986802623258451137156083339557903763787258971153135598507990986538670962098805822026704693511638643139987774524413005609600478133862229864877539603414684417042041304917739974092129299946247802874434223813699963768959763, 15779368467163552829155887858552042780948412851484383573815791940301167449302309346542230278336893665527037182849622548454420128882466125704119621262910314425821276866328941773658802929134316373368772517587582142657657900715575360467459524840118867966716907874327634603692474154990530124677535030772815052167989006001122992617640466985679436530404398438789489536785124944395405233412010429769188201394928826199790856798037225834203918546557553132305324936899974409559438594918135814322834671435422502955119706510092210390004609239572208028155476305714319440640836664426079307167151133383912378800774498518280438182203]
c = [567850780510774217643896612655688253669214849388311742718787427972718595963863911452371929027639644307000739560965604527599743560884685911816686998038866122672741996289320331846282807112765589075393537405844508645186580219935681309551863862838005818102040526577638711724084442111910507832270265866651971011054177521418891278722648745957602559922271453142590901732965224467398990543233797146010537073758529331487664292226361777579207274132983906773032552051857982429265573985911417593730675378625055722840026395255063109465505900223512922055483597989949853680781366266645807749448493048852769413781512590488755978697, 16628689839738879696100069757848926944243292698323350637649584538310327633442975226999529525456259644570646951204781799076562002069453482286619645490158456560690652510541536353482676298819618268940054219537327611925439186331762973027774866201153493437368183695220271473169270116128825534852722159311320980174393841950205399254670211089683984121742072363442124572477542374398931013755766208246311751050272325010037196256536013007563620011493948752002272261333679256550076724906019636720113375326020525053147012787943894857133740100766017749473241441986776405175439625904183358501475568051587218891671961057999591001668, 7423561249867066194530577825751519067130148169119196474389962513177470795034282161787340397726773451715094904236471232577160713265828264263562089259120152501642969714248564032095513372662818544778929805546621303928734411268330553516461133100124009496405027437585651547170169428109291156528057270539229674840678343567274741287724466921000898325604505576001240065875934111729300326280297815704004629408909170232221133608585206749960618344234298494521288440446897752115014786315122220398755923018428341634901663783340643124065995812457742638126684083971978356298818106529467920958965081076230810503321379529620885195317]


def chinese_remainder(modulus, remainders):
    Sum = 0
    prod = reduce(lambda a, b: a*b, modulus)
    for m_i, r_i in zip(modulus, remainders):
        p = prod // m_i
        Sum += r_i * (inverse(p,m_i)*p)
    return Sum % prod
e = 89
# print(chinese_remainder(N,c))
pow_m_e = chinese_remainder(N,c)
# pow_m_e = 17446992834638639179129969961058029457462398677361658450137832328330435503838651797276948890990069700515669656391607670623897280684064423087023742140145529356863469816868212911716782075239982647322703714504545802436551322108638975695013439206776300941300053940942685511792851350404139366581130688518772175108412341696958930756520037
m = iroot(pow_m_e,e)[0]
print(m)
#E2 = 239147514164170153335

第三部分

qq = getPrime(1024)
nn = P * qq
qqq = qq >> 460 << 460
print(f'nn = {nn}')
print(f'qqq = {qqq}')
# nn = 23771565097692613805925074947762277776248175039904858640393515573404187252654078983280079053221178407495942053339065277458665885747681910079114703240305875633863359892288520067708424052894526852549504476844606883063864442920259420950734334491426028395052970793501884544454476339394388681877266173301424526574691645472879318102691079950543768832010711509846115011175415348311434230971982003766118635224304817289494045882942006710195248414232575795509048370893046873209854272475037633188006029149253662072051929871584657148706162114224833446325630812536670801454942386364791171210124128405736320131739365476538566297221
# qqq = 133032422062763199415874751118944045012894101492725125544603031449033342774705002029942162253414972567936917624806763857304579798126496989113467512098796380998642424465450876083699444199323535712213201610265804758481994796046002002888732353517685661856315914283134231856793005918816096507197913746788533141504

题解
这一部分考点是已知高位攻击,并且已知q的位数是不够的,还需要爆破

nn =23771565097692613805925074947762277776248175039904858640393515573404187252654078983280079053221178407495942053339065277458665885747681910079114703240305875633863359892288520067708424052894526852549504476844606883063864442920259420950734334491426028395052970793501884544454476339394388681877266173301424526574691645472879318102691079950543768832010711509846115011175415348311434230971982003766118635224304817289494045882942006710195248414232575795509048370893046873209854272475037633188006029149253662072051929871584657148706162114224833446325630812536670801454942386364791171210124128405736320131739365476538566297221
qqq = 133032422062763199415874751118944045012894101492725125544603031449033342774705002029942162253414972567936917624806763857304579798126496989113467512098796380998642424465450876083699444199323535712213201610265804758481994796046002002888732353517685661856315914283134231856793005918816096507197913746788533141504
qbits =1024

for i in range(0,4096):#2**12=4096
    q4 = qqq >> 460 <<12
    q4 = q4 + int(i)
    kbits = qbits - q4.nbits()
    q4 = q4 << kbits
    PR.<x> = PolynomialRing(Zmod(nn))
    f = x + q4
    roots = f.small_roots(X=2^kbits,beta=0.4)
    if roots:
        qq= q4 + int(roots([0]))
        print ("nn=",nn)
        print ("qq=",qq)
        print ("P=",n/qq)
        break

但是报错了

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
 in 
     12     roots = f.small_roots(X=Integer(2)**kbits,beta=RealNumber('0.4'))
     13     if roots:
---> 14         qq= q4 + int(roots([Integer(0)]))
     15         print ("nn=",nn)
     16         print ("qq=",qq)

TypeError: 'list' object is not callable

不知道要怎么该,不过最重要的地方已经运行出来了,所以已经可以得到答案

sage: roots
[266925215453931856740419416207718765219732028418616616365603946629891067763496831421531789587395445250379235260819700817082651451002123]

x=266925215453931856740419416207718765219732028418616616365603946629891067763496831421531789587395445250379235260819700817082651451002123
i = 1139
qq = 133032422062763199415874751118944045012894101492725125544603031449033342774705002029942162253414972567936917624806763857304579798126496989113467512098796380998642424465451704219931632349503728132449802255149198775723846069933278985829545731557914585587815867421686795953411964810160894503565026740694976236811
P = 178690012021862286682053158598498851588822317718839409923223701434579151038164952153978011177841025365905802867581675992965544360513534372215202833215598681032319680797477566312560792369842713255659833645016518634561932554273302329543036996217970929404267862213561616814792646589237863215169665735626976859311

第四部分

assert len(FLAG) == 41
n1 = P * Q1
n2 = P * Q2
c1 = pow(bytes_to_long(FLAG), E1, n1)
c2 = pow(bytes_to_long(FLAG), E2, n2)
print(f'n1 = {n1}')
print(f'n2 = {n2}')
print(f'c1 = {c1}')
print(f'c2 = {c2}')
# n1 = 18615819728233110617885488070170280058678037220922758497798317613309446913882382218988276336640291907575211969819046709233017427083675321319838324128807023798454132857376509073136982503941694071901508081576711033903943161653983689801633273077510931811744081084936611086209603707614306074106350610895121051037503666820027946473646362175767823115112774958590804061001365734472031264785538167765588332991014359407442463788578662889696722061503294486117727781690597648065733866183443407041195995273979020907657344919680556433317183235313361749388077888127521789307610579244550742677827830100382943563044931371158281194163
# n2 = 22235180820309259706502497391595616630317652707229408232984221118221138397072915888161096649178543569952298675317455055054711067738814936900669274866964814475704409045609134050327687101928407393899730040553995551707206684121719794241712780730733754614935418331338491945060410407827204848804476741560106663626242515362482874211450679398078802410658500152741970056159174742779223140126949953245841551474643122777751158488054087594348467274510239721892662634242570010409232517423384460511916732209632178418225020386222389749581055404458956827746631748723546337995180200182270159027360019493521907846599393898139341839193
# c1 = 1525820431343041210301379575059913906569418054637665661643410281713022846489115293334487842528337439323978772411684711118057475068210699113700871606346215679469029155294879483210586505221025352889601315289266265230768668926522950865792439010944831832053020211211241375983851101045291043503255291515859151541329786361770243582913033586488891223478458652727336603808554667537786901236803690264785625510500540325622082291536124402060606751637583269840534796600068310534190180959390351021831168200565432749224552316357935660465975429052194600195556583114196651347453503456007077856687598779890075785752654683709014445698
# c2 = 2509634403856675946927680634000452005161577113423040711542069655740930931736584736517738770425013518679008580546264171800386267636606636241188186970317732380596679813848641163469535812260252128213739824439191581905567154377358056451969275213354169650531184684070609988553508927041905357399891406475138462092327366942092124805804648351539744975570980563068923730546229390872219181219152034725766967223049116158569712493601760712914100477130371451141171141672295748388053527312512026450202897398153439868652905337399339851546705444294075524110306269211294801860205973030097992067908247427581382455091564823866204734549

题解

虽然第三题得到结果了,但是貌似没啥用,因为就算没有解出来第四题依旧可以得到P
第四部分主要考的应该是e与phi不互素的问题,同样套代码就行

import gmpy2

e1 = 257368768118473120755
e2 = 239147514164170153335
n1 = 18615819728233110617885488070170280058678037220922758497798317613309446913882382218988276336640291907575211969819046709233017427083675321319838324128807023798454132857376509073136982503941694071901508081576711033903943161653983689801633273077510931811744081084936611086209603707614306074106350610895121051037503666820027946473646362175767823115112774958590804061001365734472031264785538167765588332991014359407442463788578662889696722061503294486117727781690597648065733866183443407041195995273979020907657344919680556433317183235313361749388077888127521789307610579244550742677827830100382943563044931371158281194163
n2 = 22235180820309259706502497391595616630317652707229408232984221118221138397072915888161096649178543569952298675317455055054711067738814936900669274866964814475704409045609134050327687101928407393899730040553995551707206684121719794241712780730733754614935418331338491945060410407827204848804476741560106663626242515362482874211450679398078802410658500152741970056159174742779223140126949953245841551474643122777751158488054087594348467274510239721892662634242570010409232517423384460511916732209632178418225020386222389749581055404458956827746631748723546337995180200182270159027360019493521907846599393898139341839193
c1 = 1525820431343041210301379575059913906569418054637665661643410281713022846489115293334487842528337439323978772411684711118057475068210699113700871606346215679469029155294879483210586505221025352889601315289266265230768668926522950865792439010944831832053020211211241375983851101045291043503255291515859151541329786361770243582913033586488891223478458652727336603808554667537786901236803690264785625510500540325622082291536124402060606751637583269840534796600068310534190180959390351021831168200565432749224552316357935660465975429052194600195556583114196651347453503456007077856687598779890075785752654683709014445698
c2 = 2509634403856675946927680634000452005161577113423040711542069655740930931736584736517738770425013518679008580546264171800386267636606636241188186970317732380596679813848641163469535812260252128213739824439191581905567154377358056451969275213354169650531184684070609988553508927041905357399891406475138462092327366942092124805804648351539744975570980563068923730546229390872219181219152034725766967223049116158569712493601760712914100477130371451141171141672295748388053527312512026450202897398153439868652905337399339851546705444294075524110306269211294801860205973030097992067908247427581382455091564823866204734549

#P = gcd(n1,n2)

from Crypto.Util.number import long_to_bytes
Q1=104179408337358612558868008137057422689908734940293245907663894772528062341664942687012702656253968320386223325341610181995698213313340764985399155577019927415849821337299391748881608973409538976781154285006805447521390306173062193658852782644658442629066469576344523312381667901203865248612595891929117487933
P=178690012021862286682053158598498851588822317718839409923223701434579151038164952153978011177841025365905802867581675992965544360513534372215202833215598681032319680797477566312560792369842713255659833645016518634561932554273302329543036996217970929404267862213561616814792646589237863215169665735626976859311
Q2=124434379788327730497139343819967400236365106121790298291776453246549065548055965336314799541166649155742031953548733624992571433119264085757211954429810777204938962392176424769036550975352366286823290570112126573736663091402582821593872869348806524514833120126769490153068794305851777640109210991478595756663
phi1=(P-1)*(Q1-1)
phi2=(P-1)*(Q2-1)
#d1 = gmpy2.invert(e1,phi1)
#m1 = pow(c1,d1,n1)
d2 = gmpy2.invert(e2,phi2)
m2 = pow(c2,d2,n2)
#print(m1)
print(m2)
#print(long_to_bytes(m1))
print(long_to_bytes(m2))
m1 = 4889200840833748726888056616116190723849300116532489075873505685564730480741636378764943779499190368346499132410535328539695824099650745972090057205841731389119074123387868274748516702794512412799471209880626105641349950367519048283025982616813442894180384568913381046309647929333979494636340182296164813229747632938753153682998808629735418882302095098631208810059057530864775477254163586795119180730036175274608850745271410775445602123958823372491574249229414378905280030190978685655726393657737457109140955551422061309395368639560908291779459808945013429224495611040930409726618733305379951543942424915996480482752
m2 = 12048689324953182392571583697269588752146565123608182825988435155466846262536008433967954343402023962316561693100692833972508520160575292452292181892498958386189968182082394735976285347773519194913704786925831995219649654507794326066388670004165893955438509768030671658248905740273469226845087377720366088709660084648448203305903911058067092967736828205429634450861249110456975976593148157252468540642398732367818262884282469569972412922160021004155320631667968262816616458239709148940428872716669880291655672962683663287977541972171329833091990922170997322193878829009565251390813482588620164826443061801467956925566

'''
>>>m1==m2
Flase
>>> c1==pow(m1,e1,n1)
False
>>> gmpy2.iroot(m1,15)
(mpz(111160167330056205118358437135862918423006), False)
>>> gmpy2.iroot(m2,15)
(mpz(118049092758983304698335630476107333299477), False)
>>>
>>> long_to_bytes(gmpy2.iroot(m1,15)[0])
b'\x01F\xab\x9d}\x08\x1d\xd2\x9e\xfd\xb8\x8d\xc3\xca4n!\xde'
>>> long_to_bytes(gmpy2.iroot(m2,15)[0])
b'\x01Z\xeaDC\xe5\xee\xa1\xc4o\xd6\xc7\t\x96u\x14\xed\x15'
>>> 
'''

from libnum import *
m3=m1%P
m2=m2%Q2
m1=m1%Q1

m=solve_crt([m1,m2,m3], [Q1,Q2,P]) 
print(m)
n=Q1*Q2
phi3=(Q1-1)*(Q2-1)
m=m%n
#gcd(15,phi3)==3
d2=modular.invmod(5,phi3)
m=pow(m,d2,n)
print(long_to_bytes(gmpy2.iroot(m,3)[0]))
#2169140254012917623129820665508566537267054938901675301178831815884267934392283070482130903429427587875602014090100624980907181666512739082289491370054277985352778406863765164089638851234476432479332039913272252174228730081430244774921660691242317712716294346577943671391473404307862516280077453714046415710915750996715865605756266143795815671928842170516768522933166189024366947062417228794680104950991464440776325764344672809200624727936041359483968046354877206172438616438742853036849462852435268847920853840114928722401977718931291237029291897976673767854052744201155972655093477730933365113507665844425516390897239338166047856426332644885354671739732100360457613749098954863114945503909830251130650400283871624461093971660097119379308132018882343211326707980010266531060013033961835903405119569781774177214570037508039578077957880957770817126297026699949150417510043321174272476710473554611280956329817792898165348440525
#b'TLS{53e1c7dd-bd3c-414c-b878-d9c10204bf37}'

你可能感兴趣的:(rsa)