[GKCTF2021]RRRRSA

题目
from Crypto.Util.number import *
from gmpy2 import gcd flag = b'xxxxxxxxxxxxx'
p = getPrime(512)
q = getPrime(512)
m = bytes_to_long(flag)
n = p*q
e = 65537
c = pow(m,e,n)
print('c={}'.format(c)) p1 = getPrime(512)
q1 = getPrime(512)
n1 = p1*q1
e1 = 65537
assert gcd(e1,(p1-1)*(q1-1)) == 1
c1 = pow(p,e1,n1)
print('n1={}'.format(n1))
print('c1={}'.format(c1))
hint1 = pow(2020 * p1 + q1, 202020, n1)
hint2 = pow(2021 * p1 + 212121, q1, n1)
print('hint1={}'.format(hint1))
print('hint2={}'.format(hint2)) p2 = getPrime(512)
q2 = getPrime(512)
n2 = p2*q2
e2 = 65537
assert gcd(e1,(p2-1)*(q2-1)) == 1
c2 = pow(q,e2,n2)
hint3 = pow(2020 * p2 + 2021 * q2, 202020, n2)
hint4 = pow(2021 * p2 + 2020 * q2, 212121, n2)
print('n2={}'.format(n2))
print('c2={}'.format(c2))
print('hint3={}'.format(hint3))
print('hint4={}'.format(hint4))
分析

不是非常常规的rsa,我们先看看四个提示:

hint1 = pow(2020 * p1 + q1, 202020, n1)=\((2020 * p1 + q1)^{202020}mod\;n1\)

hint2 = pow(2021 * p1 + 212121, q1, n1)=\((2021 * p1 + 212121)^{q1}mod\;n1\)

hint3 = pow(2020 * p2 + 2021 * q2, 202020, n2)=\((2020 * p2 + 2021 * q2)^{202020}mod\;n2\)

hint4 = pow(2021 * p2 + 2020 * q2, 212121, n2)=\((2021 * p2 + 2020 * q2)^{212121}mod\;n2\)

像hint1这样式子,我们可以用二项式定理进行一个化简。

(觉得typora写公式好麻烦干脆手写的屑↓

pq都出来之后就可以常规做题了。

from gmpy2 import gcd
from Crypto.Util.number import *
import gmpy2
c=13492392717469817866883431475453770951837476241371989714683737558395769731416522300851917887957945766132864151382877462142018129852703437240533684604508379950293643294877725773675505912622208813435625177696614781601216465807569201380151669942605208425645258372134465547452376467465833013387018542999562042758
n1=75003557379080252219517825998990183226659117019770735080523409561757225883651040882547519748107588719498261922816865626714101556207649929655822889945870341168644508079317582220034374613066751916750036253423990673764234066999306874078424803774652754587494762629397701664706287999727238636073466137405374927829
c1=68111901092027813007099627893896838517426971082877204047110404787823279211508183783468891474661365139933325981191524511345219830693064573462115529345012970089065201176142417462299650761299758078141504126185921304526414911455395289228444974516503526507906721378965227166653195076209418852399008741560796631569
hint1=23552090716381769484990784116875558895715552896983313406764042416318710076256166472426553520240265023978449945974218435787929202289208329156594838420190890104226497263852461928474756025539394996288951828172126419569993301524866753797584032740426259804002564701319538183190684075289055345581960776903740881951
hint2=52723229698530767897979433914470831153268827008372307239630387100752226850798023362444499211944996778363894528759290565718266340188582253307004810850030833752132728256929572703630431232622151200855160886614350000115704689605102500273815157636476901150408355565958834764444192860513855376978491299658773170270
n2=114535923043375970380117920548097404729043079895540320742847840364455024050473125998926311644172960176471193602850427607899191810616953021324742137492746159921284982146320175356395325890407704697018412456350862990849606200323084717352630282539156670636025924425865741196506478163922312894384285889848355244489
c2=67054203666901691181215262587447180910225473339143260100831118313521471029889304176235434129632237116993910316978096018724911531011857469325115308802162172965564951703583450817489247675458024801774590728726471567407812572210421642171456850352167810755440990035255967091145950569246426544351461548548423025004
hint3=25590923416756813543880554963887576960707333607377889401033718419301278802157204881039116350321872162118977797069089653428121479486603744700519830597186045931412652681572060953439655868476311798368015878628002547540835719870081007505735499581449077950263721606955524302365518362434928190394924399683131242077
hint4=104100726926923869566862741238876132366916970864374562947844669556403268955625670105641264367038885706425427864941392601593437305258297198111819227915453081797889565662276003122901139755153002219126366611021736066016741562232998047253335141676203376521742965365133597943669838076210444485458296240951668402513
e=65537
q1 = gcd(n1,pow(hint2-212121,202020,n1)*pow(2020,202020,n1)-hint1*pow(2021,202020,n1))
p1 = n1//q1
q2 = gcd(n2,pow(hint3,212121,n2)*pow(2021,202020*212121,n2)-pow(hint4,202020,n2)*pow(2020,202020*212121,n2))
p2 = n2//q2
d2 = gmpy2.invert(e,(q2-1)*(p2-1))
q = pow(c2,d2,n2)
d1 = gmpy2.invert(e,(q1-1)*(p1-1))
p = pow(c1,d1,n1)
d = gmpy2.invert(e,(q-1)*(p-1))
m = pow(c,d,p*q)
print(long_to_bytes(m))
总结

学了很多新东西。感觉非常奇妙,繁琐分析的最后是简短的一行表达式,有种新年第一天穿着新……(下略)

1)拿到两个式子后,先把括号去掉,然后把等号右边的常数项去掉

2)得到两个只含p和q的式子,凑一下,使两个式子的p(或q)的指数系数相同

3)将两个式子相加或相减消掉p,剩下的式子里的未知数应该只有q。再将结果与n求解最大公因数,进而求出q。

[GKCTF2021]RRRRSA的更多相关文章

  1. GKCTF2021 MISC

    1.签到 当时没签上┭┮﹏┭┮: 追踪http流,发现依次执行[ls][ls/][whoami] 发现存在[fl4g],同时发现破解的规则为hex decode->base64 decode-& ...

  2. GKCTF 2021 Reverse Writeup

    前言 GKCTF 2021所以题目均以开源,下面所说的一切思路可以自行通过源码对比IDA进行验证. Github项目地址:https://github.com/w4nd3r-0/GKCTF2021 出 ...

  3. 协议层安全相关《http请求走私与CTF利用》

    0x00 前言 最近刷题的时候多次遇到HTTP请求走私相关的题目,但之前都没怎么接触到相关的知识点,只是在GKCTF2021--hackme中使用到了 CVE-2019-20372(Nginx< ...

随机推荐

  1. 我的第一个自动刷作业脚本(大起大落的selenium经验分享)

    起因 故事的开始是大二的上学期,有一门叫计算机结构(computer organization)的课.新教授这门课的教授在原来的政策上做了一些变动.他引入了一个叫做zybook的作业平台来确保我们能跟 ...

  2. python进阶之路11 闭包函数 装饰器

    函数名的多种用法 函数名其实绑定的也是一块内存地址 只不过该地址里面存放的不是数据值而是一段代码 函数名加括号就会找到该代码并执行 1.可以当作变量名赋值 def index():pass res = ...

  3. [C++]const_cast,dynamic_cast,reinterpret_cast,static_cast转型

    C++四种新式转型: const_cast(expression) dynamic_cast(expression) reinterpret_cast(expression) static_cast( ...

  4. 洛谷P6599 「EZEC-2」异或【题解】

    题目大意 有\(T\)组数据,每组数据给定两个\(l,n\in\mathbb{N*}\),构造一个长为\(l\),每个元素不超过\(n\)的数组 令他为\(a\),要使 \[\sum_{i=1}^l\ ...

  5. VS Code離線安裝插件報錯Unable to install extension 'dart-code.flutter' as it is not compatible with VS Code '1.51.1'.

    VS Code離線安裝插件報錯Unable to install extension 'dart-code.flutter' as it is not compatible with VS Code ...

  6. Stream流中的常用方法_skip-Stream流中的常用方法_concat

    Stream流中的常用方法_skip 如果希望跳过前几个元素,可以使用skip方法获取一个截取之后的新流∶ 如果流的当前长度大于n,则跳过前n个;否则将会得到一个长度为0的空流.基本使用: Strea ...

  7. 默认方法:negate-集合信息筛选

    默认方法:negate "与"."或"已经了解了,剩下的"非"(取反)也会简单.默认方法negate的JDK源代码为︰ 集合信息筛选 数组当 ...

  8. MyBatis的使用六(解决字段名与成员名不一致)

    本文主要讲述mybatis如何解决mysql的字段名与java实体类的成员变量名称不一致. 一. 介绍实体类和数据表 1. 实体类Employee public class Employee { pr ...

  9. CentOS7 RPM方式安装JDK

    1.下载jdk rpm Java Downloads | Oracle 中国 https://www.oracle.com/cn/java/technologies/downloads/#jdk19- ...

  10. Vue22 VueCli 脚手架

    1 简介 CLI 是 Command-Line Interface, 翻译为命令行界面, 但是俗称脚手架 Vue CLI是一个官方发布 vue.js 项目脚手架 使用 vue-cli 可以快速搭建 V ...