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()
D:\python\python.exe D:/PyCode/进阶练习/CryptoCTF/keybase.py ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + 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) = 2350ee55955be41ebbceda04271b2d964a147f9ee518b836dad69f3a72923796 | Options: | [G]et the encrypted flag | [T]est the encryption | [Q]uit T | Please send your 32 bytes message to encrypt: 00000000000000000000000000000000 | enc = 4bab****************************cd5e57af792fe0ff52458f9f8f5d2417 | key = eabbe1c4d727f51463e2726fd9a7**** | Options: | [G]et the encrypted flag | [T]est the encryption | [Q]uit
from Crypto.Cipher import AES from string import printable
c = 0x2350ee55955be41ebbceda04271b2d964a147f9ee518b836dad69f3a72923796 key = 0xeabbe1c4d727f51463e2726fd9a70000 c = long_to_bytes(c)
for i inrange(65536): bkey = long_to_bytes(key+i) aes = AES.new(bkey, AES.MODE_CBC, c[:16]) m2 = aes.decrypt(c[16:]) for j in m2: ifchr(j) in printable: f = 1 else: f = 0 break if f == 1: break print(m2) print(bkey) #b'_7He_5eCrET_1V?}' #b"\xea\xbb\xe1\xc4\xd7'\xf5\x14c\xe2ro\xd9\xa7\x0b\x9b"
from Crypto.Util.number import long_to_bytes from Crypto.Cipher import AES from string import printable
c = 0x2350ee55955be41ebbceda04271b2d964a147f9ee518b836dad69f3a72923796 key = 0xeabbe1c4d727f51463e2726fd9a70000 c = long_to_bytes(c)
for i inrange(65536): bkey = long_to_bytes(key+i) aes = AES.new(bkey, AES.MODE_CBC, c[:16]) m2 = aes.decrypt(c[16:]) for j in m2: ifchr(j) in printable: f = 1 else: f = 0 break if f == 1: break
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)
from string import printable m="" w=#太长了这里就不放了 result = [[W[j][i] for j inrange(len(W))] for i inrange(len(W[0]))] for j inrange(55): for i in printable: f = 0 may_group = [] for R inrange(127): test_i = (ord(i)*(j+1)*R)%8443 may_group.append(test_i) for w in result[j]: if w notin may_group: f = 1 if f == 0: m += i print(m) #CCTF{H0w_f1Nd_th3_4lL_3I9EnV4Lu35_iN_FiN173_Fi3lD5!???}
from Crypto.Util.number import long_to_bytes,bytes_to_long f = open("g.enc","rb") a = 257 b = 263 c = 67 cipher = bytes_to_long(f.read()) arr_m = [] while cipher: arr_m.append(cipher % 5) cipher //= 5 arr_m = arr_m[::-1] for i inrange(len(arr_m) - c - 1, -1, -1): arr_m[i] -= arr_m[i+c] arr_m = arr_m[67:] arr_m = arr_m[:256] for i inrange(len(arr_m) - 2, -1, -1): arr_m[i] -= arr_m[i + 1] m = '' for i in arr_m: m += str(i) flag = long_to_bytes(int(m, 2)) print(flag)
from Crypto.Util.number import long_to_bytes,bytes_to_long f = open("g.enc","rb") a = 257 b = 263 c = 67 cipher = bytes_to_long(f.read()) arr_m = [] while cipher: arr_m.append(cipher % 5) cipher //= 5 arr_m = arr_m[::-1] for i inrange(len(arr_m) - c - 1, -1, -1): arr_m[i] -= arr_m[i+c] arr_m = arr_m[67:] arr_m = arr_m[:256] for i inrange(len(arr_m) - 2, -1, -1): arr_m[i] -= arr_m[i + 1] m = '' for i in arr_m: m += str(i) flag = long_to_bytes(int(m, 2)) print(flag) #CCTF{_how_finD_7h1s_1z_s3cr3T?!}