• 2022祥云杯---Crypto


    水平太烂,只能搞出来一道题

    little little fermat

    题目:

    1. from Crypto.Util.number import *
    2. from random import *
    3. from libnum import *
    4. import gmpy2
    5. from secret import x
    6. flag = b'?????????'
    7. m = bytes_to_long(flag)
    8. def obfuscate(p, k):
    9. nbit = p.bit_length()
    10. while True:
    11. l1 = [getRandomRange(-1, 1) for _ in '_' * k]
    12. l2 = [getRandomRange(100, nbit) for _ in '_' * k]
    13. l3 = [getRandomRange(10, nbit//4) for _ in '_' * k]
    14. l4 = [getRandomRange(2, 6) for _ in '_' *k]
    15. A = sum([l1[_] * 2 ** ((l2[_]+l3[_])//l4[_]) for _ in range(0, k)])
    16. q = p + A
    17. if isPrime(q) * A != 0:
    18. return q
    19. p = getPrime(512)
    20. q = obfuscate(p, 5)
    21. e = 65537
    22. n = p*q
    23. print(f'n = {n}')
    24. assert 114514 ** x % p == 1
    25. m = m ^ (x**2)
    26. c = pow(m, e, n)
    27. print(f'c = {c}')
    28. '''
    29. n = 141321067325716426375483506915224930097246865960474155069040176356860707435540270911081589751471783519639996589589495877214497196498978453005154272785048418715013714419926299248566038773669282170912502161620702945933984680880287757862837880474184004082619880793733517191297469980246315623924571332042031367393
    30. c = 81368762831358980348757303940178994718818656679774450300533215016117959412236853310026456227434535301960147956843664862777300751319650636299943068620007067063945453310992828498083556205352025638600643137849563080996797888503027153527315524658003251767187427382796451974118362546507788854349086917112114926883
    31. '''

    题目里的fermat暗示与费马定理有关。看p、q的生成过程,可以大概猜出pq是相近的,至于obfuscate里while循环里的内容看不太懂。用yafu工具试着分解一下n,就得到了。再用基础方法解rsa,得到m。

    再看这句, 

    assert 114514 ** x % p == 1

    114514^{x} modp\equiv 1 ==》114514^{x} \equiv 1modp

    由费马定理可以推出x = p-1,故把上面得到的m与(p-1)^{2}异或一下即为最终flag,总体代码如下:

    1. from gmpy2 import *
    2. from Crypto.Util.number import *
    3. e = 65537
    4. n = 141321067325716426375483506915224930097246865960474155069040176356860707435540270911081589751471783519639996589589495877214497196498978453005154272785048418715013714419926299248566038773669282170912502161620702945933984680880287757862837880474184004082619880793733517191297469980246315623924571332042031367393
    5. c = 81368762831358980348757303940178994718818656679774450300533215016117959412236853310026456227434535301960147956843664862777300751319650636299943068620007067063945453310992828498083556205352025638600643137849563080996797888503027153527315524658003251767187427382796451974118362546507788854349086917112114926883
    6. p = 11887853772894265642834649929578157180848240939084164222334476057487485972806971092902627112665734648016476153593841839977704512156756634066593725142934001
    7. q = 11887853772894265642834649929578157180848240939084164222334476057487485972806971092902627112665734646483980612727952939084061619889139517526028673988305393
    8. d = invert(e,(p-1)*(q-1))
    9. m = pow(c,d,n)
    10. flag = m ^ (p-1)**2
    11. print(long_to_bytes(flag))

     

  • 相关阅读:
    Java 数据结构总结
    【k8s】pod调度——亲和,反亲和,污点,容忍
    java计算机毕业设计小区车辆管理系统源程序+mysql+系统+lw文档+远程调试
    IP网络广播景区广播广播系统
    贴纸拼词 —— 记忆化搜索 / 状压DP
    【Vue项目】通过设置全局的异常处理来统一处理后端返回的异常
    每日编程——295期:试密码
    OpenLDAP开启MemberOf及配置主从
    (附源码)spring boot学业指导系统 毕业设计 030958
    学长教你学C-day3-C语言的输入与输出之scanf()函数
  • 原文地址:https://blog.csdn.net/Luiino/article/details/127746598