decrypted = [] final = b'' for i inrange(len(encrypted)//2): now = decrypt(encrypted[2*i:2*(i+1)], key) decrypted += now final += long_to_bytes(now[0])[::-1] + long_to_bytes(now[1])[::-1]
import random import time from Crypto.Util.number import * from tqdm import *
# About 3 seconds to run defAMM(o, r, q): start = time.time() print('\n----------------------------------------------------------------------------------') print('Start to run Adleman-Manders-Miller Root Extraction Method') print('Try to find one {:#x}th root of {} modulo {}'.format(r, o, q)) g = GF(q) o = g(o) p = g(random.randint(1, q)) while p ^ ((q-1) // r) == 1: p = g(random.randint(1, q)) print('[+] Find p:{}'.format(p)) t = 0 s = q - 1 while s % r == 0: t += 1 s = s // r print('[+] Find s:{}, t:{}'.format(s, t)) k = 1 while (k * s + 1) % r != 0: k += 1 alp = (k * s + 1) // r print('[+] Find alp:{}'.format(alp)) a = p ^ (r**(t-1) * s) b = o ^ (r*alp - 1) c = p ^ s h = 1 for i inrange(1, t): d = b ^ (r^(t-1-i)) if d == 1: j = 0 else: print('[+] Calculating DLP...') j = - discrete_log(a, d) print('[+] Finish DLP...') b = b * (c^r)^j h = h * c^j c = c ^ r result = o^alp * h end = time.time() print("Finished in {} seconds.".format(end - start)) print('Find one solution: {}'.format(result)) return result
deffindAllPRoot(p, e): print("Start to find all the Primitive {:#x}th root of 1 modulo {}.".format(e, p)) start = time.time() proot = set() whilelen(proot) < e: proot.add(pow(random.randint(2, p-1), (p-1)//e, p)) end = time.time() print("Finished in {} seconds.".format(end - start)) return proot
deffindAllSolutions(mp, proot, cp, p, e): print("Start to find all the {:#x}th root of {} modulo {}.".format(e, cp, p)) start = time.time() all_mp = set() for root in proot: mp2 = mp * root % p assert(pow(mp2, e, p) == cp) all_mp.add(mp2) end = time.time() print("Finished in {} seconds.".format(end - start)) return all_mp n = 211174039496861685759253930135194075344490160159278597570478160714793843648384778026214533259531963057737358092962077790023796805017455012885781079402008604439036453706912819711606916173828620000813663524065796636039272173716362247511054616756763830945978879273812551204996912252317081836281439680223663883250992957309172746671265758427396929152878633033380299036765665530677963287445843653357154379447802151146728382517702550201 c = 191928992610587693825282781627928404831411364407297375816921425636703444790996279718679090695773598752804431891678976685083991392082287393228730341768083530729456781668626228660243400914135691435374881498580469432290771039798758412160073826112909167507868640830965603769520664582121780979767127925146139051005022993085473836213944491149411881673257628267851773377966008999511673741955131386600993547529438576918914852633139878066 p = 31160882390461311665815471693453819123352546432384109928704874241292707178454748381602275005604671000436222741183159072136366212086549437801626015758789167455043851748560416003501637268653712148286072544482747238223 q = 6776895366785389188349778634427547683984792095011326393872759455291221057085426285502176493658280343252730331506803173791893339840460125807960788857396637337440004750209164671124188980183308151635629356496128717687 e = 1009 * 7
# gcd(e,p-1)=7063 # gcd(e,q-1)=1009
# p cp = c % p mp = AMM(cp, e, p) p_proot = findAllPRoot(p, e) mps = findAllSolutions(mp, p_proot, cp, p, e) print(len(mps))
start = time.time() print('Start CRT...') for mpp in tqdm(mps): for mqq in mqs: m = CRT_list([int(mpp), int(mqq)], [p, q]) res = long_to_bytes(int(m)) ifb'HECTF'in res: print(res) #print(time.time() - start)
end = time.time() print("Finished in {} seconds.".format(end - start))
n = 404647938065363927581436797059920217726808592032894907516792959730610309231807721432452916075249512425255272010683662156287639951458857927130814934886426437345595825614662468173297926187946521587383884561536234303887166938763945988155320294755695229129209227291017751192918550531251138235455644646249817136993 c = 365683379886722889532600303686680978443674067781851827634350197114193449886360409198931986483197030101273917834823409997256928872225094802167525677723275059148476025160768252077264285289388640035034637732158021710365512158554924957332812612377993122491979204310133332259340515767896224408367368108253503373778 e = 65537
deft(a, b, k): # sqrt(n) has 155 digits, so we need to figure out 77 digits on each side if k == 77: if a*b == n: print((a,b)) return for i inrange(10): for j inrange(10): # we try to guess the last not-already-guessed digits of both primes a1 = a + i*(10**k) + j*(10**(154-k)) b1 = b + j*(10**k) + i*(10**(154-k)) if a1*b1 > n: # a1 and b1 are too large continue if (a1+(10**(154-k)))*(b1+(10**(154-k))) < n: # a1 and b1 are too small continue if ((a1*b1)%(10**(k+1))) != (n%(10**(k+1))): # The last digits of a1*b1 (which won't change later) doesn't match n continue # this a1 and b1 seem to be a possible match, try to guess remaining digits t(a1, b1, k+1)
# the primes have odd number of digits (155), so we try all possible middle digits (it simplifies the code) #for i in range(10): # t(i*(10**77), i*(10**77), 0)
p,q = (39316409865082827891559777929907275271727781922450971403181273772573121561800306699150395758615464222134092274991810028405823897933152302724628919678029201, 10292087691982642720325133979832850482001819947229043122246451685759305199660300816512137527737218130417905422918772717257270992977795519872828056890461393) f = (p-1)*(q-1) d = inverse(e,f) m = pow(c,d,n) print(long_to_bytes(m))
from Crypto.Util.number import * from tqdm import * from hashlib import * from Crypto.Cipher import AES import itertools
# from https://github.com/defund/coppersmith/blob/master/coppersmith.sage defsmall_roots(f, bounds, m=1, d=None): ifnot d: d = f.degree()
R = f.base_ring() N = R.cardinality() f /= f.coefficients().pop(0) f = f.change_ring(ZZ)
G = Sequence([], f.parent()) for i inrange(m+1): base = N^(m-i) * f^i for shifts in itertools.product(range(d), repeat=f.nvariables()): g = base * prod(map(power, f.variables(), shifts)) G.append(g)
factors = [monomial(*bounds) for monomial in monomials] for i, factor inenumerate(factors): B.rescale_col(i, factor)
B = B.dense_matrix().LLL()
B = B.change_ring(QQ) for i, factor inenumerate(factors): B.rescale_col(i, 1/factor)
H = Sequence([], f.parent().change_ring(QQ)) for h infilter(None, B*monomials): H.append(h) I = H.ideal() if I.dimension() == -1: H.pop() elif I.dimension() == 0: roots = [] for root in I.variety(ring=ZZ): root = tuple(R(root[var]) for var in f.variables()) roots.append(root) return roots
return []
p = 9604080254440553624043823039323876524034439909584709693304859297324410855942111467832096190746534800378359779991381701244554754870303658957438266614583487 q = 7117529167860499983120234872664469946810713755399747931099511148595647881645694071900284496403308583631053530870961375928947111857317803005696543076720079 a = 4681007517868949260473646867708411042804596292653498068045093108939357065240201843535644313612886376810286247810943227474659270191834401055704514648846995 b = 5604862515726338933576748414825616582947323501967288114322080747741801017833194347273532400730033226601964489467416955741018175785792514035352083708135431 x1 = 5544706922427110224110125906620053049906095568886481576326706308027915868515721429471522223193053363494813044921519216114372968191072598748704528735817403 Ep_point = (0x2fa8e23f18ed4a9bd752a0c22b0750c17fbb66c76554e2089258fd979a5736b7766c974fb9788acf17fb065dc1daec6a8a6e98021de6c4ce3cde11dd54590e1d, 0xa3ce4bb1e25563b577a45cd06153d2dab584a70130c7ae71e65fe5e11b60493ccb845fbe4989dbd4a60d6a1ff12baa268b8833ed30f7c7e21c32268a139b5b6b) c = [36780810764729391947601691590378765170863850291763672158886689602006275675399596108959250284869355070618680265311484525337488013177333417742808496794250706127014303883956401715343247310936978778751394980638177344654524711571648231122027699452582302505466999915200896495338587961829985149664712686944510559820, 20958199004445348755624931477686903609410629089817702686793041731031202915294487428236505796231417377524290926704880107242252471250791747709149963693453815320856114055076830778689575609444155241642860745570792018879816650383543271943138193405548674967958109800776284787612370057476837642989670234913968669332, 19758181515666300263334531148587391869707566215385658759724970483060039216682585723722462835458856503531814316860237786892749700501436669071048571605926728917066797641628644730857333648930286503355701843365288276242984029888215453858844295912023305616753086127934173496355853797241944921600781294012353332277, 45576628433681427718167093217006549620067042472164439269014690121698560736312716407875326404496263261341269644373184438703912129559084380247641072914940830606649124606611794031719696797961847217643536070335745057048220615012019629278484208808353027070994021979997462190775853832457224157083880895894000484461]
n = p*q Ep = EllipticCurve(GF(p), [a, 0]) y1 = Ep.lift_x(x1).xy()[1] G = Ep(x1,y1) P = Ep(Ep_point)
''' G2 = G for k in trange(1,60000): if G2 == P: print(k) break G2 += G ''' # 51517
key = 51517 assert P == key*G
k = md5(long_to_bytes(key)).hexdigest().encode() aes = AES.new(k, AES.MODE_ECB) En = EllipticCurve(Zmod(n), [a, b])
first_rev = '0d30000102bc048230010df78648862a090630a604820400050101018202000102a20482238d771399dbbd0001416e7fbd7f589b44151fb0c984549c1c4d948984f85c35934453bfbb4dbb99420f63f9cb8f577a7b60d67bdf84166c500fa7d6c6e484b75d97e308151b31a49c5bd77dd1d2d6711e50d9f5f7bc37d4e2235cfd76a522713c766e240aa2eb5fb45a9015d1dbd6a58b5b28861233ae4eb75a97ced0b78e91024d195adde08f7430b7f160d1d60a4f9d0801db74ebc8c23f9397251faac5500d216acc623e8f6ab212b5e6e9495d5ef6cee995fea98f40b1db2d356dd3c4d2612c64a1295bb23936fad66dc5662cb4ba6a8929591f6b14ce30d67df5ec35edb1f0973f746bcc5fc1ca921ee9660e04c6f286677b92e12b61ba310501030219e5085e5254046204000182020100' first = ''.join([bytes.fromhex(first_rev[18*i:18*(i+1)])[::-1].hex() for i inrange(len(first_rev)//18)]) print(first)
【d mod (p-1)】 028180 51a5f7e7f4c050a50e18fde12fcee2646f2b43160b0c75ab4925e8269ae80e70cf12734f41fab18d0424ed7cceb7ddb27cbe0f554f7a6e1698d4ec5ba2b48d612e2337aeb75f8a57d8155a11d07b2c49d3d97c4ff0cfb89e6dd4f36cc37c010b5bc89356a39b576cc3edd03cdc4d791df5091a5571df1a6c15eedaa0773cf3cf
【d mod (q-1)】 028180 0fc61f05d19c96eec3edcacca34e1d3e2cab439bebab6693a3ce2ca99f88ab9cdd183ceb8e801d8298f835359864ef191db3f53269976ba04b03606e540859decd05805c4aa79dc6db22380658eaf0bffba0f4e719bcf1b1e04169d8e0cb3af4d90b2e62d7c7ed3045d49b525ca715ca3b84f07b4ece27d04d1795299fa186cd
【(inverse of q) mod p】 028180 25c20ab2529f1efd3d35347c573b282abfd95b264c92f6c4f9ec8b7c713206fbea1886880e29a36c47ef9bb753ce9567ea4d3e083c30f344022f95b7cd7114813bf6a28ecc67d5fe05953242684cd29c1c5dd8a74416890e5c943c70904ba70e349b15719a466f901fbf0cfc7840f8032e31afbccfb84f4a817ea51c8f90fd6a
from Crypto.Util.number import * from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP
n = 0x00bddb9913778d2315449b587fbd7f6e41944d1c9c5484c9b01fbf534493355cf88489cbf9630f4299bb4dbb84df7bd6607b7a578f84e4c6d6a70f506c16a4311b1508e3975db71e71d6d2d17dd75b9c23e2d437bcf7f5d9506e763c7122a576fd5c15905ab45feba20a241286285b8ba5d6dbd1b7d0ce975ab74eae338fe0dd5a194d02918e4f0ad6d160f1b730743fc2c8eb74db01089d210d50c5aa1f259793b512b26a8f3e62cc6a95e9cef65e5d49e9e66d352ddbb1408fa9fe5b29a1642c61d2c4d32c66c56dd6fa3639b2146b1f5929896abab4b1ed35ecf57dd630cecac15fcc6b743f97f086f2c6040e66e91e920531ba612be1927b6754525e08e519 e = 0x010001 dp = 0x51a5f7e7f4c050a50e18fde12fcee2646f2b43160b0c75ab4925e8269ae80e70cf12734f41fab18d0424ed7cceb7ddb27cbe0f554f7a6e1698d4ec5ba2b48d612e2337aeb75f8a57d8155a11d07b2c49d3d97c4ff0cfb89e6dd4f36cc37c010b5bc89356a39b576cc3edd03cdc4d791df5091a5571df1a6c15eedaa0773cf3cf dq = 0x0fc61f05d19c96eec3edcacca34e1d3e2cab439bebab6693a3ce2ca99f88ab9cdd183ceb8e801d8298f835359864ef191db3f53269976ba04b03606e540859decd05805c4aa79dc6db22380658eaf0bffba0f4e719bcf1b1e04169d8e0cb3af4d90b2e62d7c7ed3045d49b525ca715ca3b84f07b4ece27d04d1795299fa186cd c = 0x6f4cb0df50eb133f104727316eb23e25463b6b46a1ff743507c7663094da88a091c77c1d686a91613fa2da697c23924798b40654ba420b9690c5ceb9a362cf48e72c39177c6a3ebecc4e0ba2b9673f070a23e535fff7a01b400381ede60a6f9bf86047b3dd2c663c329b9287749bdb3783303802129b93af083aa2045c500fe0c2a7a018c2403881115927ae56ff14338b9fb98d5a5f461916d962aea7c3379ec7f7d8d77b4cc8ff756895c1500d9f2cee3552f17216339b0d67f27e0dd07e9ec1861f14c962b977559561d709d57e58fd6e6aafd27892c6d43d16b3db267902b9ce8f9ff89a66ab822b5ea3a68c872a32c69961df15581b70c00e4c61804d0d
for x inrange(1, e): if e * dp % x == 1: p = (e * dp - 1) // x + 1 if n % p == 0: q = n // p break # print(p) # print(q) assert p * q == n f = (p-1)*(q-1) d = inverse(e,f)
rsa_components = (n, e, d, p, q) myrsa = RSA.construct(rsa_components) rsakey = RSA.importKey(myrsa.exportKey()) rsakey = PKCS1_OAEP.new(rsakey) m = rsakey.decrypt(long_to_bytes(c)) print(m.decode())
withopen("oringe.zip","rb") as f: content = f.read()
key = b"abcdefghijklmnop" iv = b"qwertyuiopasdfgh"
en = encrypt(key,iv) withopen("zip2.zip","wb") as f: f.write(en)
解密data即可:
1 2 3
key = b"abcdefghijklmnop" iv = b"qwertyuiopasdfgh" c = 'cf5663613ce2bbd8ef6d3c522bcafa667ed985d67ba6ca2a21ce970c59e331dd7e090f248786c0ef8764395f1bb0aac136f1397d0a18ff55546473107fa7e5a385855cad91dda16ba4951e2f6618995b6faeecfaa1da7d3bd6ec66a4f57d6fae638a85da1cfdad89941512e9a0f7af49d33e285f4b56d66702ad4b4c1676824451125aaca20c1c74f9954d9546f9b5c059a990aae074c255cccf2866058ffa51159d6d0d67bfdbb3e233d27cb481709dd07fede7c9b28be6d7d581f2b7d476616cbc14a8133a23392eac733336d59cb42d6f5fe9a500eb340cf19dca8d6ddb47117d5a332f47b7fc260f4d1e1b8a2654b52d6403dc1376be22a6dcb9d8a72321acac5f63b59c8b82190629f644ee98757124d7883bfbe41df9d0b524e736da6bd0b54b37ce8c9b4f583b534052233a1a27f96be446e2f7e88eb2f8b4e0d84438546098c149d9ed511356726ff2bfdbdf'
#yourkey dic = '0123456789abcdef' p = '' for i inrange(64): f = open(f'secretkey/{i}','r').read() for k in dic: if hashlib.md5(k.encode()).hexdigest() == f: p += k break print(p) password = bytes.fromhex(p) print(password)
withopen(r'MSG0.db', 'rb') as f: blist = f.read() print(len(blist)) salt = blist[:16]#微信将文件头换成了盐 key = hashlib.pbkdf2_hmac('sha1', password, salt, DEFAULT_ITER, KEY_SIZE)#获得Key first = blist[16:DEFAULT_PAGESIZE]#丢掉salt # import struct mac_salt = bytes([x^0x3afor x in salt]) mac_key = hashlib.pbkdf2_hmac('sha1', key, mac_salt, 2, KEY_SIZE) hash_mac = hmac.new(mac_key ,digestmod = 'sha1')#用第一页的Hash测试一下 hash_mac.update(first[:-32]) hash_mac.update(bytes(ctypes.c_int(1))) # hash_mac.update(struct.pack('=I',1)) if (hash_mac.digest() == first[-32:-12]): print('Correct Password') else: raise RuntimeError('Wrong Password') blist = [blist[i:i+DEFAULT_PAGESIZE] for i inrange(DEFAULT_PAGESIZE,len(blist),DEFAULT_PAGESIZE)] withopen(r'out.db', 'wb') as f: f.write(SQLITE_FILE_HEADER)#写入文件头 t = AES.new(key ,AES.MODE_CBC ,first[-48:-32]) f.write(t.decrypt(first[:-48])) f.write(first[-48:]) for i in blist: t = AES.new(key ,AES.MODE_CBC ,i[-48:-32]) f.write(t.decrypt(i[:-48])) f.write(i[-48:])