ctfshow crypto 红包四wp

from secrets import randbelow
from nationalsecret import p, r, k, flag

g = 2
y = pow(g, k, p)

def gogogo():
    print("Another chance:")
    t = int(input('t = '))
    c = randbelow(p)
    print("Here is my real challenge:")
    print(f'c = {c}')
    print("Give me your answer.")
    s = int(input('s = '))
    return pow(g, s, p) == t * pow(y, c, p) % p

def dododo():
    print("Here is my gift for National's Day!")
    t = pow(g, r, p)
    print(f't = {t}')
    print("What do you want?")
    c = int(input('c = '))
    s = (r + c*k) % (p - 1)
    print("This is another gift:")
    print(f's = {s}')

print("Happy National's Day!")
print("Don't play CTF these days.")
print("Just go out and play~")
print("Don't finish this chall,")
print("although I should tell you that")
print(f"p = {p}")


dododo()

if gogogo():
    print(f"FUIYOH! You can get flag: {flag}")
    print("and then go out and play!")
else:
    print("HAIYAA! You are wrong. Just go out and play!")

以上为加密代码,再gogogo这段代码里,只要return的结果为真,也就是

pow(g, s, p) == t * pow(y, c, p) % p  的时候,会返回flag,仔细审题,发现这其实是一道deffie-hellman的现代密码题目

再来回顾一下现代密码,假设有Alice 和 Bob两个人,各自生成一个a和b,求出A=pow(g,a,p)

和B=pow(g,b,p),则key=pow(A,b,p)=pow(B,a,p)

这里看看return后的内容,如果y相当于A,c相当于b,pow(y,c,p)相当于pow(B,a,p)那么前面的pow(g,s,p)就相当于pow(A,b,p)

已知y=pow(g,k,p),根据同余性质可知      (g^c%p)^k%p=g^c^k%p

 所以我们令t=1,s=ck。c给出来了,我们求k

dododo代码块就是让我们来求k的,首先我们让c=0,得到r=224,然后再运行一次这个代码,这一次让c=1,得到r+k=5551628413330011871077674605450526902698282945776482884041995095432548061898021904002453319462057630031351641808008288358761268961670548566575970336145746

得到k=5551628413330011871077674605450526902698282945776482884041995095432548061898021904002453319462057630031351641808008288358761268961670548566575970336145522

s=ck,输进去,得到

 我太强了!

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Paintrain

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值