首页 > 其他分享 >RSA已知p高位

RSA已知p高位

时间:2024-02-27 13:22:06浏览次数:14  
标签:高位 RSA long 已知 265 print import mod equiv

from Crypto.Util.number import getPrime, bytes_to_long
from secret import flag
p = getPrime(1024)
q = getPrime(1024)
n = p * q
e = 65537
hint1 = p >> 724
hint2 = q % (2 ** 265)
ct = pow(bytes_to_long(flag), e, n)
print(hint1)
print(hint2)
print(n)
print(ct)

题目如上,由hint1可知p的低724位丢失,而q的高位丢失到只剩下低位265位,因为q%(2**256)后得到的余数一定小于2**265,而2**265对应的二进制就是265位。

提取出的数学式子就是$q_{0}\equiv q(mod\,2^{265})\Rightarrow n\equiv pq_{0}(mod\,2^{265})$

则$q_{0}^{-1}n\equiv p(mod\,2^{265})$

令$p_{0}\equiv p(mod\,2^{265})$

则$p_{0}\equiv q_{0}^{-1}n(mod\,2^{265})$

而$q_{0}$的逆为$q_{0}q_{0}^{-1}\equiv1(mod\,2^{265})$

sage代码如下:

from Crypto.Util.number import long_to_bytes

h1 = 1514296530850131082973956029074258536069144071110652176122006763622293335057110441067910479
h2 = 40812438243894343296354573724131194431453023461572200856406939246297219541329623
n = 21815431662065695412834116602474344081782093119269423403335882867255834302242945742413692949886248581138784199165404321893594820375775454774521554409598568793217997859258282700084148322905405227238617443766062207618899209593375881728671746850745598576485323702483634599597393910908142659231071532803602701147251570567032402848145462183405098097523810358199597631612616833723150146418889589492395974359466777040500971885443881359700735149623177757865032984744576285054725506299888069904106805731600019058631951255795316571242969336763938805465676269140733371287244624066632153110685509892188900004952700111937292221969
c = 19073695285772829730103928222962723784199491145730661021332365516942301513989932980896145664842527253998170902799883262567366661277268801440634319694884564820420852947935710798269700777126717746701065483129644585829522353341718916661536894041337878440111845645200627940640539279744348235772441988748977191513786620459922039153862250137904894008551515928486867493608757307981955335488977402307933930592035163126858060189156114410872337004784951228340994743202032248681976932591575016798640429231399974090325134545852080425047146251781339862753527319093938929691759486362536986249207187765947926921267520150073408188188
e = 0x10001

mod = 1<<265#即2^265
pl = n*inverse_mod(h2, mod) % mod
pbar = (h1 << 724) + pl

至此可以算出p的低265位,还有724-265=459位未知,但是coppersmith定理至少要求未知位数小于454,所以还有5位未知,可以爆破

完整sage代码如下:

from Crypto.Util.number import long_to_bytes

h1 = 1514296530850131082973956029074258536069144071110652176122006763622293335057110441067910479
h2 = 40812438243894343296354573724131194431453023461572200856406939246297219541329623
n = 21815431662065695412834116602474344081782093119269423403335882867255834302242945742413692949886248581138784199165404321893594820375775454774521554409598568793217997859258282700084148322905405227238617443766062207618899209593375881728671746850745598576485323702483634599597393910908142659231071532803602701147251570567032402848145462183405098097523810358199597631612616833723150146418889589492395974359466777040500971885443881359700735149623177757865032984744576285054725506299888069904106805731600019058631951255795316571242969336763938805465676269140733371287244624066632153110685509892188900004952700111937292221969
c = 19073695285772829730103928222962723784199491145730661021332365516942301513989932980896145664842527253998170902799883262567366661277268801440634319694884564820420852947935710798269700777126717746701065483129644585829522353341718916661536894041337878440111845645200627940640539279744348235772441988748977191513786620459922039153862250137904894008551515928486867493608757307981955335488977402307933930592035163126858060189156114410872337004784951228340994743202032248681976932591575016798640429231399974090325134545852080425047146251781339862753527319093938929691759486362536986249207187765947926921267520150073408188188
e = 0x10001

mod = 2**265
pl = n*inverse_mod(h2, mod) % mod
pbar = (h1 << 724) + pl

PR.<x> = PolynomialRing(Zmod(n))

for i in range(64):
    f = pbar + (x*mod*64) + (i<<265)
    f = f.monic()
    pp = f.small_roots(X=2^453, beta=0.4)
    if pp: break

p = int(pbar + (pp[0]<<271) + (i<<265))
q = n // p
d = inverse_mod(e, (p-1)*(q-1))
print(long_to_bytes(power_mod(c, d, n)))

标签:高位,RSA,long,已知,265,print,import,mod,equiv
From: https://www.cnblogs.com/lcjingyi/p/18035627

相关文章

  • 【国产化】禁止使用不安全的密码算法:DES、RC2,RSA(1024位及以下),MD5,SHA1
    一、引言随着互联网的普及和技术的发展,网络安全问题日益严重。密码算法作为网络安全的基石,其安全性直接关系到用户数据的安全。一些不安全的密码算法不断被曝光,给用户带来了极大的安全隐患。二、不安全的密码算法1.DESDES(DataEncryptionStandard)是一种对称加密算法,自1977年......
  • The 2023 ICPC Asia Jinan Regional Contest (The 2nd Universal Cup. Stage 17: Jina
    Preface趁着开学前最后一天再凑一场训练,今天这场手感不错前面的题都是一遍过最后靠着前期的手速7题苟进Au区,后面90min徐神B题没有Rush出来,思路啥都是对的就是一点细节没写好A.ManyManyHeads首先发现我们可以将所有相邻的同类型括号设为一对,这样一定能得出一个合法的串考......
  • Accurately computing running variance —— 已知两个数列各自的均值和方差,如何快速
    原内容来自:https://www.johndcook.com/blog/standard_deviation/计算公式:该种计算方式可以只保存历史数据的平方和,与历史数据的和。相关前文:已知两个数列各自的均值和方差,如何快速求出两个数列拼合后的均值和方差......
  • 已知两个数列各自的均值和方差,如何快速求出两个数列拼合后的均值和方差
    问题:数列A为[1,2,3,4,5,6,7,8,9],已知数列A的均值和方差和个数为mean_x,var_x,size_x数列B为[20,21,22,23,24,25,26,27,28,29],已知数列B的均值和方差和个数为mean_y,var_y,size_y现在将数列A与数列B拼合为数列Z,则数列Z为[1,2,3,4,5,6,7,8,9,20,......
  • Java RSA 加解密工具类,直接用
    importorg.junit.Test;importjavax.crypto.Cipher;importjavax.crypto.NoSuchPaddingException;importjava.io.ByteArrayOutputStream;importjava.nio.charset.Charset;importjava.nio.charset.StandardCharsets;importjava.security.*;importjava.security.i......
  • 【论文随笔】会话推荐系统综述(A Survey on Conversational Recommender Systems)
    前言今天读的论文为一篇于2021年5月发表在《ACM计算机调查》(ACMComputingSurveys)的论文,文章提供了对话式推荐系统(CRS)的全面综述,探讨了CRS的定义、概念架构、交互方式、知识元素、计算任务以及评估方法。文章还讨论了CRS在不同应用环境中的实现,如智能家居助手和聊天机器人,并指......
  • C# 与JAVA 的RSA 加密解密交互,C#使用BouncyCastle来实现私钥加密,公钥解密的方法
    因为一般C#的RSA加密解密都是公钥加密,私钥解密,没有私钥加密,公钥解密。在网上查了很多资料,终于看到有个博主的分享,关于私钥加密,公钥解密的解决方案,非常感谢(最下面有源网址)。此处就把简单应用的源码附上,需要的自己去完善。 1、私钥加密,公钥解密的源码usingOrg.BouncyCastle.......
  • QT使用OpenSSL的接口实现RSA2的签名和验签
    QT使用OpenSSL的接口实现RSA2的签名和验签加密和签名在RSA加密算法中是两个不同的概念,虽然它们都涉及RSA密钥对的使用,但目的和应用场景有所不同。加密(encrypt/decrypt):加密:使用接收方的公钥对数据进行加密,只有拥有相应私钥的接收方才能解密数据。解密:使用接收方的私钥对......
  • 遇到过的rsa解题总结
    rsa证明c=m**emodnm=c**dmodn将式1带入式2 得 m = (m ^ e % N ) ^ d % N需要证明:m == ( m ^ e % N ) ^ d % N(me%N)d%N=>  (me)d%N #模运算 a ^ b % p = ((a % p) ^ b) % pm^(e*d)%N #幂的乘方,底数不变,指数相乘将 e * d......
  • [MRCTF2020]Easy_RSA
    [MRCTF2020]Easy_RSA首先,RSA计算的5个基本公式n=pqφ(n)=(p-1)(q-1)求φ(n)e*dmodφ(n)=1求ed其中之一c=m^emodn加密m=c^dmodn解密题目:importsympyfromgmpy2importgcd,invertfromrandomimportrandintfromCrypto.Util.numberimportgetPrime,is......