BUUCTF笔记之Crypto部分WriteUp(二)

12 篇文章 1 订阅

1、凯撒?替换?呵呵!

凯撒密码一般就是26个字母经过单纯的按字母顺序来位移的加密方法(一般)
如:abc=def
进阶版的凯撒就不按照字母顺序的加密,等于是一个字母打乱顺序,使用类似密码本的形式对应另一个字母。
所以就要经过暴力破解出每一种可能的对应加密。

代码不会写,这里使用工具:

quipqiup - cryptoquip and cryptogram solver

2、[MRCTF2020]天干地支+甲子

得到得字符串用MRCTF{}包裹
一天Eki收到了一封来自Sndav的信,但是他有点迷希望您来解决一下
甲戌
甲寅
甲寅
癸卯
己酉 
甲寅
辛丑

百度天干地支表:

根据顺序得到:

11
51
51
40
46
51
38

再加一甲子也就是加60得到:

71
111
111
100
106
111
98

这里我还以为是16进制转字符串,谁知道直接对着ascii码查就行。。。。

flag{Goodjob}

3、达芬奇密码

古典密码不精,现代密码不会,唉,我密码学实在是拉跨

题:达芬奇隐藏在蒙娜丽莎中的数字列:1 233 3 2584 1346269 144 5 196418 21 1597 610 377 10946 89 514229 987 8 55 6765 2178309 121393 317811 46368 4181 1 832040 2 28657 75025 34 13 17711 
记录在达芬奇窗台口的神秘数字串:36968853882116725547342176952286

打印斐波那契数列前32项,这里直接使用我以前写的C++代码:

#include<iostream>
using namespace std;
int main(){
    int count = 32;
    int * list = (int *)malloc(count);
    void fib(int num,int * list);
    fib(count,list);
    for(int i=0;i<count;i++) cout<<list[i]<<endl; 
    getchar();
}
/**
 * 计算fib数列前n项
 * */
void fib(int num,int * list){
    list[0] = 1;    //定义数列第一项
    list[1] = 1;    //定义数列第二项
    if(num<=2) return ;
    for(int i=2;i<num;i++){
        list[i] = list[i-1]+list[i-2];
    }
}

结果:

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040 1346269 2178309

和题目的数列进行比对:

1 233 3 2584 1346269 144 5 196418 21 1597 610 377 10946 89 514229 987 8 55 6765 2178309 121393 317811 46368 4181 1 832040 2 28657 75025 34 13 17711 

得到移位顺序:

(0)(13)(4)(31)(12)............

太麻烦了还是上python脚本吧:(

# -*- coding: UTF-8 -*-
lisb = [1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040,1346269,2178309]
ts=[1,233,3,2584,1346269,144,5,196418,21,1597,610,377,10946,89,514229,987,8,55,6765,2178309,121393,317811,46368,4181,1,832040,2,28657,75025,34,13,17711]
shunxu = [0]*32
for i in range(0,32):
 for j in range(0,32):
  if(ts[i]==lisb[j]):
	shunxu[i] = j
	break
print shunxu

结果:

[0, 12, 3, 17, 30, 11, 4, 26, 7, 16, 14, 13, 20, 10, 28, 15, 5, 9, 19, 31, 25, 27, 23, 18, 0, 29, 2, 22, 24, 8, 6, 21]

根据该移位顺序从题目的密文恢复明文:

假设明文为m,则根据该移位顺序,m[12] = c[1],以此类推继续上代码:

# -*- coding: UTF-8 -*-
lisb = [1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040,1346269,2178309]
ts=[1,233,3,2584,1346269,144,5,196418,21,1597,610,377,10946,89,514229,987,8,55,6765,2178309,121393,317811,46368,4181,1,832040,2,28657,75025,34,13,17711]
shunxu = [0]*32
for i in range(0,32):
 for j in range(0,32):
  if(ts[i]==lisb[j]):
	shunxu[i] = j
	break
c = '36968853882116725547342176952286'
m = ['a']*32
for i in range(0,32):
 m[shunxu[i]] = c[i]
print m

得到['7', 'a', '9', '9', '5', '5', '8', '8', '2', '5', '6', '8', '6', '1', '2', '2', '8', '6', '1', '4', '1', '6', '5', '2', '2', '3', '3', '4', '7', '6', '8', '7']

这里因为题目的1不知道是第0还是第1个1,因此还有另一种可能:

['a', '7', '9', '9', '5', '5', '8', '8', '2', '5', '6', '8', '6', '1', '2', '2', '8', '6', '1', '4', '1', '6', '5', '2', '2', '3', '3', '4', '7', '6', '8', '7']

输出结果中还存在a,是因为斐波那契数列中存在两个1,而在index()找位置的时候,是从前往后找的,因此两次的1会覆盖掉。所以要将m中t的第二次出现1的位置上的数替换给a,然后复原被覆盖的值。

最后得到flag{37995588256861228614165223347687}

4.rot

根据题目名,猜测是移位密码。

rot5,rot13等等先分别试个遍看看最后发现是13.

a = [83,89,78,84,45,86,96,45,115,121,110,116,136,132,132,132,108,128,117,118,134,110,123,111,110,127,108,112,124,122,108,118,128,108,131,114,127,134,108,116,124,124,113,108,76,76,76,76,138,23,90,81,66,71,64,69,114,65,112,64,66,63,69,61,70,114,62,66,61,62,69,67,70,63,61,110,110,112,64,68,62,70,61,112,111,112]
s = ""
for i in range(0,len(a)):
    s+=chr(a[i]-13)
print s
FLAG IS flag{www_shiyanbar_com_is_very_good_????}
MD5:38e4c352809e150186920aac37190cbc

然后对其进行爆破,得到flag.

5.萌萌哒的八戒

提示:萌萌哒的八戒原来曾经是猪村的村长,从远古时期,猪村就有一种神秘的代码。请从附件中找出代码,看看萌萌哒的猪八戒到底想说啥 注意:得到的 flag 请包上 flag{} 提交。

根据提示和图片知道是猪圈密码。

直接翻译得到flag。

6.old-fashion

蒙蔽,词频分析:

quipqiup - cryptoquip and cryptogram solver

7.RSA2

这题是dp泄露:

import gmpy2
import binascii

def getd(n,e,dp):
    for i in range(1,e):
        if (dp*e-1)%i == 0:
            if n%(((dp*e-1)/i)+1)==0:
                p=((dp*e-1)/i)+1
                q=n/(((dp*e-1)/i)+1)
                phi = (p-1)*(q-1)
                d = gmpy2.invert(e,phi)%phi
                return d

e = 65537
n = 248254007851526241177721526698901802985832766176221609612258877371620580060433101538328030305219918697643619814200930679612109885533801335348445023751670478437073055544724280684733298051599167660303645183146161497485358633681492129668802402065797789905550489547645118787266601929429724133167768465309665906113
c = 140423670976252696807533673586209400575664282100684119784203527124521188996403826597436883766041879067494280957410201958935737360380801845453829293997433414188838725751796261702622028587211560353362847191060306578510511380965162133472698713063592621028959167072781482562673683090590521214218071160287665180751
dp = 905074498052346904643025132879518330691925174573054004621877253318682675055421970943552016695528560364834446303196939207056642927148093290374440210503657

d=getd(n,e,dp)
m=pow(c,d,n)
print binascii.unhexlify(hex(m)[2:])

运行得到flag。

8.RSA3

这题是共模攻击

#-*- coding:utf-8 -*-
import binascii
import gmpy2
e1 = 11187289
e2 = 9647291
a = gmpy2.gcdext(e1,e2)   #拓展欧里几德算法
r = int(a[1])
s = int(a[2])
c2 = 18702010045187015556548691642394982835669262147230212731309938675226458555210425972429418449273410535387985931036711854265623905066805665751803269106880746769003478900791099590239513925449748814075904017471585572848473556490565450062664706449128415834787961947266259789785962922238701134079720414228414066193071495304612341052987455615930023536823801499269773357186087452747500840640419365011554421183037505653461286732740983702740822671148045619497667184586123657285604061875653909567822328914065337797733444640351518775487649819978262363617265797982843179630888729407238496650987720428708217115257989007867331698397
c1 = 22322035275663237041646893770451933509324701913484303338076210603542612758956262869640822486470121149424485571361007421293675516338822195280313794991136048140918842471219840263536338886250492682739436410013436651161720725855484866690084788721349555662019879081501113222996123305533009325964377798892703161521852805956811219563883312896330156298621674684353919547558127920925706842808914762199011054955816534977675267395009575347820387073483928425066536361482774892370969520740304287456555508933372782327506569010772537497541764311429052216291198932092617792645253901478910801592878203564861118912045464959832566051361
N = 0xb3e1f44e2eed80c17893baed4054d7ab15bf456d9c7d3eb45e250e44bbae19b2b1dfc5e69b8b5adc423c88e392ed73a2db50f47bc2bc5e19c4f9fe93b8333d2713fcf07326032cd42bd2d3024088d8785eae42ad35eda51145f095b71330c232422e4eb6355cea55ac300a829fcb05149f539ae151276bfddf7ec2ec792707f5fdd434b0f1679849e6eeda098a1a74db493949d9146d469ee97e7b302b9eef7b000455d23cea4160cfc1135743f186fb35e5af24455630417b465e4e6f3f2fc91a0869d1d06d6dadf04a031a444bc06db0f5de8223498b0a0e78d1ea8b1c69c83bc2066b2ac4d13c0f121f1c76dfdf4a41af296ca4e6ebeb22f3579923c0fa71
m= (gmpy2.powmod(c1,r,N)*gmpy2.powmod(c2,s,N))%N      #计算明文,计算出的明文为16进制形式
flag = hex(m)
flag = binascii.a2b_hex(flag[2:]) # 将十六进制数字字符串转换为二进制数据(字符切片截取0x之后的)
print flag

运行得到flag。

9.RSAROLL

这题先把N:920139713分解一下:

然后计算一下私钥d:

然后根据私钥解密:

import gmpy2 as gp
import binascii
p = 18443
q = 49891
e = 19
c =[704796792,752211152,274704164,18414022,368270835,483295235,263072905,459788476,483295235,459788476,663551792,475206804,459788476,428313374,475206804,459788476,425392137,704796792,458265677,341524652,483295235,534149509,425392137,428313374,425392137,341524652,458265677,263072905,483295235,828509797,341524652,425392137,475206804,428313374,483295235,475206804,459788476,306220148]
n = p*q
phi = (p-1) * (q-1)
d = gp.invert(e, phi)
flag = ""
for i in range(0,len(c)):
 m = pow(c[i], d, n)
 flag+=chr(m)
print flag

10.权限获得第一步

Administrator:500:806EDC27AA52E314AAD3B435B51404EE:F4AD50F57683D4260DFD48AA351A17A8:::

这个一看就是windows密码。

把F4AD50F57683D4260DFD48AA351A17A8md5解密一下:

11.还原大师

我们得到了一串神秘字符串:TASC?O3RJMV?WDJKX?ZM,问号部分是未知大写字母,为了确定这个神秘字符串,我们通过了其他途径获得了这个字串的32位MD5码。但是我们获得它的32位MD5码也是残缺不全,E903???4DAB????08?????51?80??8A?,请猜出神秘字符串的原本模样,并且提交这个字串的32位MD5码作为答案

只有3个问号而且是大写字母,直接爆破了:
上python3脚本:

import hashlib
str1 = "TASC"
str2 = "O3RJMV"
str3 = "WDJKX"
str4 = "ZM"
dic = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
for i in range(0,26):
 for j in range(0,26):
  for k in range(0,26):
   s = str1+dic[i]+str2+dic[j]+str3+dic[k]+str4
   # print(s)
   md5s = hashlib.md5(s.encode('utf8')).hexdigest().upper()
   if(md5s[:4]=="E903"):
    print(md5s)

运行得到flag。

12.世上无难事

以下是某国现任总统外发的一段指令,经过一种奇异的加密方式,毫无规律,看来只能分析了。请将这段语句还原成通顺语句,并从中找到key作为答案提交,答案是32位.

词频分析。

大写转小写之后得到flag。

13.[MRCTF2020]古典密码知多少

蓝色是猪圈密码,黄色是圣堂武士密码,黑色是标准银河字母。

查表解密得FGCPFLIRTUASYON

放进栅栏解密一下得到flag:

14.异性相吸

这题就是个简单的异或。

把密文用winhex打开,提取二进制数据,然后把二进制数据逐位和key异或,再二进制转字符串得到flag

15. Unencode

89FQA9WMD<V1A<V1S83DY.#<W3$Q,2TM]

补知识:

uuencode是以前unix下常用编码方式应用于UUCP(unix to unix copy),通过串行通讯传输二进制文件。base64属于MIME(多用途国际互联网邮件扩展)编码,与uuencode不是同一个范畴的,MIME主要应用于邮件,Uuencode主要应用在邮件和新闻组。

php直接解码:

<?php
/*
convert_uudecode() 函数对 uuencode 编码的字符串进行解码。
该函数常与 convert_uuencode() 函数一起使用。
*/

$str = "Hello world!";
// 对字符串进行编码
$encodeString = convert_uuencode($str);
echo $encodeString . "<br>";

// 对字符串进行解码
$decodeString = convert_uudecode($encodeString);
echo $decodeString;
?>

也可以用python3的uu模块:

import uu
in2 = open("E:\\2.txt","wb")
uu.decode("E:\\1.txt",in2)

但这里对输入的源文件有特殊格式要求:

begin 666 1.txt
89FQA9WMD<V1A<V1S83DY.#<W3$Q,2TM]
 
end

第一行声明文件开始,第二行放编码,第三行空行,第四行声明文件结束。

如果不按这个格式来python会抛出错误。所以还是用php吧。

16.鸡藕椒盐味

公司食堂最新出了一种小吃,叫鸡藕椒盐味汉堡,售价八块钱,为了促销,上面有一个验证码,输入后可以再换取一个汉堡。但是问题是每个验证码几乎都有错误,而且打印的时候倒了一下。小明买到了一个汉堡,准备还原验证码,因为一个吃不饱啊验证码如下:1100 1010 0000 ,而且打印的时候倒了一下。把答案哈希一下就可以提交了。(答案为正确值(不包括数字之间的空格)的32位md5值的小写形式) 注意:得到的 flag 请包上 flag{} 提交

鸡(奇)藕(偶)椒(校)盐(验)味(位)。

奇偶校验位。。。。

这题有点脑洞大开了。

目(百)测(度)得到是海明码。

详见海明码

校验及恢复数据的python代码:

import math

userin = '110010100000' #这里填要校验的海明码
userin = userin[::-1]
haiming = [];
# 输入的海明码是字符串,下面的代码是将字符串转化成整型列表
haiming = list(haiming)
for x in userin:
    haiming.append(int(x))

# 计算校验码的个数
for i in range(0, len(haiming)):
    if 2 ** i > len(haiming):
        break
# 校验码个数
num = i
# 标记错位
flag = 0
for i in range(0, num):
    b = []
    if (i == 0):
        # 利用python的步长。
        a = haiming[(2 ** i) - 1:len(haiming):(2 ** i) + 1]
        if (a.count(1) % 2 == 1):
            flag = flag + 2 ** i
        print(a)
    else:
        for j in range(2 ** i, len(haiming) + 1):
            if ((j / (2 ** i)) % 2 == 1):
                for k in range(j, j + (2 ** i)):
                    if (k > len(haiming)):
                        break
                    b.append(haiming[k - 1])
        if (b.count(1) % 2 == 1):
            flag = flag + 2 ** i
        print(b)
    del (b)
print(flag)
if flag != 0:
    if haiming[flag - 1] == 0:
        haiming[flag - 1] = 1
    elif haiming[flag - 1] == 1:
        haiming[flag - 1] = 0
    print("第%d位出错" % (flag))
    print(haiming)
else:
    print("没有错误")
input("Press <enter>")

根据题目,把结果反转之后放进去校验:

得到0000 0101 1011

计算哈希不对,把结果再反转之后计算哈希,正确。

flag{d14084c7ceca6359eaac6df3c234dd3b}

17.[WUSTCTF2020]佛说:只能四天

这题与佛论禅解不出来,百度说使用新约佛论禅解:

然后社会主义核心价值观编码器解码:

然后用栅栏解码,记得把后面的doyou*去掉。

凯撒密码:

然后逐个尝试发现是base32:

18.[AFCTF2018]Morse

这题就是摩斯密码+16进制。

下载下来之后把/替换成空格得到:

-.... .---- -.... -.... -.... ...-- --... ....- -.... -.... --... -... ...-- .---- --... ...-- ..--- --... --... ....- ..... ..-. --... ...-- ...-- ----- ..... ..-. ...-- ...-- ...-- ....- ...-- ..... --... ----. --... -..

然后解码:

把解码结果拿去16进制转字符串:

19.RSA5

低加密指数广播攻击

import gmpy2
import libnum

e = 65537

n0 = 20474918894051778533305262345601880928088284471121823754049725354072477155873778848055073843345820697886641086842612486541250183965966001591342031562953561793332341641334302847996108417466360688139866505179689516589305636902137210185624650854906780037204412206309949199080005576922775773722438863762117750429327585792093447423980002401200613302943834212820909269713876683465817369158585822294675056978970612202885426436071950214538262921077409076160417436699836138801162621314845608796870206834704116707763169847387223307828908570944984416973019427529790029089766264949078038669523465243837675263858062854739083634207
c0 = 974463908243330865728978769213595400782053398596897741316275722596415018912929508637393850919224969271766388710025195039896961956062895570062146947736340342927974992616678893372744261954172873490878805483241196345881721164078651156067119957816422768524442025688079462656755605982104174001635345874022133045402344010045961111720151990412034477755851802769069309069018738541854130183692204758761427121279982002993939745343695671900015296790637464880337375511536424796890996526681200633086841036320395847725935744757993013352804650575068136129295591306569213300156333650910795946800820067494143364885842896291126137320

n1 = 20918819960648891349438263046954902210959146407860980742165930253781318759285692492511475263234242002509419079545644051755251311392635763412553499744506421566074721268822337321637265942226790343839856182100575539845358877493718334237585821263388181126545189723429262149630651289446553402190531135520836104217160268349688525168375213462570213612845898989694324269410202496871688649978370284661017399056903931840656757330859626183773396574056413017367606446540199973155630466239453637232936904063706551160650295031273385619470740593510267285957905801566362502262757750629162937373721291789527659531499435235261620309759
c1 = 15819636201971185538694880505120469332582151856714070824521803121848292387556864177196229718923770810072104155432038682511434979353089791861087415144087855679134383396897817458726543883093567600325204596156649305930352575274039425470836355002691145864435755333821133969266951545158052745938252574301327696822347115053614052423028835532509220641378760800693351542633860702225772638930501021571415907348128269681224178300248272689705308911282208685459668200507057183420662959113956077584781737983254788703048275698921427029884282557468334399677849962342196140864403989162117738206246183665814938783122909930082802031855

n2 = 25033254625906757272369609119214202033162128625171246436639570615263949157363273213121556825878737923265290579551873824374870957467163989542063489416636713654642486717219231225074115269684119428086352535471683359486248203644461465935500517901513233739152882943010177276545128308412934555830087776128355125932914846459470221102007666912211992310538890654396487111705385730502843589727289829692152177134753098649781412247065660637826282055169991824099110916576856188876975621376606634258927784025787142263367152947108720757222446686415627479703666031871635656314282727051189190889008763055811680040315277078928068816491
c2 = 4185308529416874005831230781014092407198451385955677399668501833902623478395669279404883990725184332709152443372583701076198786635291739356770857286702107156730020004358955622511061410661058982622055199736820808203841446796305284394651714430918690389486920560834672316158146453183789412140939029029324756035358081754426645160033262924330248675216108270980157049705488620263485129480952814764002865280019185127662449318324279383277766416258142275143923532168798413011028271543085249029048997452212503111742302302065401051458066585395360468447460658672952851643547193822775218387853623453638025492389122204507555908862

n3 = 21206968097314131007183427944486801953583151151443627943113736996776787181111063957960698092696800555044199156765677935373149598221184792286812213294617749834607696302116136745662816658117055427803315230042700695125718401646810484873064775005221089174056824724922160855810527236751389605017579545235876864998419873065217294820244730785120525126565815560229001887622837549118168081685183371092395128598125004730268910276024806808565802081366898904032509920453785997056150497645234925528883879419642189109649009132381586673390027614766605038951015853086721168018787523459264932165046816881682774229243688581614306480751
c3 = 4521038011044758441891128468467233088493885750850588985708519911154778090597136126150289041893454126674468141393472662337350361712212694867311622970440707727941113263832357173141775855227973742571088974593476302084111770625764222838366277559560887042948859892138551472680654517814916609279748365580610712259856677740518477086531592233107175470068291903607505799432931989663707477017904611426213770238397005743730386080031955694158466558475599751940245039167629126576784024482348452868313417471542956778285567779435940267140679906686531862467627238401003459101637191297209422470388121802536569761414457618258343550613

n4 = 22822039733049388110936778173014765663663303811791283234361230649775805923902173438553927805407463106104699773994158375704033093471761387799852168337898526980521753614307899669015931387819927421875316304591521901592823814417756447695701045846773508629371397013053684553042185725059996791532391626429712416994990889693732805181947970071429309599614973772736556299404246424791660679253884940021728846906344198854779191951739719342908761330661910477119933428550774242910420952496929605686154799487839923424336353747442153571678064520763149793294360787821751703543288696726923909670396821551053048035619499706391118145067
c4 = 15406498580761780108625891878008526815145372096234083936681442225155097299264808624358826686906535594853622687379268969468433072388149786607395396424104318820879443743112358706546753935215756078345959375299650718555759698887852318017597503074317356745122514481807843745626429797861463012940172797612589031686718185390345389295851075279278516147076602270178540690147808314172798987497259330037810328523464851895621851859027823681655934104713689539848047163088666896473665500158179046196538210778897730209572708430067658411755959866033531700460551556380993982706171848970460224304996455600503982223448904878212849412357

n5 = 21574139855341432908474064784318462018475296809327285532337706940126942575349507668289214078026102682252713757703081553093108823214063791518482289846780197329821139507974763780260290309600884920811959842925540583967085670848765317877441480914852329276375776405689784571404635852204097622600656222714808541872252335877037561388406257181715278766652824786376262249274960467193961956690974853679795249158751078422296580367506219719738762159965958877806187461070689071290948181949561254144310776943334859775121650186245846031720507944987838489723127897223416802436021278671237227993686791944711422345000479751187704426369
c5 = 20366856150710305124583065375297661819795242238376485264951185336996083744604593418983336285185491197426018595031444652123288461491879021096028203694136683203441692987069563513026001861435722117985559909692670907347563594578265880806540396777223906955491026286843168637367593400342814725694366078337030937104035993569672959361347287894143027186846856772983058328919716702982222142848848117768499996617588305301483085428547267337070998767412540225911508196842253134355901263861121500650240296746702967594224401650220168780537141654489215019142122284308116284129004257364769474080721001708734051264841350424152506027932

n6 = 25360227412666612490102161131174584819240931803196448481224305250583841439581008528535930814167338381983764991296575637231916547647970573758269411168219302370541684789125112505021148506809643081950237623703181025696585998044695691322012183660424636496897073045557400768745943787342548267386564625462143150176113656264450210023925571945961405709276631990731602198104287528528055650050486159837612279600415259486306154947514005408907590083747758953115486124865486720633820559135063440942528031402951958557630833503775112010715604278114325528993771081233535247118481765852273252404963430792898948219539473312462979849137
c6 = 19892772524651452341027595619482734356243435671592398172680379981502759695784087900669089919987705675899945658648623800090272599154590123082189645021800958076861518397325439521139995652026377132368232502108620033400051346127757698623886142621793423225749240286511666556091787851683978017506983310073524398287279737680091787333547538239920607761080988243639547570818363788673249582783015475682109984715293163137324439862838574460108793714172603672477766831356411304446881998674779501188163600664488032943639694828698984739492200699684462748922883550002652913518229322945040819064133350314536378694523704793396169065179

n7 = 22726855244632356029159691753451822163331519237547639938779517751496498713174588935566576167329576494790219360727877166074136496129927296296996970048082870488804456564986667129388136556137013346228118981936899510687589585286517151323048293150257036847475424044378109168179412287889340596394755257704938006162677656581509375471102546261355748251869048003600520034656264521931808651038524134185732929570384705918563982065684145766427962502261522481994191989820110575981906998431553107525542001187655703534683231777988419268338249547641335718393312295800044734534761692799403469497954062897856299031257454735945867491191
c7 = 6040119795175856407541082360023532204614723858688636724822712717572759793960246341800308149739809871234313049629732934797569781053000686185666374833978403290525072598774001731350244744590772795701065129561898116576499984185920661271123665356132719193665474235596884239108030605882777868856122378222681140570519180321286976947154042272622411303981011302586225630859892731724640574658125478287115198406253847367979883768000812605395482952698689604477719478947595442185921480652637868335673233200662100621025061500895729605305665864693122952557361871523165300206070325660353095592778037767395360329231331322823610060006

n8 = 23297333791443053297363000786835336095252290818461950054542658327484507406594632785712767459958917943095522594228205423428207345128899745800927319147257669773812669542782839237744305180098276578841929496345963997512244219376701787616046235397139381894837435562662591060768476997333538748065294033141610502252325292801816812268934171361934399951548627267791401089703937389012586581080223313060159456238857080740699528666411303029934807011214953984169785844714159627792016926490955282697877141614638806397689306795328344778478692084754216753425842557818899467945102646776342655167655384224860504086083147841252232760941
c8 = 5418120301208378713115889465579964257871814114515046096090960159737859076829258516920361577853903925954198406843757303687557848302302200229295916902430205737843601806700738234756698575708612424928480440868739120075888681672062206529156566421276611107802917418993625029690627196813830326369874249777619239603300605876865967515719079797115910578653562787899019310139945904958024882417833736304894765433489476234575356755275147256577387022873348906900149634940747104513850154118106991137072643308620284663108283052245750945228995387803432128842152251549292698947407663643895853432650029352092018372834457054271102816934

n9 = 28873667904715682722987234293493200306976947898711255064125115933666968678742598858722431426218914462903521596341771131695619382266194233561677824357379805303885993804266436810606263022097900266975250431575654686915049693091467864820512767070713267708993899899011156106766178906700336111712803362113039613548672937053397875663144794018087017731949087794894903737682383916173267421403408140967713071026001874733487295007501068871044649170615709891451856792232315526696220161842742664778581287321318748202431466508948902745314372299799561625186955234673012098210919745879882268512656931714326782335211089576897310591491
c9 = 9919880463786836684987957979091527477471444996392375244075527841865509160181666543016317634963512437510324198702416322841377489417029572388474450075801462996825244657530286107428186354172836716502817609070590929769261932324275353289939302536440310628698349244872064005700644520223727670950787924296004296883032978941200883362653993351638545860207179022472492671256630427228461852668118035317021428675954874947015197745916918197725121122236369382741533983023462255913924692806249387449016629865823316402366017657844166919846683497851842388058283856219900535567427103603869955066193425501385255322097901531402103883869

n10 = 22324685947539653722499932469409607533065419157347813961958075689047690465266404384199483683908594787312445528159635527833904475801890381455653807265501217328757871352731293000303438205315816792663917579066674842307743845261771032363928568844669895768092515658328756229245837025261744260614860746997931503548788509983868038349720225305730985576293675269073709022350700836510054067641753713212999954307022524495885583361707378513742162566339010134354907863733205921845038918224463903789841881400814074587261720283879760122070901466517118265422863420376921536734845502100251460872499122236686832189549698020737176683019
c10 = 1491527050203294989882829248560395184804977277747126143103957219164624187528441047837351263580440686474767380464005540264627910126483129930668344095814547592115061057843470131498075060420395111008619027199037019925701236660166563068245683975787762804359520164701691690916482591026138582705558246869496162759780878437137960823000043988227303003876410503121370163303711603359430764539337597866862508451528158285103251810058741879687875218384160282506172706613359477657215420734816049393339593755489218588796607060261897905233453268671411610631047340459487937479511933450369462213795738933019001471803157607791738538467

n11 = 27646746423759020111007828653264027999257847645666129907789026054594393648800236117046769112762641778865620892443423100189619327585811384883515424918752749559627553637785037359639801125213256163008431942593727931931898199727552768626775618479833029101249692573716030706695702510982283555740851047022672485743432464647772882314215176114732257497240284164016914018689044557218920300262234652840632406067273375269301008409860193180822366735877288205783314326102263756503786736122321348320031950012144905869556204017430593656052867939493633163499580242224763404338807022510136217187779084917996171602737036564991036724299
c11 = 21991524128957260536043771284854920393105808126700128222125856775506885721971193109361315961129190814674647136464887087893990660894961612838205086401018885457667488911898654270235561980111174603323721280911197488286585269356849579263043456316319476495888696219344219866516861187654180509247881251251278919346267129904739277386289240394384575124331135655943513831009934023397457082184699737734388823763306805326430395849935770213817533387235486307008892410920611669932693018165569417445885810825749609388627231235840912644654685819620931663346297596334834498661789016450371769203650109994771872404185770230172934013971

n12 = 20545487405816928731738988374475012686827933709789784391855706835136270270933401203019329136937650878386117187776530639342572123237188053978622697282521473917978282830432161153221216194169879669541998840691383025487220850872075436064308499924958517979727954402965612196081404341651517326364041519250125036424822634354268773895465698920883439222996581226358595873993976604699830613932320720554130011671297944433515047180565484495191003887599891289037982010216357831078328159028953222056918189365840711588671093333013117454034313622855082795813122338562446223041211192277089225078324682108033843023903550172891959673551
c12 = 14227439188191029461250476692790539654619199888487319429114414557975376308688908028140817157205579804059783807641305577385724758530138514972962209062230576107406142402603484375626077345190883094097636019771377866339531511965136650567412363889183159616188449263752475328663245311059988337996047359263288837436305588848044572937759424466586870280512424336807064729894515840552404756879590698797046333336445465120445087587621743906624279621779634772378802959109714400516183718323267273824736540168545946444437586299214110424738159957388350785999348535171553569373088251552712391288365295267665691357719616011613628772175

n13 = 27359727711584277234897157724055852794019216845229798938655814269460046384353568138598567755392559653460949444557879120040796798142218939251844762461270251672399546774067275348291003962551964648742053215424620256999345448398805278592777049668281558312871773979931343097806878701114056030041506690476954254006592555275342579529625231194321357904668512121539514880704046969974898412095675082585315458267591016734924646294357666924293908418345508902112711075232047998775303603175363964055048589769318562104883659754974955561725694779754279606726358588862479198815999276839234952142017210593887371950645418417355912567987
c13 = 3788529784248255027081674540877016372807848222776887920453488878247137930578296797437647922494510483767651150492933356093288965943741570268943861987024276610712717409139946409513963043114463933146088430004237747163422802959250296602570649363016151581364006795894226599584708072582696996740518887606785460775851029814280359385763091078902301957226484620428513604630585131511167015763190591225884202772840456563643159507805711004113901417503751181050823638207803533111429510911616160851391754754434764819568054850823810901159821297849790005646102129354035735350124476838786661542089045509656910348676742844957008857457

n14 = 27545937603751737248785220891735796468973329738076209144079921449967292572349424539010502287564030116831261268197384650511043068738911429169730640135947800885987171539267214611907687570587001933829208655100828045651391618089603288456570334500533178695238407684702251252671579371018651675054368606282524673369983034682330578308769886456335818733827237294570476853673552685361689144261552895758266522393004116017849397346259119221063821663280935820440671825601452417487330105280889520007917979115568067161590058277418371493228631232457972494285014767469893647892888681433965857496916110704944758070268626897045014782837
c14 = 14069112970608895732417039977542732665796601893762401500878786871680645798754783315693511261740059725171342404186571066972546332813667711135661176659424619936101038903439144294886379322591635766682645179888058617577572409307484708171144488708410543462972008179994594087473935638026612679389759756811490524127195628741262871304427908481214992471182859308828778119005750928935764927967212343526503410515793717201360360437981322576798056276657140363332700714732224848346808963992302409037706094588964170239521193589470070839790404597252990818583717869140229811712295005710540476356743378906642267045723633874011649259842

n15 = 25746162075697911560263181791216433062574178572424600336856278176112733054431463253903433128232709054141607100891177804285813783247735063753406524678030561284491481221681954564804141454666928657549670266775659862814924386584148785453647316864935942772919140563506305666207816897601862713092809234429096584753263707828899780979223118181009293655563146526792388913462557306433664296966331469906428665127438829399703002867800269947855869262036714256550075520193125987011945192273531732276641728008406855871598678936585324782438668746810516660152018244253008092470066555687277138937298747951929576231036251316270602513451
c15 = 17344284860275489477491525819922855326792275128719709401292545608122859829827462088390044612234967551682879954301458425842831995513832410355328065562098763660326163262033200347338773439095709944202252494552172589503915965931524326523663289777583152664722241920800537867331030623906674081852296232306336271542832728410803631170229642717524942332390842467035143631504401140727083270732464237443915263865880580308776111219718961746378842924644142127243573824972533819479079381023103585862099063382129757560124074676150622288706094110075567706403442920696472627797607697962873026112240527498308535903232663939028587036724

n16 = 23288486934117120315036919418588136227028485494137930196323715336208849327833965693894670567217971727921243839129969128783853015760155446770590696037582684845937132790047363216362087277861336964760890214059732779383020349204803205725870225429985939570141508220041286857810048164696707018663758416807708910671477407366098883430811861933014973409390179948577712579749352299440310543689035651465399867908428885541237776143404376333442949397063249223702355051571790555151203866821867908531733788784978667478707672984539512431549558672467752712004519300318999208102076732501412589104904734983789895358753664077486894529499
c16 = 10738254418114076548071448844964046468141621740603214384986354189105236977071001429271560636428075970459890958274941762528116445171161040040833357876134689749846940052619392750394683504816081193432350669452446113285638982551762586656329109007214019944975816434827768882704630460001209452239162896576191876324662333153835533956600295255158377025198426950944040643235430211011063586032467724329735785947372051759042138171054165854842472990583800899984893232549092766400510300083585513014171220423103452292891496141806956300396540682381668367564569427813092064053993103537635994311143010708814851867239706492577203899024

n17 = 19591441383958529435598729113936346657001352578357909347657257239777540424811749817783061233235817916560689138344041497732749011519736303038986277394036718790971374656832741054547056417771501234494768509780369075443550907847298246275717420562375114406055733620258777905222169702036494045086017381084272496162770259955811174440490126514747876661317750649488774992348005044389081101686016446219264069971370646319546429782904810063020324704138495608761532563310699753322444871060383693044481932265801505819646998535192083036872551683405766123968487907648980900712118052346174533513978009131757167547595857552370586353973
c17 = 3834917098887202931981968704659119341624432294759361919553937551053499607440333234018189141970246302299385742548278589896033282894981200353270637127213483172182529890495903425649116755901631101665876301799865612717750360089085179142750664603454193642053016384714515855868368723508922271767190285521137785688075622832924829248362774476456232826885801046969384519549385428259591566716890844604696258783639390854153039329480726205147199247183621535172450825979047132495439603840806501254997167051142427157381799890725323765558803808030109468048682252028720241357478614704610089120810367192414352034177484688502364022887

n18 = 19254242571588430171308191757871261075358521158624745702744057556054652332495961196795369630484782930292003238730267396462491733557715379956969694238267908985251699834707734400775311452868924330866502429576951934279223234676654749272932769107390976321208605516299532560054081301829440688796904635446986081691156842271268059970762004259219036753174909942343204432795076377432107630203621754552804124408792358220071862369443201584155711893388877350138023238624566616551246804054720492816226651467017802504094070614892556444425915920269485861799532473383304622064493223627552558344088839860178294589481899206318863310603
c18 = 6790553533991297205804561991225493105312398825187682250780197510784765226429663284220400480563039341938599783346724051076211265663468643826430109013245014035811178295081939958687087477312867720289964506097819762095244479129359998867671811819738196687884696680463458661374310994610760009474264115750204920875527434486437536623589684519411519100170291423367424938566820315486507444202022408003879118465761273916755290898112991525546114191064022991329724370064632569903856189236177894007766690782630247443895358893983735822824243487181851098787271270256780891094405121947631088729917398317652320497765101790132679171889

n19 = 26809700251171279102974962949184411136459372267620535198421449833298448092580497485301953796619185339316064387798092220298630428207556482805739803420279056191194360049651767412572609187680508073074653291350998253938793269214230457117194434853888765303403385824786231859450351212449404870776320297419712486574804794325602760347306432927281716160368830187944940128907971027838510079519466846176106565164730963988892400240063089397720414921398936399927948235195085202171264728816184532651138221862240969655185596628285814057082448321749567943946273776184657698104465062749244327092588237927996419620170254423837876806659
c19 = 386213556608434013769864727123879412041991271528990528548507451210692618986652870424632219424601677524265011043146748309774067894985069288067952546139416819404039688454756044862784630882833496090822568580572859029800646671301748901528132153712913301179254879877441322285914544974519727307311002330350534857867516466612474769753577858660075830592891403551867246057397839688329172530177187042229028685862036140779065771061933528137423019407311473581832405899089709251747002788032002094495379614686544672969073249309703482556386024622814731015767810042969813752548617464974915714425595351940266077021672409858645427346

n=[n0,n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16,n17,n18]
c=[c0,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18]

for i in range(len(n)):
    for j in range(len(n)):
        if(i!=j):
            if(gmpy2.gcd(n[i],n[j])!=1):   #对不同的n进行 欧几里得算法,以求出最大公约数(p)
                print(i,j)                 #输出对应的n的序号
                p = gmpy2.gcd(n[i],n[j])
                print("p = ",p)
                q = n[i] // p
                print("q = ",q)
                d = gmpy2.invert(e , (p-1)*(q-1))
                print("d = ",d)
                m = pow(c[i],d,n[i])
                print("m = ",m)

print(libnum.n2s(int(m)))

20.rsa2

看到e很大,判断是低解密指数攻击。

#该代码在python2.7下运行
import gmpy2
from Crypto.Util.number import long_to_bytes


def continuedFra(x, y):
    cF = []
    while y:
        cF += [x // y]
        x, y = y, x % y
    return cF


def Simplify(ctnf):
    numerator = 0
    denominator = 1
    for x in ctnf[::-1]:
        numerator, denominator = denominator, x * denominator + numerator
    return (numerator, denominator)


def calculateFrac(x, y):
    cF = continuedFra(x, y)
    cF = list(map(Simplify, (cF[0:i] for i in range(1, len(cF)))))
    return cF


def solve_pq(a, b, c):
    par = gmpy2.isqrt(b * b - 4 * a * c)
    return (-b + par) / (2 * a), (-b - par) / (2 * a)


def wienerAttack(e, n):
    for (d, k) in calculateFrac(e, n):
        print(e)
        print(d)
        print(k)
        if k == 0:
            continue
        if (e * d - 1) % k != 0:
            continue
        phi = (e * d - 1) / k
        p, q = solve_pq(1, n - phi + 1, n)
        if p * q == n:
            return abs(int(p)), abs(int(q))
    print('[!]not find!')
n = 101991809777553253470276751399264740131157682329252673501792154507006158434432009141995367241962525705950046253400188884658262496534706438791515071885860897552736656899566915731297225817250639873643376310103992170646906557242832893914902053581087502512787303322747780420210884852166586717636559058152544979471
e = 46731919563265721307105180410302518676676135509737992912625092976849075262192092549323082367518264378630543338219025744820916471913696072050291990620486581719410354385121760761374229374847695148230596005409978383369740305816082770283909611956355972181848077519920922059268376958811713365106925235218265173085
p, q = wienerAttack(e, n)
print('[+]Found!')
print('[-]p =', p)
print('[-]q =', q)
d = gmpy2.invert(e, (p-1)*(q-1))
import hashlib
flag = "flag{" + hashlib.md5(hex(int(gmpy2.digits(d)))).hexdigest() + "}"
print (flag)

21.Dangerous RSA


import gmpy2
import os
from functools import reduce

from Crypto.Util.number import long_to_bytes


def CRT(items):
    N = reduce(lambda x, y: x * y, (i[1] for i in items))
    result = 0
    for a, n in items:
        m = N // n
        d, r, s = gmpy2.gcdext(n, m)
        if d != 1:
            raise Exception("Input not pairwise co-prime")
        result += a * s * m
    return result % N, N


# e, n, c
e = 0x3
n=[0x52d483c27cd806550fbe0e37a61af2e7cf5e0efb723dfc81174c918a27627779b21fa3c851e9e94188eaee3d5cd6f752406a43fbecb53e80836ff1e185d3ccd7782ea846c2e91a7b0808986666e0bdadbfb7bdd65670a589a4d2478e9adcafe97c6ee23614bcb2ecc23580f4d2e3cc1ecfec25c50da4bc754dde6c8bfd8d1fc16956c74d8e9196046a01dc9f3024e11461c294f29d7421140732fedacac97b8fe50999117d27943c953f18c4ff4f8c258d839764078d4b6ef6e8591e0ff5563b31a39e6374d0d41c8c46921c25e5904a817ef8e39e5c9b71225a83269693e0b7e3218fc5e5a1e8412ba16e588b3d6ac536dce39fcdfce81eec79979ea6872793L]
c=[0x10652cdfaa6b63f6d7bd1109da08181e500e5643f5b240a9024bfa84d5f2cac9310562978347bb232d63e7289283871efab83d84ff5a7b64a94a79d34cfbd4ef121723ba1f663e514f83f6f01492b4e13e1bb4296d96ea5a353d3bf2edd2f449c03c4a3e995237985a596908adc741f32365]

data = list(zip(c, n))
x, n = CRT(data)
m = gmpy2.iroot(gmpy2.mpz(x), e)[0].digits()
print('m is: ' + long_to_bytes(m))

22.[BJDCTF2020]这是base??

就是自定义编码而已,没啥特别的,直接上java代码:




public class Base64 {

//    private static final char S_BASE64CHAR[] = {
//        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
//        'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 
//        'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 
//        'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
//        'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 
//        'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', 
//        '8', '9', '+', '/'
//    };
	
  private static final char S_BASE64CHAR[] = {
  'J', 'K','L', 'M', 'N', 'O', 'x', 'y', 'U', 'V', 'z', 
  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', '7', '8', 
  '9', 'P', 'Q', 'I', 'a', 'b', 'c', 'd', 'e', 'f', 
  'g', 'h', 'i', 'j', 'k', 'l', 'm', 'W', 'X', 'Y', 
  'Z', '0', '1', '2', '3', '4', '5', '6', 'R', 'S',
  'T', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 
  'w', '+', '/'
};
    
    


//    private static final char S_BASE64PAD = 61;

    private static final byte S_DECODETABLE[];

    static {
        S_DECODETABLE = new byte[128];
        for(int i = 0; i < S_DECODETABLE.length; i++)
            S_DECODETABLE[i] = 127;

        for(int j = 0; j < S_BASE64CHAR.length; j++)
            S_DECODETABLE[S_BASE64CHAR[j]] = (byte)j;
    }


    private Base64() { }


    public static byte[] decode(String s) {
        char ac[] = new char[4];
        int i = 0;
        byte abyte0[] = new byte[(s.length() / 4) * 3 + 3];
        int j = 0;
        for(int k = 0; k < s.length(); k++) {
            char c = s.charAt(k);
            if(c == '=' ||
               c < S_DECODETABLE.length &&
               S_DECODETABLE[c] != 127)
            {
                ac[i++] = c;
                if(i == ac.length) {
                    i = 0;
                    j += decode0(ac, abyte0, j);
                }
            }
        }

        if(j == abyte0.length) {
            return abyte0;
        } else {
            byte abyte1[] = new byte[j];
            System.arraycopy(abyte0, 0, abyte1, 0, j);
            return abyte1;
        }
    }


    public static String encode(byte abyte0[]) {
        return encode(abyte0, 0, abyte0.length);
    }


    public static String encode(byte abyte0[], int i, int j) {
        if(j <= 0)
            return "";

        char ac[] = new char[(j / 3) * 4 + 4];
        int k = i;
        int l = 0;
        int i1;
        for(i1 = j - i; i1 >= 3; i1 -= 3) {
            int j1 = ((abyte0[k] & 0xff) << 16) + ((abyte0[k + 1] & 0xff) << 8) + (abyte0[k + 2] & 0xff);
            ac[l++] = S_BASE64CHAR[j1 >> 18];
            ac[l++] = S_BASE64CHAR[j1 >> 12 & 0x3f];
            ac[l++] = S_BASE64CHAR[j1 >> 6 & 0x3f];
            ac[l++] = S_BASE64CHAR[j1 & 0x3f];
            k += 3;
        }

        if(i1 == 1) {
            int k1 = abyte0[k] & 0xff;
            ac[l++] = S_BASE64CHAR[k1 >> 2];
            ac[l++] = S_BASE64CHAR[k1 << 4 & 0x3f];
            ac[l++] = '=';
            ac[l++] = '=';
        } else if(i1 == 2) {
            int l1 = ((abyte0[k] & 0xff) << 8) + (abyte0[k + 1] & 0xff);
            ac[l++] = S_BASE64CHAR[l1 >> 10];
            ac[l++] = S_BASE64CHAR[l1 >> 4 & 0x3f];
            ac[l++] = S_BASE64CHAR[l1 << 2 & 0x3f];
            ac[l++] = '=';
        }
        return new String(ac, 0, l);
    }


    private static int decode0(char ac[], byte abyte0[], int i) {
        byte byte0 = 3;
        if(ac[3] == '=')
            byte0 = 2;
        if(ac[2] == '=')
            byte0 = 1;
        byte byte1 = S_DECODETABLE[ac[0]];
        byte byte2 = S_DECODETABLE[ac[1]];
        byte byte3 = S_DECODETABLE[ac[2]];
        byte byte4 = S_DECODETABLE[ac[3]];
        switch(byte0) {
        case 1: // '\001'
            abyte0[i] = (byte)(byte1 << 2 & 0xfc | byte2 >> 4 & 3);
            return 1;

        case 2: // '\002'
            abyte0[i++] = (byte)(byte1 << 2 & 0xfc | byte2 >> 4 & 3);
            abyte0[i] = (byte)(byte2 << 4 & 0xf0 | byte3 >> 2 & 0xf);
            return 2;

        case 3: // '\003'
            abyte0[i++] = (byte)(byte1 << 2 & 0xfc | byte2 >> 4 & 3);
            abyte0[i++] = (byte)(byte2 << 4 & 0xf0 | byte3 >> 2 & 0xf);
            abyte0[i] = (byte)(byte3 << 6 & 0xc0 | byte4 & 0x3f);
            return 3;
        }
        throw new RuntimeException("Internal Errror");
    }


    public static void main(String[] args) {
    	
    	String  a="FlZNfnF6Qol6e9w17WwQQoGYBQCgIkGTa9w3IQKw";
    	
    	byte [] b=null;
    	
    	b=a.getBytes();
    	
    	
    	String encodeString=Base64.encode(b);
    	
    	 System.out.println(encodeString);
    	
    	byte[] decodeByte=Base64.decode(a);
    	 
    	  
    	  
    	 System.out.println(new String(decodeByte));
    	
    	
//        if(args.length != 2) {
//            System.out.println("ERROR: use -encode <string> OR -decode <string>");
//            return;
//        }
//        if(args[0].equals("-encode")) {
//            System.out.println(Base64.encode(args[1].getBytes()));
//        } else if (args[0].equals("-decode")) {
//            System.out.println(new String(Base64.decode(args[1])));
//        } else {
//            System.out.println("ERROR: use -encode <string> OR -decode <string>");
//            return;
//        }
//        
        
        
        
    }

}

运行得到BJD{D0_Y0u_kNoW_Th1s_b4se_map}

23.[MRCTF2020]keyboard

得到的flag用
MRCTF{xxxxxx}形式上叫
都为小写字母

6  
666
22
444
555
33
7
44
666
66
3

手机键盘九宫格

6->m

666->o

22->b

444->i

555->l

33->e

以此类推得到{mobilephone}

24.[MRCTF2020]vigenere

维吉尼亚密码无密钥破解:

代码来自Vigenere的加密和解密、破解

from string import ascii_lowercase as lowercase

# Vigenere加密
def VigenereEncrypto(message,key):
    cipher = ''
    non_alpha_count = 0
    for i in range (len(message)):#遍历
        if message[i].isalpha():#判断是否为字母
            if message[i].islower():##判断是否为小写
                offset = ord(key[(i - non_alpha_count) % len(key)]) - ord('a')
                cipher += chr((ord (message[i]) - ord('a') + offset) % 26 + ord('a'))
            else:#大写字母
                offset = ord(key[(i - non_alpha_count) % len(key)]) - ord('a')
                cipher += chr((ord (message[i]) - ord('A') + offset) % 26 + ord('A'))        
        else:#非字母,就记下
            cipher += message[i]
            non_alpha_count += 1
    return cipher

# Vigenere解密    
def VigenereDecrypto(cipher,key):
    message = ''
    non_alpha_count = 0
    for i in range (len(cipher)):#遍历
        if cipher[i].isalpha():
            if cipher[i].islower():
                offset = ord(key[(i - non_alpha_count) % len(key)]) - ord('a')
                message += chr((ord (cipher[i]) - ord('a') - offset) % 26 + ord('a'))
            else:
                offset = ord(key[(i - non_alpha_count) % len(key)]) - ord('a')
                message += chr((ord (cipher[i]) - ord('A') - offset) % 26 + ord('A'))        
        else:
            message += cipher[i]
            non_alpha_count += 1
    return message

def get_trim_text(text):
    text = text.lower()
    trim_text = ''
    for l in text:
        if lowercase.find(l) >= 0:
            trim_text += l
    return trim_text
     
# 计算重合指数
def get_coincidence_index(text):
    text = get_trim_text(text)
    length = len(text)
    letter_stats = []
    for l in lowercase:
        lt = {}
        count = text.count(l)
        lt[l] = count
        letter_stats.append(lt)
 
    index = 0
    for d in letter_stats:
        v = list(d.values())[0]
        index += (float(v)/length) ** 2
 
    return index
    
# 计算和0.067的差距大小   
def get_var(data, mean=0.067):
    if not data:
        return 0
    var_sum = 0
    for d in data:
        var_sum += (d - mean) ** 2
 
    return float(var_sum) / len(data)
 
# 求秘钥长度
def get_key_length(text):
    # assume text length less than 26
    text = get_trim_text(text)
    group = []
    for n in range(1, len(text)+1):
        group_str = ['' for i in range(n)]
        for i in range(len(text)):
            l = text[i]
            for j in range(n):
                if i % n == j:
                    group_str[j] += l
        group.append(group_str)
 
    var_list = []
    length = 1
    for tex in group:
        data = []
        for t in tex:
            index = get_coincidence_index(t)
            data.append(index)
        var_list.append([length, get_var(data)])
        length += 1
    var_list = sorted(var_list, key=lambda x: x[1])
    print(var_list)
    return [v[0] for v in var_list[:int(n/2)+1]]  #var_list[0][0] 
  
# 统计字母频度
def countList(lis): 
    li = []
    alphabet = [chr(i) for i in range(97,123)]
    for c in alphabet:
        count = 0
        for ch in lis:
            if ch == c:
                count+=1
        li.append(float(count)/len(lis))
    return li

def openfile(fileName): # 读文件
    file = open(fileName,'r')
    text = file.read()
    file.close();
    text = text.replace('\n','')
    return text

# 根据密钥长度将密文分组
def textToList(text,length): 
    text = get_trim_text(text)
    textMatrix = []
    row = []
    index = 0
    for ch in text:
        row.append(ch)
        index += 1
        if index % length ==0:
            textMatrix.append(row)
            row = []
    textMatrix.append(row)
    return textMatrix
    
# 获取密钥
def getKey(text,length): 
    text = get_trim_text(text)
    key = [] # 定义空白列表用来存密钥
    alphaRate =[0.08167,0.01492,0.02782,0.04253,0.12705,0.02228,0.02015,0.06094,\
                0.06996,0.00153,0.00772,0.04025,0.02406,0.06749,0.07507,0.01929,\
                0.0009,0.05987,0.06327,0.09056,0.02758,0.00978,0.02360,0.0015,0.01974,0.00074]
    matrix = textToList(text,length)
    for i in range(length):
        w = [row[i] for row in matrix if len(row) > i] #获取每组密文
        li = countList(w) 
        powLi = [] #算乘积
        for j in range(26):
            Sum = 0.0
            for k in range(26):
                Sum += alphaRate[k]*li[k]
            powLi.append(Sum)
            li = li[1:]+li[:1]#循环移位
        Abs = 100
        ch = ''
        for j in range(len(powLi)):
             if abs(powLi[j] -0.065546)<Abs: # 找出最接近英文字母重合指数的项
                 Abs = abs(powLi[j] -0.065546) # 保存最接近的距离,作为下次比较的基准
                 ch = chr(j+97)
        key.append(ch)
    return key    
     
if __name__ == '__main__':
    key_lengths = []
    c = openfile(r'cipher.txt')
    key_lengths = get_key_length(c)
    print(key_lengths)
    for i in range(len(key_lengths)):
        key = getKey(c, key_lengths[i])
        print("the plaintext is %s, the length of key is %d, key is %s" \
              % (VigenereDecrypto (c , key), key_lengths[i], key))

25.[ACTF新生赛2020]crypto-classic0

提示是生日密码,那就ARCHPR暴力破解密码,纯数字,范围1970000—20201118

爆破得到解压密码19990306。解压得到C代码:

#include<stdio.h>
char flag[25] = ***
int main()
{
	int i;
	for(i=0;i<25;i++)
	{
		flag[i] -= 3;          
		flag[i] ^= 0x7;
		printf("%c",flag[i]);
	}
	return 0; 
}

明文先逐个字符减去3,再和0x7异或,写python运算一下:

#!/usr/bin/python
s = 'Ygvdmq[lYate[elghqvakl}'
sl = list(s)
m = ''
for i in range(0,len(sl)):
	a = (ord(sl[i])^0x7)+3
	m += chr(a)
print(m)

运行得到佛莱格:actf{my_naive_encrytion}

26.[ACTF新生赛2020]crypto-aes

27.[ACTF新生赛2020]crypto-rsa3

使用yafu分解大整数:

然后py脚本:

运行得到佛莱格。

28.[HDCTF2019]basic rsa

送分题:

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值