【密码学RSA】rsa_p高位泄露(2021四川省数字创新赛题)

1.题目:

from Crypto.Util.number import getPrime, bytes_to_long
FLAG = b"flag{}"

def enc(m):
    return pow(m, e, N)

if __name__ == "__main__":    
    l = 256
    p = getPrime(1024)
    N = p * getPrime(1024)
    e = 65537
    a = (p >> l) << l
    print("N:", N)
    print("Known part of p:", hex(a))
    print("Length of the unknown part:", l)
    print("enc:", enc(bytes_to_long(FLAG)))


N: 13139369168613206469808493070119137888363636548621629780897948879328793540933675072448361493321304924953815474270401406259487525517560528123707016104942485164559271692275987380567766009184969340122208041180122234792566147648471202470677782205185423853314467362074540818483729953544353584322270414479260852672948012862257167187569701381652931473637503302338392147780573148724508117699531886205586281824118899931516823621049590863613262210219765105389989391065557707559113268724368695051264276619633555407916385088611885715568165460641318205321508100969473959719364829756492542217470309748646183210141490634293731384313
Known part of p: 0xce2f93251a3a97404a11c1fe88cf15c7aaf26ffd508ff006933bff2e9ea0c6197a98f1188f03b74b16d564e958a84c877fc0e21faf00f0ae42f26bde226ebf7c9732f17d860b81d139799832d510b91001967fc33ff2d9fbd4c4767fa2438e480000000000000000000000000000000000000000000000000000000000000000
Length of the unknown part: 256
enc: 10994040238462701583659972453101990790105813765012990749608185072718600854440721383239626137413394180017327728436373282101414910961527694291269438429124480262436140242346606299693570528165786128342639697068827177018329599990851910019879374047005450085901527704075107943071874274180276690059744636245418370546246757239666384320024728428669095536636012185174919921512858446990367609436305909104080544807406108497002947094974486271586520410674562299953931051491793506395053049529401503138822284281986730639405804678960905130223082038659899687410133538282139577181626031301078543719687909557541703207389570582210505090095

2.使用软件sagemath notbook,利用sage 计算p,q ,打开其网页版:

生成一个以x为符号的一元多项式环。
PR.<x> = PolynomialRing(Zmod(n))

定义求解的函数
f = x + p4

多项式小值根求解及因子分解
X:表示求解根的上界
roots = f.small_roots(X=2^kbits, beta=0.4)

脚本1:

def phase3(high_p, n):
    R.<x> = PolynomialRing(Zmod(n), implementation='NTL')
    p = high_p + x
    x0 = p.small_roots(X = 2^256, beta = 0.1)[0]
    
    P = int(p(x0))
    Q = n // P
    print(P)
    assert n == P*Q

n = 13139369168613206469808493070119137888363636548621629780897948879328793540933675072448361493321304924953815474270401406259487525517560528123707016104942485164559271692275987380567766009184969340122208041180122234792566147648471202470677782205185423853314467362074540818483729953544353584322270414479260852672948012862257167187569701381652931473637503302338392147780573148724508117699531886205586281824118899931516823621049590863613262210219765105389989391065557707559113268724368695051264276619633555407916385088611885715568165460641318205321508100969473959719364829756492542217470309748646183210141490634293731384313
p4= 0xce2f93251a3a97404a11c1fe88cf15c7aaf26ffd508ff006933bff2e9ea0c6197a98f1188f03b74b16d564e958a84c877fc0e21faf00f0ae42f26bde226ebf7c9732f17d860b81d139799832d510b91001967fc33ff2d9fbd4c4767fa2438e480000000000000000000000000000000000000000000000000000000000000000
e = 0x10001
phase3(p4, n)

脚本2:

from sage.all import *
n = 13139369168613206469808493070119137888363636548621629780897948879328793540933675072448361493321304924953815474270401406259487525517560528123707016104942485164559271692275987380567766009184969340122208041180122234792566147648471202470677782205185423853314467362074540818483729953544353584322270414479260852672948012862257167187569701381652931473637503302338392147780573148724508117699531886205586281824118899931516823621049590863613262210219765105389989391065557707559113268724368695051264276619633555407916385088611885715568165460641318205321508100969473959719364829756492542217470309748646183210141490634293731384313
p4=0xce2f93251a3a97404a11c1fe88cf15c7aaf26ffd508ff006933bff2e9ea0c6197a98f1188f03b74b16d564e958a84c877fc0e21faf00f0ae42f26bde226ebf7c9732f17d860b81d139799832d510b91001967fc33ff2d9fbd4c4767fa2438e48
e = 0x10001
pbits = 1024
kbits = pbits - p4.nbits()
print(p4.nbits())
p4 = p4 << kbits
PR.<x> = PolynomialRing(Zmod(n))
f = x + p4
roots = f.small_roots(X=2^kbits, beta=0.4)
if roots:        
    p = p4+int(roots[0])
    print ("n: ", n)   
    print ("p: ", p)
    print ("q: ", n/p)

3. 求出p之后,flag=pow(c,d,n)

import gmpy2
from Crypto.Util.number import *
n=13139369168613206469808493070119137888363636548621629780897948879328793540933675072448361493321304924953815474270401406259487525517560528123707016104942485164559271692275987380567766009184969340122208041180122234792566147648471202470677782205185423853314467362074540818483729953544353584322270414479260852672948012862257167187569701381652931473637503302338392147780573148724508117699531886205586281824118899931516823621049590863613262210219765105389989391065557707559113268724368695051264276619633555407916385088611885715568165460641318205321508100969473959719364829756492542217470309748646183210141490634293731384313
p=144788620032174382850620144740051190004909189981823947687939533751214863574416128745504241848556196436424873523723598270584585967337289044432722639171175692346268666992200205255923809991550657146510500317550922775903638618780099941385823936837216968003763133275736822717054686219559882653840804217844349691379
q=90748631803338033382719802077109017154157229824571563567405846296920929137783882558055978191525135923468682426958334699526354800278355091313071807697974640522544224693325158583809586604573129139663109910278125191334731044785721145359018060504935418728787028099072072663992579146767368168659319905098993800547
c=10994040238462701583659972453101990790105813765012990749608185072718600854440721383239626137413394180017327728436373282101414910961527694291269438429124480262436140242346606299693570528165786128342639697068827177018329599990851910019879374047005450085901527704075107943071874274180276690059744636245418370546246757239666384320024728428669095536636012185174919921512858446990367609436305909104080544807406108497002947094974486271586520410674562299953931051491793506395053049529401503138822284281986730639405804678960905130223082038659899687410133538282139577181626031301078543719687909557541703207389570582210505090095
e=65537
d=gmpy2.invert(e,(p-1)*(q-1))
flag=pow(c,d,n)
print(long_to_bytes(flag))

最后得出flag:flag{b082411e0d6e296d4598b69825fc0cc5}

 

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是Java实现RSA算法的示例代码: ```java import java.math.BigInteger; import java.security.SecureRandom; public class RSA { private BigInteger modulus; private BigInteger privateKey; private BigInteger publicKey; // 初始化RSA算法 public RSA(int bitLength) { SecureRandom random = new SecureRandom(); BigInteger p = BigInteger.probablePrime(bitLength / 2, random); BigInteger q = BigInteger.probablePrime(bitLength / 2, random); BigInteger phi = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE)); this.modulus = p.multiply(q); this.publicKey = new BigInteger("65537"); // 一般取65537 this.privateKey = this.publicKey.modInverse(phi); } // 加密明文 public BigInteger encrypt(BigInteger plaintext) { return plaintext.modPow(publicKey, modulus); } // 解密密文 public BigInteger decrypt(BigInteger ciphertext) { return ciphertext.modPow(privateKey, modulus); } public static void main(String[] args) { RSA rsa = new RSA(1024); BigInteger plaintext = new BigInteger("123456"); System.out.println("明文:" + plaintext); BigInteger ciphertext = rsa.encrypt(plaintext); System.out.println("密文:" + ciphertext); BigInteger decryptedText = rsa.decrypt(ciphertext); System.out.println("解密后的明文:" + decryptedText); } } ``` 在上述代码中,我们首先生成了一个RSA对象,并指定了密钥长度为1024位。接着,我们可以使用encrypt方法对明文进行加密,使用decrypt方法对密文进行解密。最后,我们在main方法中对一个简单的明文进行加密、解密并输出结果。 需要注意的是,RSA算法是一种公钥加密算法,因此我们只需要保护好私钥即可,公钥可以公开,不需要保密。在实际应用中,我们可以将公钥告知通信对方,让对方使用公钥对数据进行加密,然后再使用私钥进行解密。这样可以保证数据的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值