from Crypto.Util.number import * from base64 import * cipher='805c9GMYuD5RefTmabUNfS9N9YrkwbAbdZE0df91uCEytcoy9FDSbZ8Ay8jj' alphabet='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\=' message='' for i in cipher: C=alphabet.index(i) if(C==0): message+='0' elif(C+44>=64): message+=alphabet[(C+44)%64+1] else: message+=alphabet[C+44] print(b64decode(message)) #CCTF{EnCrYp7I0n_4nD_5u8STitUtIn9_iN_Fi3Ld!}
#!/usr/bin/env python3 import random from flag import FLAG p = 8443 deftranspose(x): result = [[x[j][i] for j inrange(len(x))] for i inrange(len(x[0]))] return result defvsum(u, v): assertlen(u) == len(v) l, w = len(u), [] for i inrange(l): w += [(u[i] + v[i]) % p] return w defsprod(a, u): w = [] for i inrange(len(u)): w += [a*u[i] % p] return w defencrypt(msg): l = len(msg) hyper = [ord(m)*(i+1) for (m, i) inzip(list(msg), range(l))] V, W = [], [] for i inrange(l): v = [0]*i + [hyper[i]] + [0]*(l - i - 1) V.append(v) random.shuffle(V) for _ inrange(l): R, v = [random.randint(0, 126) for _ inrange(l)], [0]*l for j inrange(l): v = vsum(v, sprod(R[j], V[j])) W.append(v) random.shuffle(transpose(W)) return W enc = encrypt(FLAG) print('W=',enc) #输出文件保存在given.py内
from given import W from Crypto.Util.number import * from string import printable p=8443 arr=[] S="" for _t inrange(55): print(_t) print(S) arr=[] for __r inrange(55): arr.append(W[__r][_t]) #print(arr) for i in printable: #for j in range(1,56): for j in [_t+1] T,fail=[],0 for k inrange(127): checker=(ord(i)*j*k)%8443 T.append(checker) for k in arr: if k notin T: fail=1 if(fail==0): S+=i print(S) #CCTF{H0w_f1Nd_th3_4lL_3I9EnV4Lu35_iN_FiN173_Fi3lD5!???}
#!/usr/bin/env python from Crypto.Util.number import * from flag import FLAG defnextPrime(n): whileTrue: n += (n % 2) + 1 if isPrime(n): return n f = [int(x) for x inbin(int(FLAG.hex(), 16))[2:]] f.insert(0, 0) for i inrange(len(f)-1): f[i] += f[i+1] a = nextPrime(len(f)) b = nextPrime(a) g, h = [[_ for i inrange(x) for _ in f] for x in [a, b]] c = nextPrime(len(f) >> 2) for _ in [g, h]: for __ inrange(c): _.insert(0, 0) for i inrange(len(_) - c): _[i] += _[i+c] g, h = [int(''.join([str(_) for _ in __]), 5) for __ in [g, h]] for _ in [g, h]: if _ == g: fname = 'g' else: fname = 'h' of = open(f'{fname}.enc', 'wb') of.write(long_to_bytes(_)) of.close() #输出文件g.enc 19115字节 #输出文件f.enc 19561字节
from Crypto.Util.number import * Cg=open("g.enc","rb").read() Cg=bytes_to_long(Cg) Garr=[] while Cg: Garr.append(Cg%5) Cg//=5 for i inrange(67,len(Garr)): Garr[i]=Garr[i]-Garr[i-67] Garr=Garr[::-1][67:] for i inrange(256*256): assert Garr[i]==Garr[i+256] Garr=Garr[:256] for i inrange(254,-1,-1): Garr[i]-=Garr[i+1] assert Garr.count(2)==0 m=0 for i in Garr: m<<=1 m|=i print(long_to_bytes(m)) #CCTF{_how_finD_7h1s_1z_s3cr3T?!}
4.hamul
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
from Crypto.Util.number import * from flag import flag nbit = 64 whileTrue: p, q = getPrime(nbit), getPrime(nbit) P = int(str(p) + str(q)) Q = int(str(q) + str(p)) PP = int(str(P) + str(Q)) QQ = int(str(Q) + str(P)) if isPrime(PP) and isPrime(QQ): print(PP,QQ) break n = PP * QQ m = bytes_to_long(flag.encode('utf-8')) if m < n: c = pow(m, 65537, n) print('n =', n) print('c =', c)
from Crypto.Util import number from Crypto.Cipher import AES import os, sys, random from flag import flag defkeygen(): iv, key = [os.urandom(16) for _ in'01'] return iv, key defencrypt(msg, iv, key): aes = AES.new(key, AES.MODE_CBC, iv) return aes.encrypt(msg) defdecrypt(enc, iv, key): aes = AES.new(key, AES.MODE_CBC, iv) return aes.decrypt(enc) defdie(*args): pr(*args) quit() defpr(*args): s = " ".join(map(str, args)) sys.stdout.write(s + "\n") sys.stdout.flush() defsc(): return sys.stdin.readline().strip() defmain(): border = "+" pr(border*72) pr(border, " hi all, welcome to the simple KEYBASE cryptography task, try to ", border) pr(border, " decrypt the encrypted message and get the flag as a nice prize! ", border) pr(border*72) iv, key = keygen() flag_enc = encrypt(flag, iv, key).hex() whileTrue: pr("| Options: \n|\t[G]et the encrypted flag \n|\t[T]est the encryption \n|\t[Q]uit") ans = sc().lower() if ans == 'g': pr("| encrypt(flag) =", flag_enc) elif ans == 't': pr("| Please send your 32 bytes message to encrypt: ") msg_inp = sc() iflen(msg_inp) == 32: enc = encrypt(msg_inp, iv, key).hex() r = random.randint(0, 4) s = 4 - r mask_key = key[:-2].hex() + '*' * 4 mask_enc = enc[:r] + '*' * 28 + enc[32-s:] pr("| enc =", mask_enc) pr("| key =", mask_key) else: die("| SEND 32 BYTES MESSAGE :X") elif ans == 'q': die("Quitting ...") else: die("Bye ...") if __name__ == '__main__': main()
huangx607087@huangx607087-virtual-machine:~/桌面/CTF/Apr-Jul 21$ python 7.py [+] Opening connection to 01.cr.yp.toc.tf on port 17010: Done [*] Switching to interactive mode ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + hi all, welcome to the simple KEYBASE cryptography task, try to + + decrypt the encrypted message and get the flag as a nice prize! + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | Options: | [G]et the encrypted flag | [T]est the encryption | [Q]uit $ g | encrypt(flag) = b27e9095ded98a14bdb678e3a30fbbfc79b9cefc9a71b2c963804ce692e0b61e | Options: | [G]et the encrypted flag | [T]est the encryption | [Q]uit $ t | Please send your 32 bytes message to encrypt: $ 00000000000000000000000000000000 | enc = 9a****************************3d531aec9dfc944f408d4df08e8f25aeaa | key = 52ab970fd21f2526aa7421630068**** | Options: | [G]et the encrypted flag | [T]est the encryption | [Q]uit [*] Got EOF while reading in interactive