lilac 五一欢乐赛 crypto 部分 writeups

作者: zsh2517 分类: 未分类 发布时间: 2020-05-04 10:40

本部分的部分实现见 lalic_crypto @ colab(需要科学上网)

下面很多东西都可以用 cyberChef

不神必的 base64

来解码吧!
ZmxhZ3tiYXNlNjRfaXNfdXNlZF9ldmVyeXdoZXJlfQ==
hint:对于古典密码学,有一大堆在线工具可以用,但我们推荐 CyberChef. 用过的都说好!

import base64
# 不神秘的 base64
s = "ZmxhZ3tiYXNlNjRfaXNfdXNlZF9ldmVyeXdoZXJlfQ=="
print(base64.b64decode(s).decode("utf-8"))
# flag: flag{base64_is_used_everywhere}

神秘代码

以下数字就是我们的 flag. 你能把它变成字符串吗?
10786438895798227883161514210116248549506979726714263760481486728317
hint:先把十进制转十六进制,然后每两个十六进制码就可以表示一个字符了。例如字符 ‘0’ 的 ascii 码是 0x30.

# 神秘代码
x = 10786438895798227883161514210116248549506979726714263760481486728317
x = "%X" % x
print(x)
s = ""
for i in range(0, len(x), 2):
    s += chr(int("0x" + x[i:i+2], 16))
print(s)
# 这里也可以用cyberchef的 from HEX
# flag: flag{hex_encoding_is_useful}

rail fence

f{lniosoelgri_ec_sS_ayt_ra}aafe_E_Bk
hint1:别信百度百科,要信wikipedia。
hint2:CyberChef 对您可能有帮助。

参考 wikipedia Rail fence cipher

这里可以直接在 cyberChef 找到,直接试一下,key=3, offset=0
f{lniosoelgri_ec_sS_ayt_ra}aafe_E_Bk => flag{rail_fence_is_So_Easy_to_Break}

咕咕咕: 后面写一下实现试试

P.S. 比如 1234567 同样加密解密不一样?

王 室 复 辟

凯撒大帝复辟了。他在 21 世纪仍然坚持采用古典密码学,属实是开历史的倒车。
以下是他发出的一则信息,请解密吧。
synt{Pnrfne_Hfvat_Bssfrg_guvegrra_vf_Pnyyrq_EBG}

a = "synt{Pnrfne_Hfvat_Bssfrg_guvegrra_vf_Pnyyrq_EBG}"
def mvnext(x, i):
    if x >='a' and x <='z':
        x = ord(x) + i
        if(x > ord('z')):
            x-=26
        x = chr(x)
        return x
    if x >='A' and x <='Z':
        x = ord(x) + i
        if(x > ord('Z')):
            x-=26
        x = chr(x)
        return x
    return x
ori = "synt{Pnrfne_Hfvat_Bssfrg_guvegrra_vf_Pnyyrq_EBG}"
for i in range(0, 26):
    b = [mvnext(x, i) for x in ori]
    c = "".join(b)
    print(c)
# 凯撒加密就是位移加密,重新移动一下就行,大不了就是26个都试一下,找到 flag 开头的
# flag{Caesar_Using_Offset_thirteen_is_Called_ROT}
synt{Pnrfne_Hfvat_Bssfrg_guvegrra_vf_Pnyyrq_EBG}
tzou{Qosgof_Igwbu_Cttgsh_hvwfhssb_wg_Qozzsr_FCH}
uapv{Rpthpg_Jhxcv_Duuhti_iwxgittc_xh_Rpaats_GDI}
vbqw{Squiqh_Kiydw_Evviuj_jxyhjuud_yi_Sqbbut_HEJ}
wcrx{Trvjri_Ljzex_Fwwjvk_kyzikvve_zj_Trccvu_IFK}
xdsy{Uswksj_Mkafy_Gxxkwl_lzajlwwf_ak_Usddwv_JGL}
yetz{Vtxltk_Nlbgz_Hyylxm_mabkmxxg_bl_Vteexw_KHM}
zfua{Wuymul_Omcha_Izzmyn_nbclnyyh_cm_Wuffyx_LIN}
agvb{Xvznvm_Pndib_Jaanzo_ocdmozzi_dn_Xvggzy_MJO}
bhwc{Ywaown_Qoejc_Kbboap_pdenpaaj_eo_Ywhhaz_NKP}
cixd{Zxbpxo_Rpfkd_Lccpbq_qefoqbbk_fp_Zxiiba_OLQ}
djye{Aycqyp_Sqgle_Mddqcr_rfgprccl_gq_Ayjjcb_PMR}
ekzf{Bzdrzq_Trhmf_Neerds_sghqsddm_hr_Bzkkdc_QNS}
flag{Caesar_Using_Offset_thirteen_is_Called_ROT}
gmbh{Dbftbs_Vtjoh_Pggtfu_uijsuffo_jt_Dbmmfe_SPU}
hnci{Ecguct_Wukpi_Qhhugv_vjktvggp_ku_Ecnngf_TQV}
iodj{Fdhvdu_Xvlqj_Riivhw_wkluwhhq_lv_Fdoohg_URW}
jpek{Geiwev_Ywmrk_Sjjwix_xlmvxiir_mw_Geppih_VSX}
kqfl{Hfjxfw_Zxnsl_Tkkxjy_ymnwyjjs_nx_Hfqqji_WTY}
lrgm{Igkygx_Ayotm_Ullykz_znoxzkkt_oy_Igrrkj_XUZ}
mshn{Jhlzhy_Bzpun_Vmmzla_aopyallu_pz_Jhsslk_YVA}
ntio{Kimaiz_Caqvo_Wnnamb_bpqzbmmv_qa_Kittml_ZWB}
oujp{Ljnbja_Dbrwp_Xoobnc_cqracnnw_rb_Ljuunm_AXC}
pvkq{Mkockb_Ecsxq_Yppcod_drsbdoox_sc_Mkvvon_BYD}
qwlr{Nlpdlc_Fdtyr_Zqqdpe_estceppy_td_Nlwwpo_CZE}
rxms{Omqemd_Geuzs_Arreqf_ftudfqqz_ue_Omxxqp_DAF}

flag: flag{Caesar_Using_Offset_thirteen_is_Called_ROT}

点横点横横

你收到了一封电报:
-- --- .-. ... . .. ... ... --- -. .. -.-. .
请包上 flag{} 提交。本题大小写不敏感。

莫尔斯电码,从网上搜索一个解密就行

flag: flag{MORSEISSONICE}

单表替换

以下是经过了单表替换的信息,原文是一段家喻户晓的英文。

wbak txbkv idj tvnvd zvikt ilb bak wioyvkt skbalyo wbkoy bd oyct xbdocdvdo, i dvr diocbd, xbdxvcnvj cd pcsvkoz, idj jvjcxiovj ob oyv fkbfbtcocbd oyio ipp hvd ikv xkviovj vgaip.
dbr rv ikv vdlilvj cd i lkvio xcncp rik, ovtocdl ryvoyvk oyio diocbd, bk idz diocbd tb xbdxvcnvj idj tb jvjcxiovj, xid pbdl vdjakv. rv ikv hvo bd i lkvio sioopv-wcvpj bw oyio rik. rv yinv xbhv ob jvjcxiov i fbkocbd bw oyio wcvpj, it i wcdip kvtocdl fpixv wbk oybtv ryb yvkv linv oyvck pcnvt oyio oyio diocbd hclyo pcnv. co ct ipoblvoyvk wcoocdl idj fkbfvk oyio rv tybapj jb oyct.
sao, cd i piklvk tvdtv, rv xid dbo jvjcxiov—rv xid dbo xbdtvxkiov—rv xid dbo yippbr—oyct lkbadj. oyv skinv hvd, pcncdl idj jvij, ryb tokallpvj yvkv, yinv xbdtvxkiovj co, wik isbnv bak fbbk fbrvk ob ijj bk jvokixo. oyv rbkpj rcpp pcoopv dbov, dbk pbdl kvhvhsvk ryio rv tiz yvkv, sao co xid dvnvk wbklvo ryio oyvz jcj yvkv. co ct wbk at oyv pcncdl, kioyvk, ob sv jvjcxiovj yvkv ob oyv adwcdctyvj rbke rycxy oyvz ryb wbalyo yvkv yinv oyat wik tb dbspz ijnidxvj. co ct kioyvk wbk at ob sv yvkv jvjcxiovj ob oyv lkvio oite kvhicdcdl svwbkv at—oyio wkbh oyvtv ybdbkvj jvij rv oiev cdxkvitvj jvnbocbd ob oyio xiatv wbk rycxy oyvz linv oyv pito wapp hvitakv bw jvnbocbd—oyio rv yvkv yclypz kvtbpnv oyio oyvtv jvij tyipp dbo yinv jcvj cd nicd—oyio oyct diocbd, adjvk lbj, tyipp yinv i dvr sckoy bw wkvvjbh—idj oyio lbnvkdhvdo bw oyv fvbfpv, sz oyv fvbfpv, wbk oyv fvbfpv, tyipp dbo fvkcty wkbh oyv vikoy.
wpil ct wpil{pcpix.ct.wbk.oyv.toajvdot}

hint 是 善用工具,但是没找到什么工具,手动做一下算了…

首先输入数据

# 单表替换,善用工具但是没找到...代码有下面几部分组成
s = """wbak txbkv idj tvnvd zvikt ilb bak wioyvkt skbalyo wbkoy bd oyct xbdocdvdo, i dvr diocbd, xbdxvcnvj cd pcsvkoz, idj jvjcxiovj ob oyv fkbfbtcocbd oyio ipp hvd ikv xkviovj vgaip.
dbr rv ikv vdlilvj cd i lkvio xcncp rik, ovtocdl ryvoyvk oyio diocbd, bk idz diocbd tb xbdxvcnvj idj tb jvjcxiovj, xid pbdl vdjakv. rv ikv hvo bd i lkvio sioopv-wcvpj bw oyio rik. rv yinv xbhv ob jvjcxiov i fbkocbd bw oyio wcvpj, it i wcdip kvtocdl fpixv wbk oybtv ryb yvkv linv oyvck pcnvt oyio oyio diocbd hclyo pcnv. co ct ipoblvoyvk wcoocdl idj fkbfvk oyio rv tybapj jb oyct.
sao, cd i piklvk tvdtv, rv xid dbo jvjcxiov—rv xid dbo xbdtvxkiov—rv xid dbo yippbr—oyct lkbadj. oyv skinv hvd, pcncdl idj jvij, ryb tokallpvj yvkv, yinv xbdtvxkiovj co, wik isbnv bak fbbk fbrvk ob ijj bk jvokixo. oyv rbkpj rcpp pcoopv dbov, dbk pbdl kvhvhsvk ryio rv tiz yvkv, sao co xid dvnvk wbklvo ryio oyvz jcj yvkv. co ct wbk at oyv pcncdl, kioyvk, ob sv jvjcxiovj yvkv ob oyv adwcdctyvj rbke rycxy oyvz ryb wbalyo yvkv yinv oyat wik tb dbspz ijnidxvj. co ct kioyvk wbk at ob sv yvkv jvjcxiovj ob oyv lkvio oite kvhicdcdl svwbkv at—oyio wkbh oyvtv ybdbkvj jvij rv oiev cdxkvitvj jvnbocbd ob oyio xiatv wbk rycxy oyvz linv oyv pito wapp hvitakv bw jvnbocbd—oyio rv yvkv yclypz kvtbpnv oyio oyvtv jvij tyipp dbo yinv jcvj cd nicd—oyio oyct diocbd, adjvk lbj, tyipp yinv i dvr sckoy bw wkvvjbh—idj oyio lbnvkdhvdo bw oyv fvbfpv, sz oyv fvbfpv, wbk oyv fvbfpv, tyipp dbo fvkcty wkbh oyv vikoy.
wpil ct wpil{pcpix.ct.wbk.oyv.toajvdot}"""

之后处理词频

s = s.lower()
origin_count = []
total = 0
d = {}
for i in range(0, 26):
    char = chr(i + ord('a'))
    origin_count.append(len(s) - len(s.replace(char, "")))
    total += origin_count[i-1]
origin_fqnc = [x/total for x in origin_count]
for i in range(0, 26):
    char = chr(i + ord('a'))
    d[char] = origin_fqnc[i]
print("原来的频率" , origin_fqnc)
print(d)

建立一个词频的表,先用标准词频写一个,然后对照结果不断修正。等到还原出来几个单词的时候,就可以整篇文章去搜索引擎搜了
原来是竖着的,可以直接上下调整顺序方便,为了展示调成横过来的了

map_method = {
    "y":"h", "c":"i", "i":"a", "p":"l", "x":"c",
    "t":"s", "o":"t", "v":"e", "a":"u", "n":"v",
    "d":"n", "k":"r", "j":"d", "z":"y", "s":"b",
    "g":"q", "h":"m", "f":"p", "w":"f", "u":"x",
    "r":"w", "q":"j", "m":"z", "l":"g", "e":"k", 
    "b":"o",
}

打印结果

l = [ord(x) for x in s]
for key in map_method:
    for index in range(0, len(l)):
        if l[index] == ord(key):
            l[index] = map_method[key]

for index in range(0, len(l)):
    if type(l[index]) == type(1):
        l[index] = chr(l[index])

"".join(l)

最后一句话是flag flag is flag{lilac.is.for.the.students}'

flag: flag{lilac.is.for.the.students}

CH₃COOH

Owaczoe gl oa yjirmng fmeigghb bd tqrrbq nabr nlw heyvs pfxavatzf raog ktm vlvzhbx tyyocegguf.
Tbbretf gwiwpyezl ahbgybbf dbjr rh sveah cckqrlm opcmwp yvwq zr jbjnar.
Slinjem gfx opcmwp yvwq gl demwipcw pl ras sckarlmogghb bd xhuygcy mk ghetff zr opcmwp yvwq ztqgckwn.
Rasec tfr ktbl rrdrq ht iggstyk, rrnxbqggu bl lchpvs zymsegtzf.
Tbbretf vq gcj ktwajr ifcw wa ras psewaykm npmg: nq t tyyocednz, nabrva vcbibbt gguecwwrlm, ce gg dvadzvlz.
Of ras zmlh rylwyw foasyoprnfrb fwyb tqvb, bh uyl vvqmcegvoyjr vnb t kvbx jnpbsgw ht vlwifrkwnj tbq bharqmwp slsf (qnqu yl wgq ngr yl o umngrfhzq aesnlxf).
Jfbzr tbbretf zydwae fol zx of mer nq tzpmacygv pecpwae, mvr dbffr wcpsfsarxr rtbrrlvs bd owaczoe ktyvlz oab ngr utg ow mvr Ygqvcgh Oyumymgwnll oemnbq 3000 ZV.
Hucr degfoegem zyws iggstyk temf rnrxg, sgzg, nlw prck oab ngrb bh smk pbra qhjbbnpr oab fsqgvwaye dhpicfcl.
Heyvsf my wg yegb ftjr zxsa dhiab bb Rerdggtb hpgg.
Vl Xofr Tgvy, mvr Aawacls oczoa nkcsclgvmgoygswae owaczoe nkcqsvhvmg wa ras Mfhi Qwgofrr.
Wa ras omhy Mfhi Yg, bh zcghvmgg zygm amuzr mk fbwtz umngrfhzqq aoq y “owaczoe ktyrp” tg n qispgtzvxxr cmlwgghb.
Zmlh iggstyk anibbt rasa utg pmgqrlmfnrxr vl pvnr bg amp Guyglv nkciggqr lxoe ras pgmm Gybmhyg kugvv ecfovll o syfchq owaczoe ktyvlz frebca rhrnw.
Foaw Vvvlxgr tbbretff ygr gfxwe slsf dhf psewaykm nlw arbbqvltz cskdbqxg jcks jpbhgcg rbug wa ras nekwpsehhptz zyginj Jwzgg Mnmlvh.
pmqc{tbbretf_bl_fm_sglv_nlw_qugig_cjxofc}

hint1:CH₃COOH 是什么日常用品的主要有效成分?它的英文名是什么?
hint2:善用工具。
hint3:本题摘录自某场比赛,flag并不是 flag{} 的格式。CTF 比赛的 flag 经常以比赛名称作为前缀。

writeup: None

三天之内

【孙某的日记】
今天晚上,很好的月光。
有人说,三天之内杀了我。哼,他三天之内能杀谁,自己心里没点 b 数吗?
2020年4月25日
【新闻报道】
平安Lilac电台 4月27日电 昨日,东京某地发生杀人案,手段极其残忍。杀手行凶之后,还把受害者的骨灰扬了。无数人目睹了骨灰于寒风中飘散;在深蓝色天空的映衬下,纷纷扬扬的骨灰有如万朵樱花。
杀手非常嚣张,留下了一段密文flag.enc和一个用于加密的程序encryptor.py,应该是行凶之后运行这个程序,输出密文。警方推测,凶手在运行程序时,还有一个secret.py,但现在已经被销毁。
现发出布告,向全社会征集密码学高手,破解杀手留下的信息。
hint0:即使您不了解AES加密,也可以把这题做出来。
hint1:目前没有很好的攻击AES的手段,所以不要考虑直接攻击密文。
hint2:虽然不要直接攻击密文,但还是需要一点爆破的。
hint3:加密程序使用的Crypto库是 pycryptodome,而不是crypto或pycrypto. 这是现代密码学的必备库,建议使用pip或conda安装。
附件下载: encryptor.py flag.enc

writeup: None

神必 base64

窃格瓦拉出狱后,跑去窃了一段base64编码后的数据。很不巧,这个base64所使用的密码表与我们平时用的 A-Za-z0-9+/= 并不一样。
我们帮你搞到了明文的前几段。你能恢复出整个密文吗?
本文有两个文件。cipher.txt 为密文;plain.txt 为明文的前几段。
hint1:Cyberchef 可以方便地进行自定义密码表的base64编码,有助于您熟悉这种编码方式。
hint2:产生cipher.txt时,回车是LF风格的。如果您不知道这是什么意思,请查阅相关资料。
hint3:您可能无法恢复出完整的密码表,但这不影响拿到flag。
附件下载: plain.txt cipher.txt

# 神必 b64
# 处理源文本,生成 01 串
bincode = ""
# file_origin = open("plain.txt", "r")
# text = file_origin.read()
text = """We set sail on this new sea because there is new knowledge to be gained, and new rights to be won, and they must be won and used for the progress of all people. For space science, like nuclear science and all technology, has no conscience of its own. Whether it will become a force for good or ill depends on man, and only if the United States occupies a position of pre-eminence can we help decide whether this new ocean will be a sea of peace or a new terrifying theater of war. I do not say that we should or will go unprotected against the hostile misuse of space any more than we go unprotected against the hostile use of land or sea, but I do say that space can be explored and mastered without feeding the fires of war, without repeating the mistakes that man has made in extending his writ around this globe of ours.
There is no strife, no prejudice, no national conflict in outer space as yet. Its hazards are hostile to us all. Its conquest deserves the best of all mankind, and its opportunity for peaceful cooperation may never come again. But why, some say, the Moon? Why choose this as our goal? And they may well ask, why climb the highest mountain? Why, 35 years ago, fly the Atlantic? Why does Rice play Texas?
We choose to go to the Moon! We choose to go to the Moon...We choose to go to the Moon in this decade and do the other things, not because they are easy, but because they are hard; because that goal will serve to organize and measure the best of our energies and skills, because that challenge is one that we are willing to accept, one we are unwilling to postpone, and one we intend to win, and the others, too."""
for i in text:
    b = bin(ord(i))[2:]
    bincode += '0' * (8 - len(b)) + b
# 处理 b64 代码
# file_b64 = open("cipher.txt", "r")
# target = file_b64.read()
target = "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"
j = 0
# 二者匹配
mmap = {}
for i in range(0, len(bincode), 6):
    b64_ori = bincode[i:i+6]
    if len(b64_ori) < 6:
        b64_ori = b64_ori + '0' * (6 - len(b64_ori))
    # print(b64_ori, target[j])
    mmap[b64_ori] = target[j]
    j+=1
for i in range(0, 64):
    b = bin(i)[2:]
    b = '0' * (6 - len(b)) + b
    # print(b)
    if b not in mmap.keys():
        mmap[b] = "*"
        # print(b)
# 输出 b64 信息
# f_out = open("output", "w")
lines = []
for i in mmap.keys():
    lines.append("%s %s\n" % (i, mmap[i]))
lines = sorted(lines)
# f_out.writelines(lines)
# 写出映射关系
b64map = "".join([x[-2] for x in lines])
# f_out.write(b64map)
print("use the following to decrypt\n" + b64map)
# 写出直接在cyberChef上输出的内容
# f.write(str(mmap))
f_out.close()

程序输出

use the following to decrypt
*T7MHGUxCX+cipgL2fnPzNR0V6Y1lKQ*uJo/ZIj8tq43aFBsODE5mAehbw**rS*d

用下面的这个作为 b64 的文本编码表,放到 cyberchef 上面, base64 decode 一下即可

flag: flag{WelcomeToLilacLetsGoToTheMoon}

RSA-0

请查阅资料,学习RSA算法,然后解密:
p = 190200647956112783535765008317161963047
q = 285665635359057201725659119374017317427
e = 65537
c = 2404704211304008728001190278809748948299133006732012442286327326548630531233
hint:您在做本题之前,一定做了“神秘代码”。用那道题的方式,把本题的结果转化为字符串。

wikipedia 关于 RSA 的介绍

操作
公钥与私钥的产生
假设Alice想要通过一个不可靠的媒体接收Bob的一条私人消息。她可以用以下的方式来产生一个公钥和一个私钥:
随意选择两个大的素数pqp不等于q,计算N=pq
根据[[欧拉函数]],求得r=\varphi (N) = \varphi (p)\varphi (q)=(p-1)(q-1)
选择一个小于r的整數e,使er互质。并求得e关于r的[[模反元素]],命名为d(求ded \equiv 1 \pmod{r})。(模反元素存在,当且仅当er互质)
pq的记录销毁。
(N,e)是公钥,(N,d)是私钥。Alice将她的公钥(N,e)传给Bob,而将她的私钥(N,d)藏起來。
加密消息
假设Bob想给Alice送一个消息m,他知道Alice产生的Ne。他使用起先与Alice约好的格式将m转换为一个小于N的非负整数n,比如他可以将每一个字转换为这个字的[[Unicode]]码,然后将这些数字连在一起组成一个数字。假如他的信息非常长的话,他可以将这个信息分为几段,然后将每一段转换为n。用下面这个公式他可以将n加密为c
:c \equiv n^e \pmod{N}
计算c并不复杂。Bob算出c后就可以将它传递给Alice。
解密消息
Alice得到Bob的消息c后就可以利用她的密钥d来解码。她可以用以下这个公式来将c转换为n
:n \equiv c^d\ (\mathrm{mod}\ N)
得到n后,她可以将原来的信息m重新复原。
解码的原理是
:c^d \equiv n^{e \cdot d}\ (\mathrm{mod}\ N)
已知ed \equiv 1 \pmod{r},即ed=1+h\varphi (N)。 由[[欧拉定理 (数论)|欧拉定理]]得:
:n^{ed} = n^{1 + h\varphi(N)} = n \left(n^{\varphi(N)}\right)^{h} \equiv n (1)^{h} \pmod{N} \equiv n \pmod{N}
转载:RSA算法(来自 Wikipedia

欧拉函数

首先 欧拉函数的计算,根据如下两个公式
对于两个质数p, q,有\phi(x) = (p – 1) \times (q – 1)
对于多个质数p1, p2…pi相乘,有\phi(x) = x \times \prod_{i=1}^{n}(1-\frac{1}{p_i})

# RSA 0
    # 针对两个素数的RSA运算
def euler_twoprime(x, y):
    return (x - 1) * (y - 1)
    #多个素数求欧拉函数,公式 phi(x) = x * \PI_{i=1}^{n}1/p_i

def euler_list (p):
    ans = 1
    for x in p:
        ans *= x
    for x in p:
        ans *= (x - 1)
        # print(ans)
    for x in p:
        ans //= x
    # for i in range(0, len(p)):
    #     ans = ans * (1 - 1 / p[i])
    return ans

求逆元和快速幂

之后求逆元和快速幂

def gcd(a,b):
        while a!=0:
            a,b = b%a,a
        return b

#定义一个函数,参数分别为a,n,返回值为b
def findModReverse(a,m):#这个扩展欧几里得算法求模逆
        if gcd(a,m)!=1:
            return None
        u1,u2,u3 = 1,0,a
        v1,v2,v3 = 0,1,m
        while v3!=0:
            q = u3//v3
            v1,v2,v3,u1,u2,u3 = (u1-q*v1),(u2-q*v2),(u3-q*v3),v1,v2,v3
        return u1%m
def poww(base,exp,mod):
    base=base%mod
    ans=1
    while exp!=0:
        if exp&1:
            ans=(ans*base)%mod
        exp>>=1
        base=(base*base)%mod
    return ans

求解问题

# RSA-0
p = 190200647956112783535765008317161963047
q = 285665635359057201725659119374017317427
e = 65537
c = 2404704211304008728001190278809748948299133006732012442286327326548630531233
n = p * q
r = euler_twoprime(p, q)
d = findModReverse(e, r)
d
e * d % r
n = poww(c, d, n)
# n = 10786438895798227883161514210116248549506979726714263760481486728317
print(dec2str(n)) # 这个 十进制 转 字符串 前面定义过了

直接输出 flag{lilac_RSA_is_so_Easy}

RSA-1

既然你已经写完了RSA-0,那么来看看它的变种吧!
加密程序已经给出。
附件(因为很短,所以不传附件了)

# RSA-1.py
p = 252647779892687905173761792949656998433
q = 290615416181922737045361451171930371659
r = 281613259213037257262703439109757908501

n = p * q * r
e = 0x10001
print(pow(flag, e, n))
# 1169612223485519024207841670191078798101684935551461601922416127588930439758194701318838707953651437973827125265577
p = 252647779892687905173761792949656998433
q = 290615416181922737045361451171930371659
r = 281613259213037257262703439109757908501
l = [p, q, r]
n = p * q * r
r = euler_list(l)
# flag = 123
e = 65537
d = findModReverse(e, r)
enc = 1169612223485519024207841670191078798101684935551461601922416127588930439758194701318838707953651437973827125265577
dec = pow(enc, d, n)
print(dec2str(dec))
# 1169612223485519024207841670191078798101684935551461601922416127588930439758194701318838707953651437973827125265577

在前面的函数的基础上,换成三个数即可。输出 flag

flag: flag{euler_function_is_So_Interesting}

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

一条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注

标签云