diff --git a/Attack.py b/Attack.py index d82655a..8d9c306 100644 --- a/Attack.py +++ b/Attack.py @@ -1,144 +1,80 @@ -#!/usr/bin/env python2 -#-*- coding: utf-8 -*- -# -# author : X-Vector -# Tested on Kali Linux / Parrot Os / Ubuntu -# Simple script for RSA Attack -import sys -import platform,os -from urllib2 import * -from platform import system -def slowprint(s): - for c in s + '\n': - sys.stdout.write(c) - sys.stdout.flush() - time.sleep(4. / 100) - -clear = "" -if "Windows" in platform.system(): - clear = "cls" -if "Linux" in platform.system(): - clear = "clear" -os.system(clear) -is_windows = sys.platform.startswith('win') - -# Console Colors -if is_windows: - # Windows deserves coloring too :D - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - try: - import win_unicode_console , colorama - win_unicode_console.enable() - colorama.init() - #Now the unicode will work ^_^ - except: - print("[!] Error: Coloring libraries not installed, no coloring will be used") - G = Y = B = R = W = G = Y = B = R = W = '' - - -else: - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - - -def banner(): - print("""%s - -_____ ________________ _____ _____ -\ \ / /\ \ _____\ \ / |_ - \ | | / \ /\ \ / / \ | / \\ - \ \ / / | \_\ | | | /___/|| /\ \\ - \ | / | ___/ ____\ \ | ||| | | \\ - / | \ | \ ____ / /\ \|___|/| \/ \\ - / /|\ \ / /\ \/ \| |/ \ \ |\ /\ \\ - |____|/ \|____| /_____/ |\______||\____\ /____/| | \_____\ \_____\\ - | | | | | | | | || | || | | | | | | | - |____| |____| |_____|/ \|_____| \|___||____|/ \|_____|\|_____| - %s%s -[ Version : 0.2 ]\033[92m -[ Author : X-Vector ]\033[96m -[ Github : github.com/X-Vector ]\033[93m -[ Twitter : twitter.com/@XVector11 ]\033[95m -[ Facebook: facebook.com/X.Vector1 ]\033[95m -[ GreeteZ : Karem Ali ]\033[94m - """ % (R, W,R)) +from banner import * banner() - print(""" +[1] - Attacks +[2] - Factorization Number +""") +check = int(input(">>> ")) +if check == 2: + import Factorization + +elif check == 1: + banner() + print(""" [1] - Attack(c,n,e) \033[92m [2] - Attack(c,p,q,e) \033[96m [3] - Attack (c,n,e,{p or q}) [4] - Attack (c,n,e,dp) [5] - Attack(c,n,d,e,phi) \033[93m -[6] - Attack(c1,c2,c3,n1,n2,n3) \033[95m [ Hasted ] -[7] - Attack(c1,c2,c3,n1,n2,n3,e) \033[0m [ Hasted2 ] -[8] - Attack(n,limit) \033[92m [ Fermat ] -[9] - Attack(c1,c2,e1,e2,n) \033[96m [ Common Modulus ] -[10] - Attack(c,p,q,dp,dq) \033[93m [ Chinese Remainder Theorem ] -[11] - Attack(n,e) \033[95m [ Wiener ] -[12] - Attack(c,n,d)\033[0m -[13] - Attack(c,d,n,e)\033[92m -[14] - Attack(c,n,e) \033[93m [ Multi Prime Number ] -[15] - Attack(c,e = 3)\033[0m -[16] - Get(n,p,q,d,e) From PublicKey\033[96m +[6] - Attack(c1,c2,c3,n1,n2,n3,e=3) \033[95m [ Hasted ] +[7] - Attack(c1,c2,e1,e2,n) \033[96m [ Common Modulus ] +[8] - Attack(c,p,q,dp,dq) \033[93m [ Chinese Remainder Theorem ] +[9] - Attack(n,e) \033[95m [ Wiener ] +[10] - Attack(c,n,d)\033[0m +[11] - Attack(c,d,n,e)\033[92m +[12] - Attack(c,n,e) \033[93m [ Multi Prime Number ] +[13] - Attack(c,e = 3)\033[0m +[14] - Attack(c1,c2,n1,n2,e) \033[96m [ Common Factor ] +[15] - Attack(c,n,e) \033[95m [ boneh durfee ] [0] - Exit \033[92m - """) -x = int(input(">>> ")) -if x == 1: - os.system(clear) - import RSA -elif x == 2: - os.system(clear) - import RSA2 -elif x == 3: - os.system(clear) - import RSA6 -elif x == 4: - os.system(clear) - import RSA7 -elif x == 5: - os.system(clear) - import RSA3 -elif x == 6: - os.system(clear) - import RSA_hasted -elif x == 7: - os.system(clear) - import RSA_hasted2 -elif x == 8: - os.system(clear) - import RSA_fermat -elif x == 9: - os.system(clear) - import RSA_common_modulus -elif x == 10: - os.system(clear) - import RSA_chinese_remainder_theorem -elif x == 11: - os.system(clear) - import RSA_wiener -elif x == 12: - os.system(clear) - import RSA4 -elif x == 13: - os.system(clear) - import RSA5 -elif x == 14: - os.system(clear) - import RSA_multiPrime1 -elif x == 15: - os.system(clear) - import RSA8 -elif x == 16: - os.system(clear) - import public + """) + x = int(input(">>> ")) + if x == 1: + os.system(clear) + import Attacks.RSA + elif x == 2: + os.system(clear) + import Attacks.RSA2 + elif x == 3: + os.system(clear) + import Attacks.RSA6 + elif x == 4: + os.system(clear) + import Attacks.RSA7 + elif x == 5: + os.system(clear) + import Attacks.RSA3 + elif x == 6: + os.system(clear) + import Attacks.RSA_hasted + elif x == 7: + os.system(clear) + import Attacks.RSA_common_modulus + elif x == 8: + os.system(clear) + import Attacks.RSA_chinese_remainder_theorem + elif x == 9: + os.system(clear) + import Attacks.RSA_wiener + elif x == 10: + os.system(clear) + import Attacks.RSA4 + elif x == 11: + os.system(clear) + import Attacks.RSA5 + elif x == 12: + os.system(clear) + import Attacks.RSA_multiPrime1 + elif x == 13: + os.system(clear) + import Attacks.RSA8 + elif x == 14: + os.system(clear) + import Attacks.RSA_common_exponent + elif x == 15: + os.system(clear) + import Attacks.RSA_boneh_durfee + else: + exit() else: - slowprint("\t\t\t[+] Thanx For using T00l <3") exit() diff --git a/Attacks/RSA.py b/Attacks/RSA.py new file mode 100644 index 0000000..4c6c676 --- /dev/null +++ b/Attacks/RSA.py @@ -0,0 +1,46 @@ +from banner import * +from utilis import egcd,modinv,Convert +from Factorizations.factordb import * +banner() + +""" +# Example : +c = 194048013822218245260658018019940874060627700835842604475987702337533801266490182061968998210807564778328557627772974110046885380635225974269865976518335375789734689098164529086561756412074742698644530189076800227300946408167039318949544794351233987752575608106800908043533012088081995031010618521695843625062 +n = 248501410365662412791489552646042256782092770118253438700194718631291036762726489658495565276550205113648626040596191969135846656414394584577305526761671104277390765264806022908497647300596494542202565022133435383403344333672279722534625284520459706609569974491538689429548817677759350947931780871046796607829 +e = 65537 +""" + +try: + + def factordb(n): + f = FactorDB(n) + f.connect() + return f.get_factor_list() + + c = int(input(">>> c = ")) + n = int(input(">>> n = ")) + e = int(input(">>> e = ")) + + factordb = factordb(n) + q = factordb[0] + p = factordb[1] + phi = (p-1)*(q-1) + d = modinv(e,phi) + decode = pow(c,d,n) + Convert(decode) + +except IndexError: + slowprint("[-] Sorry Can't Factorize n ") + slowprint("\n[!] Try To Use MultiPrime Attack ") +except ImportError: + slowprint("\n[-] Module Not Setup") +except ValueError: + slowprint("\n[-] c, e, n Must Be Integar Number") +except AssertionError: + slowprint("\n[-] Wrong Data") +except KeyboardInterrupt: + exit() +except requests.exceptions.ConnectionError: + slowprint("\n[-] Check Your Internet") +except: + slowprint("False Attack") diff --git a/Attacks/RSA2.py b/Attacks/RSA2.py new file mode 100644 index 0000000..ff67757 --- /dev/null +++ b/Attacks/RSA2.py @@ -0,0 +1,32 @@ +from banner import * +from utilis import Convert,modinv +banner() + +""" +c = 29846947519214575162497413725060412546119233216851184246267357770082463030225 +p = 238324208831434331628131715304428889871 +q = 296805874594538235115008173244022912163 +e = 3 +""" + +try: + c = int(input(">>> c = ")) + p = int(input(">>> p = ")) + q = int(input(">>> q = ")) + e = int(input(">>> e = ")) + + n = p*q + phi = (p-1)*(q-1) + d = modinv(e,phi) + m = pow(c,d,n) + Convert(m) + +except ImportError: + slowprint("\n[-] Module Not Setup") +except ValueError: + slowprint("\n[-] c,p,q,e Must Be Integar Number") +except AssertionError: + slowprint("\n[-] Wrong Data") +except KeyboardInterrupt: + exit() + diff --git a/Attacks/RSA3.py b/Attacks/RSA3.py new file mode 100644 index 0000000..2da1e53 --- /dev/null +++ b/Attacks/RSA3.py @@ -0,0 +1,28 @@ +from banner import * +from utilis import Convert +banner() + +# Note : Check if phi , n has the same p,q +""" +c = 37209877026138824302301697292319328185824059912506644719041273895972747926698556612194455367172368277268883774102719113194850674012999971337550561061697826458468363574428378679179721672530515881761949297613967812683948622068020382771205609975220407119022966600675469044732891210789248284410739482876937857726026431593283960162298707892143970513804892248370427455318472402011536095802255121284911517882268092785246985981687913310965628793178703498961617628661113277249071490584774764571170891391355080033791745662119139170834529306548644716069025161989103880671580075279656495472299747401794635781847901588156099495017 +n = 43210326036290106251088810298667915702007744567467854988107937233622821671752930583378558808381197036542903396534337305723496107154050324953536530993744267386008120658910242143992656773360792182698142273761182047011119261779988377408208858109503177413194541524543447827777831409903664749520527375514748678462101261445039900408999429969510786960471887158957679337190091527913340383310225813322177657695256994315048270952315001100548949554323965404643005206732777361386623591544461533797263652780595639288158892122219626720481472616420118039436680647298714768071797941290307081010086729667552569828323354008441335674251 +d = 36745622940545343875759976434157197886755506358760982257308567037006074391719705315666781200065625116203199598633622026070492775743618607966652393996419663853350085914252797887742782661594880295499227386075929594641556658033207382848075073319786244161193801795105901007640174254798831863226544268004149920625395925259715625248417078457317167458592444532825039828013768181860349705192246622240475711133444054985367520564542488697167606480838294747710396401026533820191299116891186474240520253953309474166963956334430798860084072450328931700881244717326902973061667440442315315084591766881188371668945135391290476758145 +e = 65537 +phi = 43210326036290106251088810298667915702007744567467854988107937233622821671752930583378558808381197036542903396534337305723496107154050324953536530993744267386008120658910242143992656773360792182698142273761182047011119261779988377408208858109503177413194541524543447827777831409903664749520527375514748678461684001179681025836243272641520046001108394411608315423967170123709569701951917513847951924936347574671212789694222728087028519090194131250999358518159691864169187723815719657688598576820104123456547706995794001020837756695923476582226622677087727699446323459489485731800382017980557710365098279949382831681952 +""" + +try: + c = int(input(">>> c = ")) + n = int(input(">>> n = ")) + d = int(input(">>> d = ")) + e = int(input(">>> e = ")) + phi = int(input(">>> phi = ")) + m = pow(c,d,n) + Convert(m) +except ValueError: + slowprint("\n[-] c,n,d,e,phi Must Be Integar Number") +except AssertionError: + slowprint("\n[-] Wrong Data") +except KeyboardInterrupt: + exit() + diff --git a/Attacks/RSA4.py b/Attacks/RSA4.py new file mode 100644 index 0000000..70d0202 --- /dev/null +++ b/Attacks/RSA4.py @@ -0,0 +1,23 @@ +from banner import * +from utilis import Convert +banner() +""" +n = 744818955050534464823866087257532356968231824820271085207879949998948199709147121321290553099733152323288251591199926821010868081248668951049658913424473469563234265317502534369961636698778949885321284313747952124526309774208636874553139856631170172521493735303157992414728027248540362231668996541750186125327789044965306612074232604373780686285181122911537441192943073310204209086616936360770367059427862743272542535703406418700365566693954029683680217414854103 +d = 108642162821084938181507878056324903120999504739411128372202198922197750954973 +c = 305357304207903396563769252433798942116307601421155386799392591523875547772911646596463903009990423488430360340024642675941752455429625701977714941340413671092668556558724798890298527900305625979817567613711275466463556061436226589272364057532769439646178423063839292884115912035826709340674104581566501467826782079168130132642114128193813051474106526430253192254354664739229317787919578462780984845602892238745777946945435746719940312122109575086522598667077632 +""" + +try: + c = int(input(">>> c = ")) + n = int(input(">>> n = ")) + d = int(input(">>> d = ")) + + m = pow(c,d,n) + Convert(m) +except ValueError: + slowprint("\n[-] c,n,d Must Be Integar Number") +except AssertionError: + slowprint("\n[-] Wrong Data") +except KeyboardInterrupt: + exit() + diff --git a/Attacks/RSA5.py b/Attacks/RSA5.py new file mode 100644 index 0000000..a874d69 --- /dev/null +++ b/Attacks/RSA5.py @@ -0,0 +1,57 @@ +from banner import * +from utilis import floorSqrt,modinv,Convert +import sympy as sp +banner() + +""" +n = 441702548375597546265397060347208952276967475274905135912477869560482177333510361384285033947480946188145623354029215951664169885735094097025317925522966956898965174945445544364452506966796499944587005657873812585848386501149925011035458189838748181551356757610548391767844207725796544451539421763642181449329080300719368344152034024278647417306053843499995586981318017650558507022155655502745770884654996618443849292140572931614016663705074656028412717677339714961533961375052773639815540324868758969940933964991941461245158471917647200963814004656567184986255121144463936821101981682767655530654441359046234900674147458678928268719664791590208181725402425003322043157763682685830046581059368233541228450498829982145850640255968763746232611610641869191302389245779111356909617313529277007241602005067848947949307502548984897136138368091937447795052112684321369869694696022995282137270503420632588160475186971207198459134307608759756767731733884585073817881348559945137106821730698056732508316693757300672313883162959195685130393016049081156405669205548223148112949446059403857423978820671852027526778494740114169769151540080734651779260084907492653816632846142736609605163394526211986727369288330063666919831047575220019292555559557 +e = 65537 +d = 20929658227875938261058725840264131720240194797254721497929999366502087574029656881231720165783065592372637815940915666212613039099487922843233911353847485450062009142435445746729639472299106954973763080234456847113054478990804619551767588431261008218457316621723802599302359261042715498257923026077002571619206016545993843650901654469988654046781302853813921776892035397733517301594459235978192450212096281249109282450379464222318996342527212038893311335406614944259596938858799625134827471785811994514192796802507658154603626182055262115513322362471136252282318391917574551672482430283925389405151320872665308738881 +c = 223986220247071217870216666779014143338794244806897003439570261828585609461069625566993441707552858342245944642945164681672765137831785039339186019388055221324788275090628463675778644169814393286912058814837219760390476510773975243291075922094945253348123263684444264887680045467214228402806502985409400661033608167210517145287411533259812656955874094033211161421678440562053003042479607653554525213535393053262147643051962890823534380167045346153321162009201462563059751490608169219660707113678434438625268806729271472128212898965284892857326073914569265528121606803508514214189119223383833552072586814770466728871437446190820676075097507547134736007108263775508884871752392084557481102147503606249246354860813514035927154754953172611572565854652996865901035897405554136502508927222416891115275427864126981053248969607448749019664721233739296140343775145313825978714021202368146466869500962807323607852112530742349058090066478686837929805776585570594765868633295031229876778165218553418581369590926767847888849449544777172583513049998378026545824378239267780173494553559807077301106319453742180763436936829925045341906074598727300955137592558540978215844567601254095649299172760605263652690076724946138284654947266095386055238602230 +""" +def premRSA(n,e,d,c): + bitLenN = int(sp.floor(sp.log(n)/sp.log(2)) + 1) + bitLenD0 = 2048 + + assert bitLenD0 >= bitLenN/2 + + d = halfdPartialKeyRecoveryAttack(d,2048,4096,n,e) + return pow(c,d,n) + +def halfdPartialKeyRecoveryAttack(d0,d0BitSize,nBitSize,n="n",e="e"): + test = pow(3, e, n) + test2 = pow(5, e, n) + for k in range(1,e): + d = ((k * n + 1) // e) + d >>= d0BitSize + d <<= d0BitSize + d |= d0 + if((e * d) % k == 1): + if pow(test, d, n) == 3: + if pow(test2, d, n) == 5: + totientN = (e*d - 1) // k + b = totientN - n - 1 + discriminant = b*b - 4*n + root = floorSqrt(discriminant) + if(root*root != discriminant): + continue + p = (-b + root) // 2 + q = n // p + return d + +try: + c = int(input(">>> c = ")) + n = int(input(">>> n = ")) + e = int(input(">>> e = ")) + d = int(input(">>> d = ")) + m = premRSA(n,e,d,c) + Convert(m) + +except ValueError: + slowprint("\n[-] c,n,d,e Must Be Integar Number") +except AssertionError: + slowprint("\n[-] Wrong Data") +except KeyboardInterrupt: + exit() +except: + slowprint("\n[-] False Attack !") diff --git a/Attacks/RSA6.py b/Attacks/RSA6.py new file mode 100644 index 0000000..683d925 --- /dev/null +++ b/Attacks/RSA6.py @@ -0,0 +1,25 @@ +from banner import * +from utilis import egcd,modinv,Convert +from fractions import Fraction +banner() +""" +p = 153143042272527868798412612417204434156935146874282990942386694020462861918068684561281763577034706600608387699148071015194725533394126069826857182428660427818277378724977554365910231524827258160904493774748749088477328204812171935987088715261127321911849092207070653272176072509933245978935455542420691737433 +c = 14699632914289984358210582075909309608817619615764122409514577850253033131275996955127394794512801987443786025277031557775238937388086632327611216460613138074110905840487213116627139558528011448028813522809156509374602431319619052803531008581645459969997969897966475478980398396687104746302415391434104278327526947341073215599683555703818611403510483832532921625745935543818100178607417658929607435582550913918895885596025822532531372429301293588416086854338617700672628239475365045267537032531973594689061842791028000992635092519215619497247452814483357403667400283975070444902253349175045305073603687442947532925780 +e = 65537 +n = 23952937352643527451379227516428377705004894508566304313177880191662177061878993798938496818120987817049538365206671401938265663712351239785237507341311858383628932183083145614696585411921662992078376103990806989257289472590902167457302888198293135333083734504191910953238278860923153746261500759411620299864395158783509535039259714359526738924736952759753503357614939203434092075676169179112452620687731670534906069845965633455748606649062394293289967059348143206600765820021392608270528856238306849191113241355842396325210132358046616312901337987464473799040762271876389031455051640937681745409057246190498795697239 +""" +try: + c = int(input(">>> c = ")) + n = int(input(">>> n = ")) + e = int(input(">>> e = ")) + p = int(input(">>> p = ")) + print(n) + q = n//p + phi = (p-1) * (q-1) + d = modinv(e,phi) + decode = pow(c,d,n) + Convert(decode) +except AssertionError: + slowprint("\n[-] Wrong Data") +except KeyboardInterrupt: + exit() diff --git a/Attacks/RSA7.py b/Attacks/RSA7.py new file mode 100644 index 0000000..5d8d183 --- /dev/null +++ b/Attacks/RSA7.py @@ -0,0 +1,36 @@ +from banner import * +from utilis import egcd,modinv,Convert +import binascii +banner() + +""" +e = 65537 +n = 642313240848064014975043934308658242447312485152342673610756859535090103704610472004913349502648157091104463303511131278665176160214474038294042375555935567033107229886104534241324327133387923226576002115108963521725703773387678635509903034467838260875686083768549775481391190161412646384559222421917626615323 +dp = 17765378008759755288183210466105878526943875374957170036175281330288884608317141953683920408636506981101765935449140323585600732241535721917282237462133813 +c = 147903288008907053469880199469959588903705520519775597541160700501753344741954421604588338524905987922631822425828587114084662512860181022047137469441292833823381362238861070683420786510831001513730638949486694641768638258876688738949817816449109334961820861920165271653627904957302093274915248851406573361863 +""" + +try: + c = int(input(">>> c = ")) + n = int(input(">>> n = ")) + e = int(input(">>> e = ")) + dp = int(input(">>> dp = ")) + mp = (dp * e) - 1 + for i in range(2,1000000): + p = (mp // i) + 1 + if n % p == 0: + break + q = n//p + slowprint("\n[+] Please Wait ... \033[95m\n") + phi = (p-1)*(q-1) + + d = modinv(e,phi) + decode = pow(c,d,n) + Convert(decode) + +except ValueError: + slowprint("\n[-] c,n,e,dp Must Be Integar Number") +except AssertionError: + slowprint("\n[-] Wrong Data") +except KeyboardInterrupt: + exit() diff --git a/Attacks/RSA8.py b/Attacks/RSA8.py new file mode 100644 index 0000000..8e01b87 --- /dev/null +++ b/Attacks/RSA8.py @@ -0,0 +1,26 @@ +from banner import * +from utilis import Convert +banner() + + + +# Example +#c = 74802199268254280440493690700608296874229186682164386879225634595063352871356558051619716745745659385094914838131944342112615526475056433710891149995541243955498341403563518984577892267288643941 + +try: + import gmpy2 + import binascii + c = int(input(">>> c = ")) + m = gmpy2.iroot(c, 3)[0] + assert pow(m,3) == c + Convert(m) +except ImportError: + slowprint("\n[-] Module Not Setup") +except ValueError: + slowprint("\n[-] c Must Be Integar Number") +except AssertionError: + slowprint("\n[-] Wrong Data") +except KeyboardInterrupt: + exit() +except: + slowprint("\n[-] False Attack !") diff --git a/Attacks/RSA_boneh_durfee.py b/Attacks/RSA_boneh_durfee.py new file mode 100644 index 0000000..f40ca93 --- /dev/null +++ b/Attacks/RSA_boneh_durfee.py @@ -0,0 +1,337 @@ + + +from __future__ import print_function +from banner import * +banner() +# This file was *autogenerated* from the file boneh_durfee.sage +from sage.all_cmdline import * # import sage library +from Crypto.Util.number import long_to_bytes +_sage_const_7 = Integer(7); _sage_const_0 = Integer(0); _sage_const_1 = Integer(1); _sage_const_60 = Integer(60); _sage_const_2 = Integer(2); _sage_const_0xc2fd2913bae61f845ac94e4ee1bb10d8531dda830d31bb221dac5f179a8f883f15046d7aa179aff848db2734b8f88cc73d09f35c445c74ee35b01a96eb7b0a6ad9cb9ccd6c02c3f8c55ecabb55501bb2c318a38cac2db69d510e152756054aaed064ac2a454e46d9b3b755b67b46906fbff8dd9aeca6755909333f5f81bf74db = Integer(0xc2fd2913bae61f845ac94e4ee1bb10d8531dda830d31bb221dac5f179a8f883f15046d7aa179aff848db2734b8f88cc73d09f35c445c74ee35b01a96eb7b0a6ad9cb9ccd6c02c3f8c55ecabb55501bb2c318a38cac2db69d510e152756054aaed064ac2a454e46d9b3b755b67b46906fbff8dd9aeca6755909333f5f81bf74db); _sage_const_0x19441f679c9609f2484eb9b2658d7138252b847b2ed8ad182be7976ed57a3e441af14897ce041f3e07916445b88181c22f510150584eee4b0f776a5a487a4472a99f2ddc95efdd2b380ab4480533808b8c92e63ace57fb42bac8315fa487d03bec86d854314bc2ec4f99b192bb98710be151599d60f224114f6b33f47e357517 = Integer(0x19441f679c9609f2484eb9b2658d7138252b847b2ed8ad182be7976ed57a3e441af14897ce041f3e07916445b88181c22f510150584eee4b0f776a5a487a4472a99f2ddc95efdd2b380ab4480533808b8c92e63ace57fb42bac8315fa487d03bec86d854314bc2ec4f99b192bb98710be151599d60f224114f6b33f47e357517); _sage_const_p18 = RealNumber('.18'); _sage_const_4 = Integer(4); _sage_const_0p292 = RealNumber('0.292') + +import time +from sage.all import * +preparser(True) + +############################################ +# Config +########################################## + +""" +Setting debug to true will display more informations +about the lattice, the bounds, the vectors... +""" +debug = True + +""" +Setting strict to true will stop the algorithm (and +return (-1, -1)) if we don't have a correct +upperbound on the determinant. Note that this +doesn't necesseraly mean that no solutions +will be found since the theoretical upperbound is +usualy far away from actual results. That is why +you should probably use `strict = False` +""" +strict = False + +""" +This is experimental, but has provided remarkable results +so far. It tries to reduce the lattice as much as it can +while keeping its efficiency. I see no reason not to use +this option, but if things don't work, you should try +disabling it +""" +helpful_only = True +dimension_min = _sage_const_7 # stop removing if lattice reaches that dimension + +############################################ +# Functions +########################################## + +# display stats on helpful vectors +def helpful_vectors(BB, modulus): + nothelpful = _sage_const_0 + for ii in range(BB.dimensions()[_sage_const_0 ]): + if BB[ii,ii] >= modulus: + nothelpful += _sage_const_1 + + print(nothelpful, "/", BB.dimensions()[_sage_const_0 ], " vectors are not helpful") + +# display matrix picture with 0 and X +def matrix_overview(BB, bound): + for ii in range(BB.dimensions()[_sage_const_0 ]): + a = ('%02d ' % ii) + for jj in range(BB.dimensions()[_sage_const_1 ]): + a += '0' if BB[ii,jj] == _sage_const_0 else 'X' + if BB.dimensions()[_sage_const_0 ] < _sage_const_60 : + a += ' ' + if BB[ii, ii] >= bound: + a += '~' + print(a) + +# tries to remove unhelpful vectors +# we start at current = n-1 (last vector) +def remove_unhelpful(BB, monomials, bound, current): + # end of our recursive function + if current == -_sage_const_1 or BB.dimensions()[_sage_const_0 ] <= dimension_min: + return BB + + # we start by checking from the end + for ii in range(current, -_sage_const_1 , -_sage_const_1 ): + # if it is unhelpful: + if BB[ii, ii] >= bound: + affected_vectors = _sage_const_0 + affected_vector_index = _sage_const_0 + # let's check if it affects other vectors + for jj in range(ii + _sage_const_1 , BB.dimensions()[_sage_const_0 ]): + # if another vector is affected: + # we increase the count + if BB[jj, ii] != _sage_const_0 : + affected_vectors += _sage_const_1 + affected_vector_index = jj + + # level:0 + # if no other vectors end up affected + # we remove it + if affected_vectors == _sage_const_0: + print("* removing unhelpful vector", ii) + BB = BB.delete_columns([ii]) + BB = BB.delete_rows([ii]) + monomials.pop(ii) + BB = remove_unhelpful(BB, monomials, bound, ii-_sage_const_1 ) + return BB + + elif affected_vectors == _sage_const_1: + affected_deeper = all( + BB[kk, affected_vector_index] == _sage_const_0 + for kk in range( + affected_vector_index + _sage_const_1, + BB.dimensions()[_sage_const_0], + ) + ) + # remove both it if no other vector was affected and + # this helpful vector is not helpful enough + # compared to our unhelpful one + if affected_deeper and abs(bound - BB[affected_vector_index, affected_vector_index]) < abs(bound - BB[ii, ii]): + print("* removing unhelpful vectors", ii, "and", affected_vector_index) + BB = BB.delete_columns([affected_vector_index, ii]) + BB = BB.delete_rows([affected_vector_index, ii]) + monomials.pop(affected_vector_index) + monomials.pop(ii) + BB = remove_unhelpful(BB, monomials, bound, ii-_sage_const_1 ) + return BB + # nothing happened + return BB + +""" +Returns: +* 0,0 if it fails +* -1,-1 if `strict=true`, and determinant doesn't bound +* x0,y0 the solutions of `pol` +""" +def boneh_durfee(pol, modulus, mm, tt, XX, YY): + """ + Boneh and Durfee revisited by Herrmann and May + + finds a solution if: + * d < N^delta + * |x| < e^delta + * |y| < e^0.5 + whenever delta < 1 - sqrt(2)/2 ~ 0.292 + """ + + # substitution (Herrman and May) + PR = PolynomialRing(ZZ, names=('u', 'x', 'y',)) + (u, x, y,) = PR._first_ngens(3) + Q = PR.quotient(x*y + _sage_const_1 - u) # u = xy + 1 + polZ = Q(pol).lift() + + UU = XX*YY + _sage_const_1 + + # x-shifts + gg = [] + for kk in range(mm + _sage_const_1 ): + for ii in range(mm - kk + _sage_const_1 ): + xshift = x**ii * modulus**(mm - kk) * polZ(u, x, y)**kk + gg.append(xshift) + gg.sort() + + # x-shifts list of monomials + monomials = [] + for polynomial in gg: + for monomial in polynomial.monomials(): + if monomial not in monomials: + monomials.append(monomial) + monomials.sort() + + # y-shifts (selected by Herrman and May) + for jj in range(_sage_const_1 , tt + _sage_const_1 ): + for kk in range(floor(mm/tt) * jj, mm + _sage_const_1 ): + yshift = y**jj * polZ(u, x, y)**kk * modulus**(mm - kk) + yshift = Q(yshift).lift() + gg.append(yshift) # substitution + + # y-shifts list of monomials + for jj in range(_sage_const_1 , tt + _sage_const_1 ): + monomials.extend( + u**kk * y**jj + for kk in range(floor(mm / tt) * jj, mm + _sage_const_1) + ) + # construct lattice B + nn = len(monomials) + BB = Matrix(ZZ, nn) + for ii in range(nn): + BB[ii, _sage_const_0 ] = gg[ii](_sage_const_0 , _sage_const_0 , _sage_const_0 ) + for jj in range(_sage_const_1 , ii + _sage_const_1 ): + if monomials[jj] in gg[ii].monomials(): + BB[ii, jj] = gg[ii].monomial_coefficient(monomials[jj]) * monomials[jj](UU,XX,YY) + + # Prototype to reduce the lattice + if helpful_only: + # automatically remove + BB = remove_unhelpful(BB, monomials, modulus**mm, nn-_sage_const_1 ) + # reset dimension + nn = BB.dimensions()[_sage_const_0 ] + if nn == _sage_const_0 : + print("failure") + return _sage_const_0 ,_sage_const_0 + + # check if vectors are helpful + if debug: + helpful_vectors(BB, modulus**mm) + + # check if determinant is correctly bounded + det = BB.det() + bound = modulus**(mm*nn) + if det >= bound: + print("We do not have det < bound. Solutions might not be found.") + print("Try with highers m and t.") + if debug: + diff = (log(det) - log(bound)) / log(_sage_const_2 ) + print("size det(L) - size e^(m*n) = ", floor(diff)) + if strict: + return -_sage_const_1 , -_sage_const_1 + else: + print("det(L) < e^(m*n) (good! If a solution exists < N^delta, it will be found)") + + # LLL + if debug: + print("optimizing basis of the lattice via LLL, this can take a long time") + + BB = BB.LLL() + + if debug: + print("LLL is done!") + + # transform vector i & j -> polynomials 1 & 2 + if debug: + print("looking for independent vectors in the lattice") + found_polynomials = False + + for pol1_idx in range(nn - _sage_const_1 ): + for pol2_idx in range(pol1_idx + _sage_const_1 , nn): + # for i and j, create the two polynomials + PR = PolynomialRing(ZZ, names=('w', 'z',)) + (w, z,) = PR._first_ngens(2) + pol1 = pol2 = _sage_const_0 + for jj in range(nn): + pol1 += monomials[jj](w*z+_sage_const_1 ,w,z) * BB[pol1_idx, jj] / monomials[jj](UU,XX,YY) + pol2 += monomials[jj](w*z+_sage_const_1 ,w,z) * BB[pol2_idx, jj] / monomials[jj](UU,XX,YY) + + # resultant + PR = PolynomialRing(ZZ, names=('q',)) + (q,) = PR._first_ngens(1) + rr = pol1.resultant(pol2) + + if rr.is_zero() or rr.monomials() == [_sage_const_1 ]: + continue + print("found them, using vectors", pol1_idx, "and", pol2_idx) + found_polynomials = True + break + if found_polynomials: + break + + if not found_polynomials: + print("no independant vectors could be found. This should very rarely happen...") + return _sage_const_0 , _sage_const_0 + + rr = rr(q, q) + + # solutions + soly = rr.roots() + + if len(soly) == _sage_const_0 : + print("Your prediction (delta) is too small") + return _sage_const_0 , _sage_const_0 + + soly = soly[_sage_const_0 ][_sage_const_0 ] + ss = pol1(q, soly) + solx = ss.roots()[_sage_const_0 ][_sage_const_0 ] + + # + return solx, soly + + + +try: + ############################################ + # How To Use This Script + ########################################## + + # + # The problem to solve (edit the following values) + # + + # the modulus + + N = int(input(">>> n = ")) + e = int(input(">>> e = ")) + c = int(input(">>> c = ")) + + # the hypothesis on the private exponent (the theoretical maximum is 0.292) + delta = _sage_const_p18 # this means that d < N^delta + + # + # Lattice (tweak those values) + # + + # you should tweak this (after a first run), (e.g. increment it until a solution is found) + m = _sage_const_4 # size of the lattice (bigger the better/slower) + + # you need to be a lattice master to tweak these + t = int((_sage_const_1 -_sage_const_2 *delta) * m) # optimization from Herrmann and May + X = _sage_const_2 *floor(N**delta) # this _might_ be too much + Y = floor(N**(_sage_const_1 /_sage_const_2 )) # correct if p, q are ~ same size + + # + # Don't touch anything below + # + + # Problem put in equation + P = PolynomialRing(ZZ, names=('x', 'y',)) + (x, y,) = P._first_ngens(2) + A = int((N+_sage_const_1 )/_sage_const_2 ) + pol = _sage_const_1 + x * (A + y) + + # boneh_durfee + if debug: + print("=== running algorithm ===") + start_time = time.time() + + solx, soly = boneh_durfee(pol, e, m, t, X, Y) + + # found a solution? + if solx > _sage_const_0: + print("=== solution found ===") + d = int(pol(solx, soly) / e) + print( "private key found:", d) + print(long_to_bytes(pow(c,d,N)).decode()) + else: + print("=== no solution was found ===") + +except AssertionError: + slowprint("\n[-] Wrong Data") +except KeyboardInterrupt: + exit() + + + diff --git a/Attacks/RSA_chinese_remainder_theorem.py b/Attacks/RSA_chinese_remainder_theorem.py new file mode 100644 index 0000000..532ecf8 --- /dev/null +++ b/Attacks/RSA_chinese_remainder_theorem.py @@ -0,0 +1,32 @@ +from banner import * +from utilis import modinv,Convert +banner() +#Example +""" +c = 62078086677416686867183857957350338314446280912673392448065026850212685326551183962056495964579782325302082054393933682265772802750887293602432512967994805549965020916953644635965916607925335639027579187435180607475963322465417758959002385451863122106487834784688029167720175128082066670945625067803812970871 +p = 7901324502264899236349230781143813838831920474669364339844939631481665770635584819958931021644265960578585153616742963330195946431321644921572803658406281 +q = 12802918451444044622583757703752066118180068668479378778928741088302355425977192996799623998720429594346778865275391307730988819243843851683079000293815051 +dp = 5540655028622021934429306287937775291955623308965208384582009857376053583575510784169616065113641391169613969813652523507421157045377898542386933198269451 +dq = 9066897320308834206952359399737747311983309062764178906269475847173966073567988170415839954996322314157438770225952491560052871464136163421892050057498651 +""" +try: + c = int(input(">>> c = ")) + p = int(input(">>> p = ")) + q = int(input(">>> q = ")) + dp = int(input(">>> dp = ")) + dq = int(input(">>> dq = ")) + + slowprint("\n[+] Please Wait ... \033[95m\n") + def chinese_remainder_theorem(p,q,dp,dq,chipher_text): + q_inv = modinv(p , q) + m1 = pow(chipher_text,dp,p) + m2 = pow(chipher_text,dq,q) + h = (q_inv*(m1-m2)) % p + return m2 + h * q + Convert(chinese_remainder_theorem(p,q,dp,dq,c)) +except ValueError: + slowprint("\n[-] c,p,q,dp,dq Must Be Integar Number") +except AssertionError: + slowprint("\n[-] Wrong Data") +except KeyboardInterrupt: + exit() diff --git a/Attacks/RSA_common_exponent.py b/Attacks/RSA_common_exponent.py new file mode 100644 index 0000000..10760fc --- /dev/null +++ b/Attacks/RSA_common_exponent.py @@ -0,0 +1,39 @@ +from banner import * +from utilis import egcd,modinv,Convert +banner() +""" +n1 = 101160013260894210379231391020878154246531211501917641525287013838064105641873685273682200363965750860043980554926996771004697813924018366330613894159009241440066954185337675909482468504824750801346658013253206226485930206600036498618437728843373496089729788328065664820868923399881331844439326306628030735331 +n2 = 10210032872259313686164929473710869592253665432474968016181665104415833992599841926829524498430031021642288824945683044264288937821741278527156550423507713336648261819557381748119064650541556448192793186877429032749223390372405186620392499684387407129200331654256366265932387530132500098451224874600895892420947291972547271991706165603868937708772321221206586800972518013238899316242083977031307714803896630612616326248775444788388309984437812243115218989690016673 +c1 = 66392227979404854586458510145632770314092785183583016343730600866783415262123689751268136986554015242755982639115194608534311988971825645169411730748186710947220673034519486951708550346274786041388143262608730637962604985755406789741345581700390500478433427709495522821473858104289236940045533583368435030007 +c2 = 930631363282343082351877158963544600758567980445720982159124066613164772521801283854718425072511342860751113498013426077785323580215340717638119847338418916957048868138332980175665931485108461262077829550248080371766930777925758187526884947593887945397880426339790518082874861594313920319799945895788141894706108889222173060684161230756671422619830375801046671640406258873119163443168478229210037764301299889548125261094599251838443625348832760001827881979679672 +e = 65537 +""" +n1 = int(input(">>> n1 = ")) +n2 = int(input(">>> n2 = ")) +c1 = int(input(">>> c1 = ")) +c2 = int(input(">>> c2 = ")) +e = int(input(">>> e = ")) + + +if egcd(n1,n2)[0] == 1: + print("[-] Sorry , gcd(n1,n2) = 1") + exit() +else: + p = egcd(n1,n2)[0] + q1 = n1 // p + q2 = n2 // p + phi1 = (p-1) * (q1 - 1) + phi2 = (p-1) * (q2 - 1) + d1 = modinv(e,phi1) + d2 = modinv(e,phi2) + try: + m1 = pow(c1,d1,n1) + Convert(m1) + except TypeError: + pass + try: + m2 = pow(c2,d2,n2) + Convert(m2) + except TypeError: + pass + diff --git a/Attacks/RSA_common_modulus.py b/Attacks/RSA_common_modulus.py new file mode 100644 index 0000000..53eae8a --- /dev/null +++ b/Attacks/RSA_common_modulus.py @@ -0,0 +1,44 @@ +from banner import * +from utilis import egcd,Convert +banner() +""" +n = 121785996773018308653850214729611957957750585856946607620398279656647965006857599756926384863459274369411103073349913717154710735727786240206066327436155758154142877120260776520601315370480059127244029804523614658953301573686851312721445206131147094674807765817210890772194336025491364961932882951123597124291 +e2 = 343223 +c2 = 99993713982446651581396992055360571139557381122865583938229634474666415937105325664345678113405954865343401854091338680448775405253508255042453184099961570780032181898606546389573694481401653361757628850127420072609555997892925890632116852740542002226555293049123266123721696951805937683483979653786235824108 +e1 = 65537 +c1 = 5050983197907648139720782448847677677343236446273586870502111273113384857588837608900494692102715861436825279596563904392832518247929761994240007673498974877828278590361242528762459283022987952424770766975922016521475963712698089809426428406068793291250622593222599407825968002220906973019105007856539702124 +""" +try: + import gmpy2 + from Crypto.Util.number import GCD + + def neg_pow(a, b, n): + assert b < 0 + assert GCD(a, n) == 1 + res = int(gmpy2.invert(a, n)) + return pow(res, b*(-1), n) + + def common_modulus(e1, e2, n, c1, c2): + g, a, b = egcd(e1, e2) + c1 = neg_pow(c1, a, n) if a < 0 else pow(c1, a, n) + c2 = neg_pow(c2, b, n) if b < 0 else pow(c2, b, n) + ct = c1*c2 % n + return int(gmpy2.iroot(ct, g)[0]) + + c1 = int(input(">>> c1 = ")) + c2 = int(input(">>> c2 = ")) + e1 = int(input(">>> e1 = ")) + e2 = int(input(">>> e2 = ")) + n = int(input(">>> n = ")) + + Convert(common_modulus(e1, e2, n, c1, c2)) +except ImportError: + slowprint("\n[-] Module Not Setup") +except ValueError: + slowprint("\n[-] e1, e2, n, c1, c2 Must Be Integar Number") +except AssertionError: + slowprint("\n[-] Wrong Data") +except KeyboardInterrupt: + exit() +except: + slowprint("\n[-] False Attack !") diff --git a/Attacks/RSA_hasted.py b/Attacks/RSA_hasted.py new file mode 100644 index 0000000..52c9224 --- /dev/null +++ b/Attacks/RSA_hasted.py @@ -0,0 +1,46 @@ +from banner import * +from utilis import inv_pow,mul_inv,Convert + +import functools +banner() + +""" +N1 = 79608037716527910392060670707842954224114341083822168077002144855358998405023007345791355970838437273653492726857398313047195654933011803740498167538754807659255275632647165202835846338059572102420992692073303341392512490988413552501419357400503232190597741120726276250753866130679586474440949586692852365179 +C1 = 34217065803425349356447652842993191079705593197469002356250751196039765990549766822180265723173964726087016890980051189787233837925650902081362222218365748633591895514369317316450142279676583079298758397507023942377316646300547978234729578678310028626408502085957725408232168284955403531891866121828640919987 +N2 = 58002222048141232855465758799795991260844167004589249261667816662245991955274977287082142794911572989261856156040536668553365838145271642812811609687362700843661481653274617983708937827484947856793885821586285570844274545385852401777678956217807768608457322329935290042362221502367207511491516411517438589637 +C2 = 48038542572368143315928949857213341349144690234757944150458420344577988496364306227393161112939226347074838727793761695978722074486902525121712796142366962172291716190060386128524977245133260307337691820789978610313893799675837391244062170879810270336080741790927340336486568319993335039457684586195656124176 +N3 = 95136786745520478217269528603148282473715660891325372806774750455600642337159386952455144391867750492077191823630711097423473530235172124790951314315271310542765846789908387211336846556241994561268538528319743374290789112373774893547676601690882211706889553455962720218486395519200617695951617114702861810811 +C3 = 55139001168534905791033093049281485849516290567638780139733282880064346293967470884523842813679361232423330290836063248352131025995684341143337417237119663347561882637003640064860966432102780676449991773140407055863369179692136108534952624411669691799286623699981636439331427079183234388844722074263884842748 +""" +N = [] +C = [] + +def chinese_remainder(n, a): + sum = 0 + prod = functools.reduce(lambda a, b: a*b, n) + for n_i, a_i in zip(n, a): + p = prod // n_i + sum += a_i * mul_inv(p, n_i) * p + return sum % prod + +try: + num = int(input(">>> number of n,c pairs = ")) + N = [] + C = [] + for _ in range(num): + n = int(input(">>> n = ")) + c = int(input(">>> c = ")) + N.append(n) + C.append(c) + e = len(N) + a = chinese_remainder(N, C) + for n, c in zip(N, C): + assert a % n == c + m = inv_pow(a, e) + Convert(m) +except ValueError: + slowprint("\n[-] c1,c2,c3,n1,n2,n3 Must Be Integar Number") +except AssertionError: + slowprint("\n[-] Wrong Data") +except KeyboardInterrupt: + exit() diff --git a/Attacks/RSA_multiPrime1.py b/Attacks/RSA_multiPrime1.py new file mode 100644 index 0000000..88cd411 --- /dev/null +++ b/Attacks/RSA_multiPrime1.py @@ -0,0 +1,37 @@ +from banner import * +from utilis import egcd,modinv,Convert +from Factorizations.ecm import * +banner() + +""" +c= 3821925911648555519353747434606743159593808677487039861592438384426669998207423450606829031692403202928227703884307291926528640413305346863805555214851644456742958636273721157021584443312591620736285469414067076228984358550669307967587995994219000349054979046909041864106400708453105658165917613077273501 +n= 6311257310749529896994764164885908074730315623107218148732436180339784730655096846277587690299624960654670853389184027362495475005388709090759335907428646246751073917955576737663877861242491426053238800688758573959939180213510980211538490409598005851282273664989880554327678869807688158210471903939248513 +e= 65537 +prime = [2160890461,2247289019,2250778319,2442210431,2458778093,2534226749,2535292559,2546035901,2651829007,2690421313,2737511971,2807722121,2985359177,3074912623,3142693039,3144852421,3159476069,3166527541,3269492927,3328687379,3493484429,3505945799,3538145749,3610828651,3699668617,3715792519,4036077043,4058968889,4089517513,4116792439,4262477123,4291039453] +""" + +try: + + c = int(input(">>> c = ")) + n = int(input(">>> n = ")) + e = int(input(">>> e = ")) + prime = primefactors(n) + + phi = 1 + for i in prime: + phi *= i-1 + + d = modinv(e, phi) + m = pow(c, d, n) + Convert(m) + +except IndexError: + slowprint("[-] Sorry Can't Factorize n ") +except ImportError: + slowprint("\n[-] Module Not Setup") +except ValueError: + slowprint("\n[-] c,n,e Must Be Integar Number") +except KeyboardInterrupt: + exit() +except: + slowprint("[-] False Attack !") diff --git a/Attacks/RSA_wiener.py b/Attacks/RSA_wiener.py new file mode 100644 index 0000000..c56163f --- /dev/null +++ b/Attacks/RSA_wiener.py @@ -0,0 +1,40 @@ +from banner import * +from utilis import reduites_fraction_continue,fraction_continue,division_euclidienne,Convert +import random +banner() + +""" +#Example : +n = 744818955050534464823866087257532356968231824820271085207879949998948199709147121321290553099733152323288251591199926821010868081248668951049658913424473469563234265317502534369961636698778949885321284313747952124526309774208636874553139856631170172521493735303157992414728027248540362231668996541750186125327789044965306612074232604373780686285181122911537441192943073310204209086616936360770367059427862743272542535703406418700365566693954029683680217414854103 +e = 57595780582988797422250554495450258341283036312290233089677435648298040662780680840440367886540630330262961400339569961467848933132138886193931053170732881768402173651699826215256813839287157821765771634896183026173084615451076310999329120859080878365701402596570941770905755711526708704996817430012923885310126572767854017353205940605301573014555030099067727738540219598443066483590687404131524809345134371422575152698769519371943813733026109708642159828957941 +c = 305357304207903396563769252433798942116307601421155386799392591523875547772911646596463903009990423488430360340024642675941752455429625701977714941340413671092668556558724798890298527900305625979817567613711275466463556061436226589272364057532769439646178423063839292884115912035826709340674104581566501467826782079168130132642114128193813051474106526430253192254354664739229317787919578462780984845602892238745777946945435746719940312122109575086522598667077632 +""" + +try: + n = int(input(">>> n = ")) + e = int(input(">>> e = ")) + + def wiener(n, e): + fc = fraction_continue(e, n) + reduites = reduites_fraction_continue(fc) + message_clair = random.randint(10**1,10**5) + message_chiffre = pow(message_clair, e, n) + l = len(reduites) + i = 0 + while i < l and pow(message_chiffre, reduites[i][1], n) != message_clair: + i += 1 + if i != l: + return (reduites[i][1]) + print("[-] Sorry it's Not Wiener Attack\n") + exit(0) + + d = wiener(n,e) + print("\nd = ",d) + c = int(input(">>> c = ")) + decode = pow(c,d,n) + Convert(decode) + +except ImportError: + slowprint("\n[-] Module Not Setup") +except ValueError: + slowprint("\n[-] c,p,q,e Must Be Integar Number") diff --git a/Factorization.py b/Factorization.py new file mode 100644 index 0000000..e9b2f96 --- /dev/null +++ b/Factorization.py @@ -0,0 +1,53 @@ +from banner import * +banner() + +from Factorizations.factordb import * +from Factorizations.fermat import * +from Factorizations.ecm import * +from Factorizations.qs import * + +print(""" +[1] - Factordb Factorization \033[92m +[2] - Fermat Factorization \033[96m +[3] - ECM Factorization \033[93m +[4] - Quadratic sieve Factorization \033[95m +[0] - Exit \033[92m + """) + +x = int(input(">>> ")) +#Factordb +if x == 1: + + n = int(input("%s\n>>> n = %s"%(B,B))) + print("\n[+] Please Wait ...\n") + def factordb(n): + f = FactorDB(n) + f.connect() + return f.get_factor_list() + + print("%sprimes =%s"%(Y,Y),"%s"%(G),factordb(n)) + +#Fermet +elif x == 2: + n = int(input("%s\n>>> n = %s"%(B,B))) + print("\n[+] Please Wait ...\n") + fermat(n) + +#ECM +elif x == 3: + n = int(input("%s\n>>> n = %s"%(B,B))) + print("\n[+] Please Wait ...\n") + print("%sprimes =%s"%(Y,Y),"%s"%(G),primefactors(n)) + +elif x == 4: + N = int(input("%s\n>>> n = %s"%(B,B))) + if not factorise(N): + print("Number Must Be Greater Than 0 and Must Be Integer") + elif factorise(N) == "Long": + print("N is Too Long ") + else: + print("\n[+] Please Wait ...\n") + print ("%sprimes =%s"%(Y,Y),"%s"%(G),factorise(N)) + +else: + exit() diff --git a/Factorizations/ecm.py b/Factorizations/ecm.py new file mode 100644 index 0000000..f0b308b --- /dev/null +++ b/Factorizations/ecm.py @@ -0,0 +1,135 @@ +import random + +def primesbelow(N): + # http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188 + # Input N>=6, Returns a list of primes, 2 <= p < N + correction = N % 6 > 1 + N = {0:N, 1:N-1, 2:N+4, 3:N+3, 4:N+2, 5:N+1}[N%6] + sieve = [True] * (N // 3) + sieve[0] = False + for i in range(int(N ** .5) // 3 + 1): + if sieve[i]: + k = (3 * i + 1) | 1 + sieve[k*k // 3::2*k] = [False] * ((N//6 - (k*k)//6 - 1)//k + 1) + sieve[(k*k + 4*k - 2*k*(i%2)) // 3::2*k] = [False] * ((N // 6 - (k*k + 4*k - 2*k*(i%2))//6 - 1) // k + 1) + return [2, 3] + [(3 * i + 1) | 1 for i in range(1, N//3 - correction) if sieve[i]] + +smallprimeset = set(primesbelow(100000)) +_smallprimeset = 100000 +def isprime(n, precision=7): + # http://en.wikipedia.org/wiki/Miller-Rabin_primality_test#Algorithm_and_running_time + if n < 1: + raise ValueError("Out of bounds, first argument must be > 0") + elif n <= 3: + return n >= 2 + elif n % 2 == 0: + return False + elif n < _smallprimeset: + return n in smallprimeset + + + d = n - 1 + s = 0 + while d % 2 == 0: + d //= 2 + s += 1 + + for _ in range(precision): + a = random.randrange(2, n - 2) + x = pow(a, d, n) + + if x in [1, n - 1]: continue + + for r in range(s - 1): + x = pow(x, 2, n) + if x == 1: return False + if x == n - 1: break + else: return False + + return True + +# https://comeoncodeon.wordpress.com/2010/09/18/pollard-rho-brent-integer-factorization/ +def pollard_brent(n): + if n % 2 == 0: return 2 + if n % 3 == 0: return 3 + + y, c, m = random.randint(1, n-1), random.randint(1, n-1), random.randint(1, n-1) + g, r, q = 1, 1, 1 + while g == 1: + x = y + for _ in range(r): + y = (pow(y, 2, n) + c) % n + + k = 0 + while k < r and g==1: + ys = y + for _ in range(min(m, r-k)): + y = (pow(y, 2, n) + c) % n + q = q * abs(x-y) % n + g = gcd(q, n) + k += m + r *= 2 + if g == n: + while True: + ys = (pow(ys, 2, n) + c) % n + g = gcd(abs(x - ys), n) + if g > 1: + break + + return g + +smallprimes = primesbelow(1000) # might seem low, but 1000*1000 = 1000000, so this will fully factor every composite < 1000000 +def primefactors(n, sort=False): + factors = [] + + for checker in smallprimes: + while n % checker == 0: + factors.append(checker) + n //= checker + if checker > n: break + + if n < 2: return factors + + while n > 1: + if isprime(n): + factors.append(n) + break + factor = pollard_brent(n) # trial division did not fully factor, switch to pollard-brent + factors.extend(primefactors(factor)) # recurse to factor the not necessarily prime factor returned by pollard-brent + n //= factor + + if sort: factors.sort() + + return factors + +def factorization(n): + factors = {} + for p1 in primefactors(n): + try: + factors[p1] += 1 + except KeyError: + factors[p1] = 1 + return factors + +totients = {} +def totient(n): + if n == 0: return 1 + + try: return totients[n] + except KeyError: pass + + tot = 1 + for p, exp in factorization(n).items(): + tot *= (p - 1) * p ** (exp - 1) + + totients[n] = tot + return tot + +def gcd(a, b): + if a == b: return a + while b > 0: a, b = b, a % b + return a + +def lcm(a, b): + return abs((a // gcd(a, b)) * b) + diff --git a/Factorizations/factordb.py b/Factorizations/factordb.py new file mode 100644 index 0000000..9ca572f --- /dev/null +++ b/Factorizations/factordb.py @@ -0,0 +1,39 @@ +# -*- coding: utf-8 -*- +from __future__ import print_function, unicode_literals +import requests + + +ENDPOINT = "http://factordb.com/api" + + +class FactorDB(): + def __init__(self, n): + self.n = n + self.result = None + + def connect(self, reconnect=False): + if self.result and not reconnect: + return self.result + self.result = requests.get(ENDPOINT, params={"query": str(self.n)}) + return self.result + + def get_id(self): + return self.result.json().get("id") if self.result else None + + def get_status(self): + return self.result.json().get("status") if self.result else None + + def get_factor_from_api(self): + return self.result.json().get("factors") if self.result else None + + def get_factor_list(self): + """ + get_factors: [['2', 3], ['3', 2]] + Returns: [2, 2, 2, 3, 3] + """ + factors = self.get_factor_from_api() + if not factors: + return [] + ml = [[int(x)] * y for x, y in factors] + return [y for x in ml for y in x] + diff --git a/Factorizations/fermat.py b/Factorizations/fermat.py new file mode 100644 index 0000000..79f3db2 --- /dev/null +++ b/Factorizations/fermat.py @@ -0,0 +1,19 @@ +from gmpy2 import isqrt +from banner import * +def fermat(n): + a = isqrt(n) + b2 = a*a - n + b = isqrt(n) + while b*b != b2: + a = a + 1 + b2 = a*a - n + b = isqrt(b2) + if b > n : + print("\n%s[-] Sorry Can't Factorize Number%s"%(R,R)) + exit() + p = a+b + q = a-b + print(f"{Y}p ={G}", p) + print(f"{Y}q ={G}", q) + + diff --git a/Factorizations/qs.py b/Factorizations/qs.py new file mode 100644 index 0000000..e2b8769 --- /dev/null +++ b/Factorizations/qs.py @@ -0,0 +1,778 @@ +from banner import * +banner() + + +from math import log,sqrt, ceil, floor +import random +from fractions import gcd +import sys +from builtins import ValueError + +MAX_DIGITS_POLLARD = 30 +POLLARD_QUICK_ITERATIONS = 20 +MIN_DIGITS_POLLARD_QUICK2 = 45 +POLLARD_QUICK2_ITERATIONS = 25 +SIQS_TRIAL_DIVISION_EPS = 25 +SIQS_MIN_PRIME_POLYNOMIAL = 400 +SIQS_MAX_PRIME_POLYNOMIAL = 4000 + +MILLER_RABIN_ITERATIONS = 50 + + +class Polynomial: + + def __init__(self, coeff=[], a=None, b=None): + self.coeff = coeff + self.a = a + self.b = b + + def eval(self, x): + res = 0 + for a in self.coeff[::-1]: + res *= x + res += a + return res + + +class FactorBasePrime: + + def __init__(self, p, tmem, lp): + self.p = p + self.soln1 = None + self.soln2 = None + self.tmem = tmem + self.lp = lp + self.ainv = None + + +def lowest_set_bit(a): + b = (a & -a) + low_bit = -1 + while (b): + b >>= 1 + low_bit += 1 + return low_bit + + +def to_bits(k): + + k_binary = bin(k)[2:] + return (bit == '1' for bit in k_binary[::-1]) + + +def pow_mod(a, k, m): + + r = 1 + b = a + for bit in to_bits(k): + if bit: + r = (r * b) % m + b = (b * b) % m + return r + + +def is_quadratic_residue(a, p): + return legendre(a, (p - 1) // 2, 1, p) == 1 + + +def legendre(a, q, l, n): + x = q ** l + if x == 0: + return 1 + + z = 1 + a %= n + + while x != 0: + if x % 2 == 0: + a = (a ** 2) % n + x //= 2 + else: + x -= 1 + z = (z * a) % n + return z + + +def sqrt_mod_prime(a, p): + # Algorithm from http://www.mersennewiki.org/index.php/Modular_Square_Root + assert a < p + assert is_probable_prime(p) + if a == 0: + return 0 + if p == 2: + return a + if p % 2 == 0: + return None + p_mod_8 = p % 8 + if p_mod_8 == 1: + # Shanks method + q = p // 8 + e = 3 + while q % 2 == 0: + q //= 2 + e += 1 + while True: + x = random.randint(2, p - 1) + z = pow_mod(x, q, p) + if pow_mod(z, 2 ** (e - 1), p) != 1: + break + y = z + r = e + x = pow_mod(a, (q - 1) // 2, p) + v = (a * x) % p + w = (v * x) % p + while True: + if w == 1: + return v + k = 1 + while pow_mod(w, 2 ** k, p) != 1: + k += 1 + d = pow_mod(y, 2 ** (r - k - 1), p) + y = (d ** 2) % p + r = k + v = (d * v) % p + w = (w * y) % p + elif p_mod_8 == 5: + v = pow_mod(2 * a, (p - 5) // 8, p) + i = (2 * a * v * v) % p + return (a * v * (i - 1)) % p + else: + return pow_mod(a, (p + 1) // 4, p) + + +def inv_mod(a, m): + return eea(a, m)[0] % m + + +def eea(a, b): + + if a == 0: + return (0, 1, b) + x = eea(b % a, a) + return (x[1] - b // a * x[0], x[0], x[2]) + + +def is_probable_prime(a): + + if a == 2: + return True + if a == 1 or a % 2 == 0: + return False + return primality_test_miller_rabin(a, MILLER_RABIN_ITERATIONS) + + +def primality_test_miller_rabin(a, iterations): + m = a - 1 + lb = lowest_set_bit(m) + m >>= lb + + for _ in range(iterations): + b = random.randint(2, a - 1) + j = 0 + z = pow_mod(b, m, a) + while (j != 0 or z != 1) and z != a - 1: + if (j > 0 and z == 1 or j + 1 == lb): + return False + j += 1 + z = (z * z) % a + + return True + + +def siqs_factor_base_primes(n, nf): + + global small_primes + factor_base = [] + for p in small_primes: + if is_quadratic_residue(n, p): + t = sqrt_mod_prime(n % p, p) + lp = round(log2(p)) + factor_base.append(FactorBasePrime(p, t, lp)) + if len(factor_base) >= nf: + break + return factor_base + + +def siqs_find_first_poly(n, m, factor_base): + + p_min_i = None + p_max_i = None + for i, fb in enumerate(factor_base): + if p_min_i is None and fb.p >= SIQS_MIN_PRIME_POLYNOMIAL: + p_min_i = i + if p_max_i is None and fb.p > SIQS_MAX_PRIME_POLYNOMIAL: + p_max_i = i - 1 + break + + # The following may happen if the factor base is small, make sure + # that we have enough primes. + if p_max_i is None: + p_max_i = len(factor_base) - 1 + if p_min_i is None or p_max_i - p_min_i < 20: + p_min_i = min(p_min_i, 5) + + target = sqrt(2 * float(n)) / m + target1 = target / ((factor_base[p_min_i].p + + factor_base[p_max_i].p) / 2) ** 0.5 + + # find q such that the product of factor_base[q_i] is approximately + # sqrt(2 * n) / m; try a few different sets to find a good one + best_q, best_a, best_ratio = None, None, None + for _ in range(30): + a = 1 + q = [] + + while a < target1: + p_i = 0 + while p_i == 0 or p_i in q: + p_i = random.randint(p_min_i, p_max_i) + p = factor_base[p_i].p + a *= p + q.append(p_i) + + ratio = a / target + + # ratio too small seems to be not good + if (best_ratio is None or (ratio >= 0.9 and ratio < best_ratio) or + best_ratio < 0.9 and ratio > best_ratio): + best_q = q + best_a = a + best_ratio = ratio + a = best_a + q = best_q + + s = len(q) + B = [] + for l in range(s): + fb_l = factor_base[q[l]] + q_l = fb_l.p + assert a % q_l == 0 + gamma = (fb_l.tmem * inv_mod(a // q_l, q_l)) % q_l + if gamma > q_l // 2: + gamma = q_l - gamma + B.append(a // q_l * gamma) + + b = sum(B) % a + b_orig = b + if (2 * b > a): + b = a - b + + assert 0 < b + assert 2 * b <= a + assert ((b * b - n) % a == 0) + + g = Polynomial([b * b - n, 2 * a * b, a * a], a, b_orig) + h = Polynomial([b, a]) + for fb in factor_base: + if a % fb.p != 0: + fb.ainv = inv_mod(a, fb.p) + fb.soln1 = (fb.ainv * (fb.tmem - b)) % fb.p + fb.soln2 = (fb.ainv * (-fb.tmem - b)) % fb.p + + return g, h, B + + +def siqs_find_next_poly(n, factor_base, i, g, B): + + v = lowest_set_bit(i) + 1 + z = -1 if ceil(i / (2 ** v)) % 2 == 1 else 1 + b = (g.b + 2 * z * B[v - 1]) % g.a + a = g.a + b_orig = b + if (2 * b > a): + b = a - b + assert ((b * b - n) % a == 0) + + g = Polynomial([b * b - n, 2 * a * b, a * a], a, b_orig) + h = Polynomial([b, a]) + for fb in factor_base: + if a % fb.p != 0: + fb.soln1 = (fb.ainv * (fb.tmem - b)) % fb.p + fb.soln2 = (fb.ainv * (-fb.tmem - b)) % fb.p + + return g, h + + +def siqs_sieve(factor_base, m): + sieve_array = [0] * (2 * m + 1) + for fb in factor_base: + if fb.soln1 is None: + continue + p = fb.p + i_start_1 = -((m + fb.soln1) // p) + a_start_1 = fb.soln1 + i_start_1 * p + lp = fb.lp + if p > 20: + for a in range(a_start_1 + m, 2 * m + 1, p): + sieve_array[a] += lp + + i_start_2 = -((m + fb.soln2) // p) + a_start_2 = fb.soln2 + i_start_2 * p + for a in range(a_start_2 + m, 2 * m + 1, p): + sieve_array[a] += lp + return sieve_array + + +def siqs_trial_divide(a, factor_base): + + divisors_idx = [] + for i, fb in enumerate(factor_base): + if a % fb.p == 0: + exp = 0 + while a % fb.p == 0: + a //= fb.p + exp += 1 + divisors_idx.append((i, exp)) + if a == 1: + return divisors_idx + return None + + +def siqs_trial_division(n, sieve_array, factor_base, smooth_relations, g, h, m, + req_relations): + + sqrt_n = sqrt(float(n)) + limit = log2(m * sqrt_n) - SIQS_TRIAL_DIVISION_EPS + for (i, sa) in enumerate(sieve_array): + if sa >= limit: + x = i - m + gx = g.eval(x) + divisors_idx = siqs_trial_divide(gx, factor_base) + if divisors_idx is not None: + u = h.eval(x) + v = gx + assert (u * u) % n == v % n + smooth_relations.append((u, v, divisors_idx)) + if (len(smooth_relations) >= req_relations): + return True + return False + + +def siqs_build_matrix(factor_base, smooth_relations): + fb = len(factor_base) + M = [] + for sr in smooth_relations: + mi = [0] * fb + for j, exp in sr[2]: + mi[j] = exp % 2 + M.append(mi) + return M + + +def siqs_build_matrix_opt(M): + + m = len(M[0]) + cols_binary = [""] * m + for mi in M: + for j, mij in enumerate(mi): + cols_binary[j] += "1" if mij else "0" + return [int(cols_bin[::-1], 2) for cols_bin in cols_binary], len(M), m + + +def add_column_opt(M_opt, tgt, src): + + M_opt[tgt] ^= M_opt[src] + + +def find_pivot_column_opt(M_opt, j): + + return None if M_opt[j] == 0 else lowest_set_bit(M_opt[j]) + + +def siqs_solve_matrix_opt(M_opt, n, m): + + row_is_marked = [False] * n + pivots = [-1] * m + for j in range(m): + i = find_pivot_column_opt(M_opt, j) + if i is not None: + pivots[j] = i + row_is_marked[i] = True + for k in range(m): + if k != j and (M_opt[k] >> i) & 1: # test M[i][k] == 1 + add_column_opt(M_opt, k, j) + perf_squares = [] + for i in range(n): + if not row_is_marked[i]: + perfect_sq_indices = [i] + for j in range(m): + if (M_opt[j] >> i) & 1: # test M[i][j] == 1 + perfect_sq_indices.append(pivots[j]) + perf_squares.append(perfect_sq_indices) + return perf_squares + + +def siqs_calc_sqrts(square_indices, smooth_relations): + + res = [1, 1] + for idx in square_indices: + res[0] *= smooth_relations[idx][0] + res[1] *= smooth_relations[idx][1] + res[1] = sqrt_int(res[1]) + return res + + +def sqrt_int(n): + + a = n + s = 0 + o = 1 << (int(log(n,2)) & ~1) + while o != 0: + t = s + o + if a >= t: + a -= t + s = (s >> 1) + o + else: + s >>= 1 + o >>= 2 + assert s * s == n + return s + + +def kth_root_int(n, k): + + u = n + s = n + 1 + while u < s: + s = u + t = (k - 1) * s + n // pow(s, k - 1) + u = t // k + return s + + +def siqs_factor_from_square(n, square_indices, smooth_relations): + sqrt1, sqrt2 = siqs_calc_sqrts(square_indices, smooth_relations) + assert (sqrt1 * sqrt1) % n == (sqrt2 * sqrt2) % n + return gcd(abs(sqrt1 - sqrt2), n) + + +def siqs_find_factors(n, perfect_squares, smooth_relations): + + factors = [] + rem = n + non_prime_factors = set() + prime_factors = set() + for square_indices in perfect_squares: + fact = siqs_factor_from_square(n, square_indices, smooth_relations) + if fact not in [1, rem]: + if is_probable_prime(fact): + if fact not in prime_factors: + prime_factors.add(fact) + + while rem % fact == 0: + factors.append(fact) + rem //= fact + + if rem == 1: + break + if is_probable_prime(rem): + factors.append(rem) + rem = 1 + break + elif fact not in non_prime_factors: + non_prime_factors.add(fact) + + if rem != 1 and non_prime_factors: + non_prime_factors.add(rem) + for fact in sorted(siqs_find_more_factors_gcd(non_prime_factors)): + while fact != 1 and rem % fact == 0: + factors.append(fact) + rem //= fact + if rem == 1 or is_probable_prime(rem): + break + + if rem != 1: + factors.append(rem) + return factors + + +def siqs_find_more_factors_gcd(numbers): + res = set() + for n in numbers: + res.add(n) + for m in numbers: + if n != m: + fact = gcd(n, m) + if fact not in [1, n, m]: + if fact not in res: + res.add(fact) + res.add(n // fact) + res.add(m // fact) + return res + + +def siqs_choose_nf_m(d): + # Using similar parameters as msieve-1.52 + if d <= 34: + return 200, 65536 + if d <= 36: + return 300, 65536 + if d <= 38: + return 400, 65536 + if d <= 40: + return 500, 65536 + if d <= 42: + return 600, 65536 + if d <= 44: + return 700, 65536 + if d <= 48: + return 1000, 65536 + if d <= 52: + return 1200, 65536 + if d <= 56: + return 2000, 65536 * 3 + if d <= 60: + return 4000, 65536 * 3 + if d <= 66: + return 6000, 65536 * 3 + if d <= 74: + return 10000, 65536 * 3 + if d <= 80: + return 30000, 65536 * 3 + if d <= 88: + return 50000, 65536 * 3 + return (60000, 65536 * 9) if d <= 94 else (100000, 65536 * 9) + + +def siqs_factorise(n): + + dig = len(str(n)) + nf, m = siqs_choose_nf_m(dig) + + factor_base = siqs_factor_base_primes(n, nf) + + required_relations_ratio = 1.05 + success = False + smooth_relations = [] + prev_cnt = 0 + i_poly = 0 + while not success: + required_relations = round(len(factor_base) * required_relations_ratio) + enough_relations = False + while not enough_relations: + if i_poly == 0: + g, h, B = siqs_find_first_poly(n, m, factor_base) + else: + g, h = siqs_find_next_poly(n, factor_base, i_poly, g, B) + i_poly += 1 + if i_poly >= 2 ** (len(B) - 1): + i_poly = 0 + sieve_array = siqs_sieve(factor_base, m) + enough_relations = siqs_trial_division( + n, sieve_array, factor_base, smooth_relations, + g, h, m, required_relations) + + if (len(smooth_relations) >= required_relations or + i_poly % 8 == 0 and len(smooth_relations) > prev_cnt): + + prev_cnt = len(smooth_relations) + + + M = siqs_build_matrix(factor_base, smooth_relations) + M_opt, M_n, M_m = siqs_build_matrix_opt(M) + + + perfect_squares = siqs_solve_matrix_opt(M_opt, M_n, M_m) + + + factors = siqs_find_factors(n, perfect_squares, smooth_relations) + if len(factors) > 1: + success = True + else: + required_relations_ratio += 0.05 + + return factors + + +def check_factor(n, i, factors): + while n % i == 0: + n //= i + factors.append(i) + if is_probable_prime(n): + factors.append(n) + n = 1 + return n + + +def trial_div_init_primes(n, upper_bound): + global small_primes + is_prime = [True] * (upper_bound + 1) + is_prime[:2] = [False] * 2 + factors = [] + small_primes = [] + max_i = sqrt_int(upper_bound) + rem = n + for i in range(2, max_i + 1): + if is_prime[i]: + small_primes.append(i) + rem = check_factor(rem, i, factors) + if rem == 1: + return factors, 1 + + for j in (range(i ** 2, upper_bound + 1, i)): + is_prime[j] = False + + for i in range(max_i + 1, upper_bound + 1): + if is_prime[i]: + small_primes.append(i) + rem = check_factor(rem, i, factors) + if rem == 1: + return factors, 1 + return factors, rem + + +def pollard_brent_f(c, n, x): + x1 = (x * x) % n + c + if x1 >= n: + x1 -= n + assert x1 >= 0 and x1 < n + return x1 + + +def pollard_brent_find_factor(n, max_iter=None): + + y, c, m = (random.randint(1, n - 1) for _ in range(3)) + r, q, g = 1, 1, 1 + i = 0 + while g == 1: + x = y + for _ in range(r): + y = pollard_brent_f(c, n, y) + k = 0 + while k < r and g == 1: + ys = y + for _ in range(min(m, r - k)): + y = pollard_brent_f(c, n, y) + q = (q * abs(x - y)) % n + g = gcd(q, n) + k += m + r *= 2 + if max_iter: + i += 1 + if (i == max_iter): + return None + + if g == n: + while True: + ys = pollard_brent_f(c, n, ys) + g = gcd(abs(x - ys), n) + if g > 1: + break + return g + + +def pollard_brent_quick(n, factors): + + rem = n + while True: + if is_probable_prime(rem): + factors.append(rem) + rem = 1 + break + + digits = len(str(n)) + if digits < MIN_DIGITS_POLLARD_QUICK2: + max_iter = POLLARD_QUICK_ITERATIONS + else: + max_iter = POLLARD_QUICK2_ITERATIONS + + f = pollard_brent_find_factor(rem, max_iter) + if f and f < rem: + if is_probable_prime(f): + factors.append(f) + assert rem % f == 0 + rem //= f + else: + rem_f = pollard_brent_quick(f, factors) + rem = (rem // f) * rem_f + else: + break + return rem + + +def check_perfect_power(n): + largest_checked_prime = small_primes[-1] + for b in small_primes: + bth_root = kth_root_int(n, b) + if bth_root < largest_checked_prime: + break + if (bth_root ** b == n): + return (bth_root, b) + return None + + +def find_prime_factors(n): + if perfect_power := check_perfect_power(n): + factors = [perfect_power[0]] + else: + digits = len(str(n)) + if digits <= MAX_DIGITS_POLLARD: + factors = [pollard_brent_find_factor(n)] + else: + factors = siqs_factorise(n) + + prime_factors = [] + for f in set(factors): + prime_factors.extend(iter(find_all_prime_factors(f))) + return prime_factors + + +def find_all_prime_factors(n): + rem = n + factors = [] + + while rem > 1: + if is_probable_prime(rem): + factors.append(rem) + break + + for f in find_prime_factors(rem): + assert is_probable_prime(f) + assert rem % f == 0 + while rem % f == 0: + rem //= f + factors.append(f) + + return factors + + +def product(factors): + prod = 1 + for f in factors: + prod *= f + return prod + + +def factorise(n): + + if type(n) is long: + return "Long" + if type(n) != int or n < 1: + return False + + if n == 1: + return [] + + if is_probable_prime(n): + return [n] + + factors, rem = trial_div_init_primes(n, 1000000) + + if not factors: + if rem != 1: + digits = len(str(rem)) + if digits > MAX_DIGITS_POLLARD: + rem = pollard_brent_quick(rem, factors) + if rem > 1: + for fr in find_all_prime_factors(rem): + factors.append(fr) + + factors.sort() + assert product(factors) == n + for p in factors: + assert is_probable_prime(p) + return factors + + + + diff --git a/README.md b/README.md index acf781e..3b0ce5b 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,31 @@ -# X-RSA For RSA Attacks : -- X-RSA V0.2 contains a many of attack types such as Hasted, Fermat, Common Modulus, Chinese Remainder Theorem, Wiener ... etc , and it's still under development and adding other Attack. +# X-RSA -- New Attack Added To X-RSA V0.2 And Fixing Many Error in V0.1 +**What's X-RSA ?** +- it's a Tool Which contains a many of attack types in RSA such as Hasted, Common Modulus, Chinese Remainder Theorem, Wiener ... etc , and it's still under development and adding other Attacks. -- X-RSA helps you in [CTF, Hacking , Penetration Testing , Decryption] +**Why X-RSA ?** +- X-RSA help you in [CTF , Penetration Testing , Decryption] -- Written By [Python] +- Written By [ Python 3 ] -# Installing and Open Tool : -1 - install tool : + +# Installing and Running Tool +1 - Install Tool ``` git clone https://github.com/X-Vector/X-RSA.git ``` 2 - Download Requirement ``` apt install libgmp-dev libmpfr-dev libmpc-dev -pip install -r requirement.txt +pip3 install -r requirement.txt ``` -3 - Open Tool : +3 - Run Tool ``` -python Attack.py +python3 Attack.py ``` -# Screenshots : - -![X-RSA](https://e.top4top.net/p_1073esqyv1.png "X-RSA in Action") - Coded By X-Vector - [Facebook](https://www.facebook.com/X.Vector1) - [Linkedin](https://www.linkedin.com/in/x-vector/) - [Twitter](https://twitter.com/@XVector11) + +# Reference +- [Quadratic sieve](https://github.com/skollmann/PyFactorise) diff --git a/RSA.py b/RSA.py deleted file mode 100644 index efcec37..0000000 --- a/RSA.py +++ /dev/null @@ -1,138 +0,0 @@ -#!/usr/bin/env python2 -#-*- coding: utf-8 -*- -# -# author : X-Vector -# Tested on Kali Linux / Parrot Os / Ubuntu -# Simple script for RSA Attack -import sys -import platform,os -from urllib2 import * -from platform import system -def slowprint(s): - for c in s : - sys.stdout.write(c) - sys.stdout.flush() - time.sleep(4. / 100) - -clear = "" -if "Windows" in platform.system(): - clear = "cls" -if "Linux" in platform.system(): - clear = "clear" -os.system(clear) -is_windows = sys.platform.startswith('win') - -# Console Colors -if is_windows: - # Windows deserves coloring too :D - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - try: - import win_unicode_console , colorama - win_unicode_console.enable() - colorama.init() - #Now the unicode will work ^_^ - except: - print("[!] Error: Coloring libraries not installed, no coloring will be used") - G = Y = B = R = W = G = Y = B = R = W = '' - - -else: - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - - -def banner(): - print("""%s - -_____ ________________ _____ _____ -\ \ / /\ \ _____\ \ / |_ - \ | | / \ /\ \ / / \ | / \\ - \ \ / / | \_\ | | | /___/|| /\ \\ - \ | / | ___/ ____\ \ | ||| | | \\ - / | \ | \ ____ / /\ \|___|/| \/ \\ - / /|\ \ / /\ \/ \| |/ \ \ |\ /\ \\ - |____|/ \|____| /_____/ |\______||\____\ /____/| | \_____\ \_____\\ - | | | | | | | | || | || | | | | | | | - |____| |____| |_____|/ \|_____| \|___||____|/ \|_____|\|_____| - %s%s -[ Version : 0.2 ]\033[92m -[ Author : X-Vector ]\033[96m -[ Github : github.com/X-Vector ]\033[93m -[ Twitter : twitter.com/@XVector11 ]\033[95m -[ Facebook: facebook.com/X.Vector1 ]\033[95m -[ GreeteZ : Karem Ali ]\033[94m - - """ % (R, W,R)) -banner() - -import binascii -import gmpy2 - - - -# Example 1 : -#c = 194048013822218245260658018019940874060627700835842604475987702337533801266490182061968998210807564778328557627772974110046885380635225974269865976518335375789734689098164529086561756412074742698644530189076800227300946408167039318949544794351233987752575608106800908043533012088081995031010618521695843625062 -#n = 248501410365662412791489552646042256782092770118253438700194718631291036762726489658495565276550205113648626040596191969135846656414394584577305526761671104277390765264806022908497647300596494542202565022133435383403344333672279722534625284520459706609569974491538689429548817677759350947931780871046796607829 -#e = 65537 - -# Example 2 : -#n = 95927091022214131235859313618337025685657931350656500630264132698389852599510198358308813869652616688807245375001990146888881030682552800587836982145527192151998189057703804830882536718184311723290034393819853924918814439601355616561237190273205228499357454477829605938284765657202538703493014172555332956153 -#e = 3 -#c = 74802199268254280440493690700608296874229186682164386879225634595063352871356558051619716745745659385094914838131944342112615526475056433710891149995541243955498341403563518984577892267288643941 - - -c = input(">>> c = ") -n = input(">>> n = ") -e = input(">>> e = ") - - -gmpy2.get_context().precision=200 -m = gmpy2.iroot(c, e)[0] -try: - if pow(m,e) == c: - slowprint("\n[+] Please Wait ... \033[95m\n") - def hex_pair(x): - return ('0' * (len(x) % 2)) + x - m_hex = '{:x}'.format(m) - m_hex = hex_pair(m_hex) - msg = binascii.unhexlify(m_hex) - slowprint("[+] The PlainText = ") - print(msg.decode()) - else: - slowprint("\n[+] Please Wait ... \033[95m\n") - factordb = RSA.attacks.factordb(n) - q = factordb[0] - p = factordb[1] - phi = (p-1)*(q-1) - def egcd(a,b): - if a == 0 : - return(b,0,1) - else: - g,y,x = egcd(b%a,a) - return (g,x-(b/a)*y,y) - - def modinv(a,m): - g,x,y = egcd(a,m) - if g != 1: - raise Expection("RSA Hello") - else : - return x%m - - d = modinv(e,phi) - decode = pow(c,d,n) - output = (hex(decode)[2:].replace('L','')).decode("hex") - slowprint("[+] The PlainText = ") - print(output) - -except IndexError: - slowprint("[-] Sorry Can't Factorize n :( ") - slowprint("\n[!] Try To Use MultiPrime Attack ") -except: - slowprint("[-] False Attack !! ") diff --git a/RSA3.py b/RSA3.py deleted file mode 100644 index b4abc50..0000000 --- a/RSA3.py +++ /dev/null @@ -1,90 +0,0 @@ -import sys -import platform,os -from urllib2 import * -from platform import system -def slowprint(s): - for c in s : - sys.stdout.write(c) - sys.stdout.flush() - time.sleep(4. / 100) - -clear = "" -if "Windows" in platform.system(): - clear = "cls" -if "Linux" in platform.system(): - clear = "clear" -os.system(clear) -is_windows = sys.platform.startswith('win') - -# Console Colors -if is_windows: - # Windows deserves coloring too :D - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - try: - import win_unicode_console , colorama - win_unicode_console.enable() - colorama.init() - #Now the unicode will work ^_^ - except: - print("[!] Error: Coloring libraries not installed, no coloring will be used") - G = Y = B = R = W = G = Y = B = R = W = '' - - -else: - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - - -def banner(): - print("""%s - -_____ ________________ _____ _____ -\ \ / /\ \ _____\ \ / |_ - \ | | / \ /\ \ / / \ | / \\ - \ \ / / | \_\ | | | /___/|| /\ \\ - \ | / | ___/ ____\ \ | ||| | | \\ - / | \ | \ ____ / /\ \|___|/| \/ \\ - / /|\ \ / /\ \/ \| |/ \ \ |\ /\ \\ - |____|/ \|____| /_____/ |\______||\____\ /____/| | \_____\ \_____\\ - | | | | | | | | || | || | | | | | | | - |____| |____| |_____|/ \|_____| \|___||____|/ \|_____|\|_____| - %s%s -[ Version : 0.2 ]\033[92m -[ Author : X-Vector ]\033[96m -[ Github : github.com/X-Vector ]\033[93m -[ Twitter : twitter.com/@XVector11 ]\033[95m -[ Facebook: facebook.com/X.Vector1 ]\033[95m -[ GreeteZ : Karem Ali ]\033[94m - - """ % (R, W,R)) -banner() - - -#c = 37209877026138824302301697292319328185824059912506644719041273895972747926698556612194455367172368277268883774102719113194850674012999971337550561061697826458468363574428378679179721672530515881761949297613967812683948622068020382771205609975220407119022966600675469044732891210789248284410739482876937857726026431593283960162298707892143970513804892248370427455318472402011536095802255121284911517882268092785246985981687913310965628793178703498961617628661113277249071490584774764571170891391355080033791745662119139170834529306548644716069025161989103880671580075279656495472299747401794635781847901588156099495017 -#n = 43210326036290106251088810298667915702007744567467854988107937233622821671752930583378558808381197036542903396534337305723496107154050324953536530993744267386008120658910242143992656773360792182698142273761182047011119261779988377408208858109503177413194541524543447827777831409903664749520527375514748678462101261445039900408999429969510786960471887158957679337190091527913340383310225813322177657695256994315048270952315001100548949554323965404643005206732777361386623591544461533797263652780595639288158892122219626720481472616420118039436680647298714768071797941290307081010086729667552569828323354008441335674251 -#d = 36745622940545343875759976434157197886755506358760982257308567037006074391719705315666781200065625116203199598633622026070492775743618607966652393996419663853350085914252797887742782661594880295499227386075929594641556658033207382848075073319786244161193801795105901007640174254798831863226544268004149920625395925259715625248417078457317167458592444532825039828013768181860349705192246622240475711133444054985367520564542488697167606480838294747710396401026533820191299116891186474240520253953309474166963956334430798860084072450328931700881244717326902973061667440442315315084591766881188371668945135391290476758145 -#e = 65537 -#phi = 43210326036290106251088810298667915702007744567467854988107937233622821671752930583378558808381197036542903396534337305723496107154050324953536530993744267386008120658910242143992656773360792182698142273761182047011119261779988377408208858109503177413194541524543447827777831409903664749520527375514748678461684001179681025836243272641520046001108394411608315423967170123709569701951917513847951924936347574671212789694222728087028519090194131250999358518159691864169187723815719657688598576820104123456547706995794001020837756695923476582226622677087727699446323459489485731800382017980557710365098279949382831681952 - -c = input(">>> c = ") -n = input(">>> n = ") -d = input(">>> d = ") -e = input(">>> e = ") -phi = input(">>> phi = ") - - -try: - slowprint("\n[+] Please Wait ... \033[95m\n") - n = hex(pow(c,d,n))[2:] - decode = (n.replace('L','')).decode("hex") - slowprint("[+] The PlainText = ") - print decode -except: - slowprint("\n[!] False Attack ... \033[95m\n") diff --git a/RSA4.py b/RSA4.py deleted file mode 100644 index cac2b5f..0000000 --- a/RSA4.py +++ /dev/null @@ -1,84 +0,0 @@ -import sys -import platform,os -from urllib2 import * -from platform import system -def slowprint(s): - for c in s : - sys.stdout.write(c) - sys.stdout.flush() - time.sleep(4. / 100) - -clear = "" -if "Windows" in platform.system(): - clear = "cls" -if "Linux" in platform.system(): - clear = "clear" -os.system(clear) -is_windows = sys.platform.startswith('win') - -# Console Colors -if is_windows: - # Windows deserves coloring too :D - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - try: - import win_unicode_console , colorama - win_unicode_console.enable() - colorama.init() - #Now the unicode will work ^_^ - except: - print("[!] Error: Coloring libraries not installed, no coloring will be used") - G = Y = B = R = W = G = Y = B = R = W = '' - - -else: - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - - -def banner(): - print("""%s - -_____ ________________ _____ _____ -\ \ / /\ \ _____\ \ / |_ - \ | | / \ /\ \ / / \ | / \\ - \ \ / / | \_\ | | | /___/|| /\ \\ - \ | / | ___/ ____\ \ | ||| | | \\ - / | \ | \ ____ / /\ \|___|/| \/ \\ - / /|\ \ / /\ \/ \| |/ \ \ |\ /\ \\ - |____|/ \|____| /_____/ |\______||\____\ /____/| | \_____\ \_____\\ - | | | | | | | | || | || | | | | | | | - |____| |____| |_____|/ \|_____| \|___||____|/ \|_____|\|_____| - %s%s -[ Version : 0.2 ]\033[92m -[ Author : X-Vector ]\033[96m -[ Github : github.com/X-Vector ]\033[93m -[ Twitter : twitter.com/@XVector11 ]\033[95m -[ Facebook: facebook.com/X.Vector1 ]\033[95m -[ GreeteZ : Karem Ali ]\033[94m - - """ % (R, W,R)) -banner() - -#n = 744818955050534464823866087257532356968231824820271085207879949998948199709147121321290553099733152323288251591199926821010868081248668951049658913424473469563234265317502534369961636698778949885321284313747952124526309774208636874553139856631170172521493735303157992414728027248540362231668996541750186125327789044965306612074232604373780686285181122911537441192943073310204209086616936360770367059427862743272542535703406418700365566693954029683680217414854103 -#d = 108642162821084938181507878056324903120999504739411128372202198922197750954973 -#c = 305357304207903396563769252433798942116307601421155386799392591523875547772911646596463903009990423488430360340024642675941752455429625701977714941340413671092668556558724798890298527900305625979817567613711275466463556061436226589272364057532769439646178423063839292884115912035826709340674104581566501467826782079168130132642114128193813051474106526430253192254354664739229317787919578462780984845602892238745777946945435746719940312122109575086522598667077632 - -c = input(">>> c = ") -n = input(">>> n = ") -d = input(">>> d = ") - -slowprint("\n[+] Please Wait ... \033[95m\n") -try: - decode = pow(c,d,n) - output = (hex(decode)[2:].replace('L','')).decode("hex") - slowprint("[+] The PlainText = ") - print output -except: - slowprint("[-] False Attack !! ") diff --git a/RSA5.py b/RSA5.py deleted file mode 100644 index 0b32086..0000000 --- a/RSA5.py +++ /dev/null @@ -1,156 +0,0 @@ -#!/usr/bin/env python2 -#-*- coding: utf-8 -*- -# -# author : X-Vector -# Tested on Kali Linux / Parrot Os / Ubuntu -# Simple script for RSA Attack -import sys -import platform,os -from urllib2 import * -from platform import system -def slowprint(s): - for c in s : - sys.stdout.write(c) - sys.stdout.flush() - time.sleep(4. / 100) - -clear = "" -if "Windows" in platform.system(): - clear = "cls" -if "Linux" in platform.system(): - clear = "clear" -os.system(clear) -is_windows = sys.platform.startswith('win') - -# Console Colors -if is_windows: - # Windows deserves coloring too :D - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - try: - import win_unicode_console , colorama - win_unicode_console.enable() - colorama.init() - #Now the unicode will work ^_^ - except: - print("[!] Error: Coloring libraries not installed, no coloring will be used") - G = Y = B = R = W = G = Y = B = R = W = '' - - -else: - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - - -def banner(): - print("""%s - -_____ ________________ _____ _____ -\ \ / /\ \ _____\ \ / |_ - \ | | / \ /\ \ / / \ | / \\ - \ \ / / | \_\ | | | /___/|| /\ \\ - \ | / | ___/ ____\ \ | ||| | | \\ - / | \ | \ ____ / /\ \|___|/| \/ \\ - / /|\ \ / /\ \/ \| |/ \ \ |\ /\ \\ - |____|/ \|____| /_____/ |\______||\____\ /____/| | \_____\ \_____\\ - | | | | | | | | || | || | | | | | | | - |____| |____| |_____|/ \|_____| \|___||____|/ \|_____|\|_____| - %s%s -[ Version : 0.2 ]\033[92m -[ Author : X-Vector ]\033[96m -[ Github : github.com/X-Vector ]\033[93m -[ Twitter : twitter.com/@XVector11 ]\033[95m -[ Facebook: facebook.com/X.Vector1 ]\033[95m -[ GreeteZ : Karem Ali ]\033[94m - """ % (R, W,R)) -banner() - -from Crypto.PublicKey import RSA -import sympy as sp -import math -""" -#n = 441702548375597546265397060347208952276967475274905135912477869560482177333510361384285033947480946188145623354029215951664169885735094097025317925522966956898965174945445544364452506966796499944587005657873812585848386501149925011035458189838748181551356757610548391767844207725796544451539421763642181449329080300719368344152034024278647417306053843499995586981318017650558507022155655502745770884654996618443849292140572931614016663705074656028412717677339714961533961375052773639815540324868758969940933964991941461245158471917647200963814004656567184986255121144463936821101981682767655530654441359046234900674147458678928268719664791590208181725402425003322043157763682685830046581059368233541228450498829982145850640255968763746232611610641869191302389245779111356909617313529277007241602005067848947949307502548984897136138368091937447795052112684321369869694696022995282137270503420632588160475186971207198459134307608759756767731733884585073817881348559945137106821730698056732508316693757300672313883162959195685130393016049081156405669205548223148112949446059403857423978820671852027526778494740114169769151540080734651779260084907492653816632846142736609605163394526211986727369288330063666919831047575220019292555559557 -#e = 65537 -#d = 20929658227875938261058725840264131720240194797254721497929999366502087574029656881231720165783065592372637815940915666212613039099487922843233911353847485450062009142435445746729639472299106954973763080234456847113054478990804619551767588431261008218457316621723802599302359261042715498257923026077002571619206016545993843650901654469988654046781302853813921776892035397733517301594459235978192450212096281249109282450379464222318996342527212038893311335406614944259596938858799625134827471785811994514192796802507658154603626182055262115513322362471136252282318391917574551672482430283925389405151320872665308738881 -#c = 223986220247071217870216666779014143338794244806897003439570261828585609461069625566993441707552858342245944642945164681672765137831785039339186019388055221324788275090628463675778644169814393286912058814837219760390476510773975243291075922094945253348123263684444264887680045467214228402806502985409400661033608167210517145287411533259812656955874094033211161421678440562053003042479607653554525213535393053262147643051962890823534380167045346153321162009201462563059751490608169219660707113678434438625268806729271472128212898965284892857326073914569265528121606803508514214189119223383833552072586814770466728871437446190820676075097507547134736007108263775508884871752392084557481102147503606249246354860813514035927154754953172611572565854652996865901035897405554136502508927222416891115275427864126981053248969607448749019664721233739296140343775145313825978714021202368146466869500962807323607852112530742349058090066478686837929805776585570594765868633295031229876778165218553418581369590926767847888849449544777172583513049998378026545824378239267780173494553559807077301106319453742180763436936829925045341906074598727300955137592558540978215844567601254095649299172760605263652690076724946138284654947266095386055238602230 -""" -n = input(">>> n = ") -e = input(">>> e = ") -d = input(">>> d = ") -c = input(">>> c = ") - -slowprint("\n[+] Please Wait ... \033[95m\n") - -def premRSA(n,e,d,c): - bitLenN = int(sp.floor(sp.log(n)/sp.log(2)) + 1) - bitLenD0 = 2048 - - assert bitLenD0 >= bitLenN/2 - - d = halfdPartialKeyRecoveryAttack(d,2048,4096,n,e) - c = pow(c,d,n) - plaintext = str(bytearray(hex(c)[2:])).replace("L","") - slowprint("[+] The PlainText = ") - print(plaintext.decode("hex")) - -def halfdPartialKeyRecoveryAttack(d0,d0BitSize,nBitSize,n="n",e="e"): - test = pow(3, e, n) - test2 = pow(5, e, n) - for k in range(1,e): - d = ((k * n + 1) // e) - d >>= d0BitSize - d <<= d0BitSize - d |= d0 - if((e * d) % k == 1): - if pow(test, d, n) == 3: - if pow(test2, d, n) == 5: - totientN = (e*d - 1) // k - b = totientN - n - 1 - discriminant = b*b - 4*n - root = floorSqrt(discriminant) - if(root*root != discriminant): - continue - p = (-b + root) // 2 - q = n // p - return d - - -def extended_gcd(aa, bb): - lastremainder, remainder = abs(aa), abs(bb) - x, lastx, y, lasty = 0, 1, 1, 0 - while remainder: - lastremainder, (quotient, remainder) = remainder, divmod(lastremainder, remainder) - x, lastx = lastx - quotient*x, x - y, lasty = lasty - quotient*y, y - return lastremainder, lastx * (-1 if aa < 0 else 1), lasty * (-1 if bb < 0 else 1) - -def modinv(a, m): - """Modular Multiplicative Inverse, - from https://rosettacode.org/wiki/Modular_inverse#Python - """ - g, x, y = extended_gcd(a, m) - if g != 1: - raise ValueError - return x % m - -def floorSqrt(n): - x = n - y = (x + 1) // 2 - while y < x: - x = y - y = (x + n // x) // 2 - return x - - - - -try: - premRSA(n,e,d,c) -except: - slowprint("\n[!] False Attack ... \033[95m\n") diff --git a/RSA6.py b/RSA6.py deleted file mode 100644 index 068fd93..0000000 --- a/RSA6.py +++ /dev/null @@ -1,109 +0,0 @@ -#!/usr/bin/env python2 -#-*- coding: utf-8 -*- -# -# author : X-Vector -# Tested on Kali Linux / Parrot Os / Ubuntu -# Simple script for RSA Attack -import sys -import platform,os -from urllib2 import * -from platform import system -def slowprint(s): - for c in s : - sys.stdout.write(c) - sys.stdout.flush() - time.sleep(4. / 100) - -clear = "" -if "Windows" in platform.system(): - clear = "cls" -if "Linux" in platform.system(): - clear = "clear" -os.system(clear) -is_windows = sys.platform.startswith('win') - -# Console Colors -if is_windows: - # Windows deserves coloring too :D - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - try: - import win_unicode_console , colorama - win_unicode_console.enable() - colorama.init() - #Now the unicode will work ^_^ - except: - print("[!] Error: Coloring libraries not installed, no coloring will be used") - G = Y = B = R = W = G = Y = B = R = W = '' - - -else: - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - - -def banner(): - print("""%s - -_____ ________________ _____ _____ -\ \ / /\ \ _____\ \ / |_ - \ | | / \ /\ \ / / \ | / \\ - \ \ / / | \_\ | | | /___/|| /\ \\ - \ | / | ___/ ____\ \ | ||| | | \\ - / | \ | \ ____ / /\ \|___|/| \/ \\ - / /|\ \ / /\ \/ \| |/ \ \ |\ /\ \\ - |____|/ \|____| /_____/ |\______||\____\ /____/| | \_____\ \_____\\ - | | | | | | | | || | || | | | | | | | - |____| |____| |_____|/ \|_____| \|___||____|/ \|_____|\|_____| - %s%s -[ Version : 0.2 ]\033[92m -[ Author : X-Vector ]\033[96m -[ Github : github.com/X-Vector ]\033[93m -[ Twitter : twitter.com/@XVector11 ]\033[95m -[ Facebook: facebook.com/X.Vector1 ]\033[95m -[ GreeteZ : Karem Ali ]\033[94m - - """ % (R, W,R)) -banner() -import binascii - -""" -p = 153143042272527868798412612417204434156935146874282990942386694020462861918068684561281763577034706600608387699148071015194725533394126069826857182428660427818277378724977554365910231524827258160904493774748749088477328204812171935987088715261127321911849092207070653272176072509933245978935455542420691737433 -c = 14699632914289984358210582075909309608817619615764122409514577850253033131275996955127394794512801987443786025277031557775238937388086632327611216460613138074110905840487213116627139558528011448028813522809156509374602431319619052803531008581645459969997969897966475478980398396687104746302415391434104278327526947341073215599683555703818611403510483832532921625745935543818100178607417658929607435582550913918895885596025822532531372429301293588416086854338617700672628239475365045267537032531973594689061842791028000992635092519215619497247452814483357403667400283975070444902253349175045305073603687442947532925780 -e = 65537 -n = 23952937352643527451379227516428377705004894508566304313177880191662177061878993798938496818120987817049538365206671401938265663712351239785237507341311858383628932183083145614696585411921662992078376103990806989257289472590902167457302888198293135333083734504191910953238278860923153746261500759411620299864395158783509535039259714359526738924736952759753503357614939203434092075676169179112452620687731670534906069845965633455748606649062394293289967059348143206600765820021392608270528856238306849191113241355842396325210132358046616312901337987464473799040762271876389031455051640937681745409057246190498795697239 -""" -c = input(">>> c = ") -n = input(">>> n = ") -e = input(">>> e = ") -p = input(">>> p or q = ") - - -slowprint("\n[+] Please Wait ... \033[95m\n") - -q = n/p -phi = (p-1)*(q-1) -def egcd(a,b): - if a == 0 : - return(b,0,1) - else: - g,y,x = egcd(b%a,a) - return (g,x-(b/a)*y,y) -def modinv(a,m): - g,x,y = egcd(a,m) - if g != 1: - raise Expection("RSA Hello") - else : - return x%m - -d = modinv(e,phi) -decode = pow(c,d,n) -output = (hex(decode)[2:].replace('L','')).decode("hex") -slowprint("[+] The PlainText = ") -print(output) diff --git a/RSA7.py b/RSA7.py deleted file mode 100644 index 9ededd1..0000000 --- a/RSA7.py +++ /dev/null @@ -1,117 +0,0 @@ -#!/usr/bin/env python2 -#-*- coding: utf-8 -*- -# -# author : X-Vector -# Tested on Kali Linux / Parrot Os / Ubuntu -# Simple script for RSA Attack -import sys -import platform,os -from urllib2 import * -from platform import system -def slowprint(s): - for c in s : - sys.stdout.write(c) - sys.stdout.flush() - time.sleep(4. / 100) - -clear = "" -if "Windows" in platform.system(): - clear = "cls" -if "Linux" in platform.system(): - clear = "clear" -os.system(clear) -is_windows = sys.platform.startswith('win') - -# Console Colors -if is_windows: - # Windows deserves coloring too :D - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - try: - import win_unicode_console , colorama - win_unicode_console.enable() - colorama.init() - #Now the unicode will work ^_^ - except: - print("[!] Error: Coloring libraries not installed, no coloring will be used") - G = Y = B = R = W = G = Y = B = R = W = '' - - -else: - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - - -def banner(): - print("""%s - -_____ ________________ _____ _____ -\ \ / /\ \ _____\ \ / |_ - \ | | / \ /\ \ / / \ | / \\ - \ \ / / | \_\ | | | /___/|| /\ \\ - \ | / | ___/ ____\ \ | ||| | | \\ - / | \ | \ ____ / /\ \|___|/| \/ \\ - / /|\ \ / /\ \/ \| |/ \ \ |\ /\ \\ - |____|/ \|____| /_____/ |\______||\____\ /____/| | \_____\ \_____\\ - | | | | | | | | || | || | | | | | | | - |____| |____| |_____|/ \|_____| \|___||____|/ \|_____|\|_____| - %s%s -[ Version : 0.2 ]\033[92m -[ Author : X-Vector ]\033[96m -[ Github : github.com/X-Vector ]\033[93m -[ Twitter : twitter.com/@XVector11 ]\033[95m -[ Facebook: facebook.com/X.Vector1 ]\033[95m -[ GreeteZ : Karem Ali ]\033[94m - - """ % (R, W,R)) -banner() -import binascii -""" -e = 65537 -n = 642313240848064014975043934308658242447312485152342673610756859535090103704610472004913349502648157091104463303511131278665176160214474038294042375555935567033107229886104534241324327133387923226576002115108963521725703773387678635509903034467838260875686083768549775481391190161412646384559222421917626615323 -dp = 17765378008759755288183210466105878526943875374957170036175281330288884608317141953683920408636506981101765935449140323585600732241535721917282237462133813 -c = 147903288008907053469880199469959588903705520519775597541160700501753344741954421604588338524905987922631822425828587114084662512860181022047137469441292833823381362238861070683420786510831001513730638949486694641768638258876688738949817816449109334961820861920165271653627904957302093274915248851406573361863 -""" -c = input(">>> c = ") -n = input(">>> n = ") -e = input(">>> e = ") -dp = input(">>> dp = ") - -# dP = (1/e) mod (p-1) -mp = (dp * e) - 1 #mp is multiple of p-1 -for i in range(2,1000000): - p = (mp / i) + 1 - if n % p == 0: - break -#print "p = ",p -q = n/p -#print "q = ",q -slowprint("\n[+] Please Wait ... \033[95m\n") -try: - phi = (p-1)*(q-1) - def egcd(a,b): - if a == 0 : - return(b,0,1) - else: - g,y,x = egcd(b%a,a) - return (g,x-(b/a)*y,y) - def modinv(a,m): - g,x,y = egcd(a,m) - if g != 1: - raise Expection("[-] Sorry") - else : - return x%m - - d = modinv(e,phi) - decode = pow(c,d,n) - output = (hex(decode)[2:].replace('L','')).decode("hex") - slowprint("[+] The PlainText = ") - print(output) -except: - slowprint("[-] False Attack !! ") diff --git a/RSA8.py b/RSA8.py deleted file mode 100644 index 138bd3b..0000000 --- a/RSA8.py +++ /dev/null @@ -1,88 +0,0 @@ -import sys -import platform,os -from urllib2 import * -from platform import system -def slowprint(s): - for c in s : - sys.stdout.write(c) - sys.stdout.flush() - time.sleep(4. / 100) - -clear = "" -if "Windows" in platform.system(): - clear = "cls" -if "Linux" in platform.system(): - clear = "clear" -os.system(clear) -is_windows = sys.platform.startswith('win') - -# Console Colors -if is_windows: - # Windows deserves coloring too :D - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - try: - import win_unicode_console , colorama - win_unicode_console.enable() - colorama.init() - #Now the unicode will work ^_^ - except: - print("[!] Error: Coloring libraries not installed, no coloring will be used") - G = Y = B = R = W = G = Y = B = R = W = '' - - -else: - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - - -def banner(): - print("""%s - -_____ ________________ _____ _____ -\ \ / /\ \ _____\ \ / |_ - \ | | / \ /\ \ / / \ | / \\ - \ \ / / | \_\ | | | /___/|| /\ \\ - \ | / | ___/ ____\ \ | ||| | | \\ - / | \ | \ ____ / /\ \|___|/| \/ \\ - / /|\ \ / /\ \/ \| |/ \ \ |\ /\ \\ - |____|/ \|____| /_____/ |\______||\____\ /____/| | \_____\ \_____\\ - | | | | | | | | || | || | | | | | | | - |____| |____| |_____|/ \|_____| \|___||____|/ \|_____|\|_____| - %s%s -[ Version : 0.2 ]\033[92m -[ Author : X-Vector ]\033[96m -[ Github : github.com/X-Vector ]\033[93m -[ Twitter : twitter.com/@XVector11 ]\033[95m -[ Facebook: facebook.com/X.Vector1 ]\033[95m -[ GreeteZ : Karem Ali ]\033[94m - """ % (R, W,R)) -banner() - -import gmpy2 -import binascii - -gmpy2.get_context().precision=99999999999999999 - -# Example -#c = 74802199268254280440493690700608296874229186682164386879225634595063352871356558051619716745745659385094914838131944342112615526475056433710891149995541243955498341403563518984577892267288643941 -c = input(">>> c = ") -slowprint("\n[+] Please Wait ... \033[95m\n") -try: - m = gmpy2.iroot(c, 3)[0] - assert pow(m,3) == c - def hex_pair(x): - return ('0' * (len(x) % 2)) + x - m_hex = '{:x}'.format(m) - m_hex = hex_pair(m_hex) - msg = binascii.unhexlify(m_hex) - slowprint("[+] The PlainText = ") - print(msg.decode()) -except: - slowprint("[-] False Attack !! ") diff --git a/RSA_chinese_remainder_theorem.py b/RSA_chinese_remainder_theorem.py deleted file mode 100644 index a465dc9..0000000 --- a/RSA_chinese_remainder_theorem.py +++ /dev/null @@ -1,109 +0,0 @@ -import sys -import platform,os -from urllib2 import * -from platform import system -def slowprint(s): - for c in s : - sys.stdout.write(c) - sys.stdout.flush() - time.sleep(4. / 100) - -clear = "" -if "Windows" in platform.system(): - clear = "cls" -if "Linux" in platform.system(): - clear = "clear" -os.system(clear) -is_windows = sys.platform.startswith('win') - -# Console Colors -if is_windows: - # Windows deserves coloring too :D - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - try: - import win_unicode_console , colorama - win_unicode_console.enable() - colorama.init() - #Now the unicode will work ^_^ - except: - print("[!] Error: Coloring libraries not installed, no coloring will be used") - G = Y = B = R = W = G = Y = B = R = W = '' - - -else: - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - - -def banner(): - print("""%s - -_____ ________________ _____ _____ -\ \ / /\ \ _____\ \ / |_ - \ | | / \ /\ \ / / \ | / \\ - \ \ / / | \_\ | | | /___/|| /\ \\ - \ | / | ___/ ____\ \ | ||| | | \\ - / | \ | \ ____ / /\ \|___|/| \/ \\ - / /|\ \ / /\ \/ \| |/ \ \ |\ /\ \\ - |____|/ \|____| /_____/ |\______||\____\ /____/| | \_____\ \_____\\ - | | | | | | | | || | || | | | | | | | - |____| |____| |_____|/ \|_____| \|___||____|/ \|_____|\|_____| - %s%s -[ Version : 0.2 ]\033[92m -[ Author : X-Vector ]\033[96m -[ Github : github.com/X-Vector ]\033[93m -[ Twitter : twitter.com/@XVector11 ]\033[95m -[ Facebook: facebook.com/X.Vector1 ]\033[95m -[ GreeteZ : Karem Ali ]\033[94m - - """ % (R, W,R)) -banner() -""" -#Example - -c = 62078086677416686867183857957350338314446280912673392448065026850212685326551183962056495964579782325302082054393933682265772802750887293602432512967994805549965020916953644635965916607925335639027579187435180607475963322465417758959002385451863122106487834784688029167720175128082066670945625067803812970871 -p = 7901324502264899236349230781143813838831920474669364339844939631481665770635584819958931021644265960578585153616742963330195946431321644921572803658406281 -q = 12802918451444044622583757703752066118180068668479378778928741088302355425977192996799623998720429594346778865275391307730988819243843851683079000293815051 -dp = 5540655028622021934429306287937775291955623308965208384582009857376053583575510784169616065113641391169613969813652523507421157045377898542386933198269451 -dq = 9066897320308834206952359399737747311983309062764178906269475847173966073567988170415839954996322314157438770225952491560052871464136163421892050057498651 -""" - -c = input(">>> c = ") -p = input(">>> p = ") -q = input(">>> q = ") -dp = input(">>> dp = ") -dq = input(">>> dq = ") - -slowprint("\n[+] Please Wait ... \033[95m\n") - -try: - def egcd(a,b): - if a == 0: - return (b, 0, 1) - else: - g, y, x = egcd(b % a, a) - return (g, x - (b / a) * y, y) - def modinv(a,m): - g, x, y = egcd(a, m) - if g != 1: - raise Exception('modular inverse does not exist') - else: - return x % m - def chinese_remainder_theorem(p,q,dp,dq,chipher_text): - q_inv = modinv(p , q) - m1 = pow(chipher_text,dp,p) - m2 = pow(chipher_text,dq,q) - h = (q_inv*(m1-m2)) % p - return m2 + h * q - decode = hex(chinese_remainder_theorem(p,q,dp,dq,c))[2:].replace('L','') - slowprint("[+] The PlainText = ") - print decode.decode("hex") -except: - slowprint("[-] False Attack !! \n") diff --git a/RSA_common_modulus.py b/RSA_common_modulus.py deleted file mode 100644 index 85653a5..0000000 --- a/RSA_common_modulus.py +++ /dev/null @@ -1,120 +0,0 @@ -import sys -import platform,os -from urllib2 import * -from platform import system -def slowprint(s): - for c in s : - sys.stdout.write(c) - sys.stdout.flush() - time.sleep(4. / 100) - -clear = "" -if "Windows" in platform.system(): - clear = "cls" -if "Linux" in platform.system(): - clear = "clear" -os.system(clear) -is_windows = sys.platform.startswith('win') - -# Console Colors -if is_windows: - # Windows deserves coloring too :D - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - try: - import win_unicode_console , colorama - win_unicode_console.enable() - colorama.init() - #Now the unicode will work ^_^ - except: - print("[!] Error: Coloring libraries not installed, no coloring will be used") - G = Y = B = R = W = G = Y = B = R = W = '' - - -else: - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - - -def banner(): - print("""%s - -_____ ________________ _____ _____ -\ \ / /\ \ _____\ \ / |_ - \ | | / \ /\ \ / / \ | / \\ - \ \ / / | \_\ | | | /___/|| /\ \\ - \ | / | ___/ ____\ \ | ||| | | \\ - / | \ | \ ____ / /\ \|___|/| \/ \\ - / /|\ \ / /\ \/ \| |/ \ \ |\ /\ \\ - |____|/ \|____| /_____/ |\______||\____\ /____/| | \_____\ \_____\\ - | | | | | | | | || | || | | | | | | | - |____| |____| |_____|/ \|_____| \|___||____|/ \|_____|\|_____| - %s%s -[ Version : 0.2 ]\033[92m -[ Author : X-Vector ]\033[96m -[ Github : github.com/X-Vector ]\033[93m -[ Twitter : twitter.com/@XVector11 ]\033[95m -[ Facebook: facebook.com/X.Vector1 ]\033[95m -[ GreeteZ : Karem Ali ]\033[94m - - """ % (R, W,R)) -banner() -c1 = input(">>> c1 = ") -c2 = input(">>> c2 = ") -e1 = input(">>> e1 = ") -e2 = input(">>> e2 = ") -n = input(">>> n = ") - -slowprint("\n[+] Please Wait ... \033[95m\n") - - -from sys import setrecursionlimit -import codecs -class Comod(object): - def accueil(self): - slowprint("[+] Getting info ... ") - setrecursionlimit(1000000) - def xgcd(self, a, b): - if a == 0: - return (b, 0, 1) - else: - gcd, u, v = self.xgcd(b % a, a) - return (gcd, v - (b // a) * u, u) - def modinv(self, a, n): - g, x, y = self.xgcd(a, n) - return x % n - def pow_mod(self, a, b, n): - number = 1 - while b: - if b & 1: - number = number * a % n - b >>= 1 - a = a * a % n - return number - def __init__(self, n, e1, e2, c1, c2): - - self.accueil() - egcd = self.xgcd(e1, e2) - u, v = egcd[1], egcd[2] - if u >= 0: - p1 = self.pow_mod(c1,u,n) - else: - p1 = self.modinv(self.pow_mod(c1,-u,n),n) - if v >= 0: - p2 = self.pow_mod(c2,v,n) - else: - p2 = self.modinv(self.pow_mod(c2,(-v),n),n) - res = (p1 * p2) % n - print "\n\t[+] Decimal plaintext: ",res,"\n" - try: - plaintext = codecs.decode(hex(res)[2:].replace('L','')) - print "\t[+] The plaintext = ",plaintext - except: - slowprint("\t[-] Can't Get The PlainText\n") -Comod(n, e1, e2, c1, c2) diff --git a/RSA_fermat.py b/RSA_fermat.py deleted file mode 100644 index 02309e0..0000000 --- a/RSA_fermat.py +++ /dev/null @@ -1,129 +0,0 @@ -import sys -import platform,os -from urllib2 import * -from platform import system -def slowprint(s): - for c in s + '\n': - sys.stdout.write(c) - sys.stdout.flush() - time.sleep(4. / 100) - -clear = "" -if "Windows" in platform.system(): - clear = "cls" -if "Linux" in platform.system(): - clear = "clear" -os.system(clear) -is_windows = sys.platform.startswith('win') - -# Console Colors -if is_windows: - # Windows deserves coloring too :D - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - try: - import win_unicode_console , colorama - win_unicode_console.enable() - colorama.init() - #Now the unicode will work ^_^ - except: - print("[!] Error: Coloring libraries not installed, no coloring will be used") - G = Y = B = R = W = G = Y = B = R = W = '' - - -else: - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - - -def banner(): - print("""%s - -_____ ________________ _____ _____ -\ \ / /\ \ _____\ \ / |_ - \ | | / \ /\ \ / / \ | / \\ - \ \ / / | \_\ | | | /___/|| /\ \\ - \ | / | ___/ ____\ \ | ||| | | \\ - / | \ | \ ____ / /\ \|___|/| \/ \\ - / /|\ \ / /\ \/ \| |/ \ \ |\ /\ \\ - |____|/ \|____| /_____/ |\______||\____\ /____/| | \_____\ \_____\\ - | | | | | | | | || | || | | | | | | | - |____| |____| |_____|/ \|_____| \|___||____|/ \|_____|\|_____| - %s%s -[ Version : 0.2 ]\033[92m -[ Author : X-Vector ]\033[96m -[ Github : github.com/X-Vector ]\033[93m -[ Twitter : twitter.com/@XVector11 ]\033[95m -[ Facebook: facebook.com/X.Vector1 ]\033[95m -[ GreeteZ : Karem Ali ]\033[94m - - """ % (R, W,R)) -banner() - -n = input(">>> n = ") -limit = input(">>> limit = ") - -slowprint("\n[+] Please Wait ... \033[95m\n") - - -import sys -import math -import Crypto.PublicKey.RSA -import fractions -import random -import argparse -class Fermat(object): - def accueil(self): - slowprint("[+] Getting info ... ") - def carre_parfait(self, x): - if x < 1: - return(False) - sqrt_x = math.sqrt(x) - return (sqrt_x == int(math.floor(sqrt_x))) - def fermat(self,n): - a = 2*math.ceil(math.sqrt(n)) + 1 - aux = 2 * a +1 - n4 = 4 * n - c = pow(a, 2) - n4 - while not carre_parfait(c): - c += aux - a += 1 - aux += 2 - b = int(math.sqrt(c)) - p = (a - b) // 2 - q = (a + b) // 2 - if (p*q != n): - slowprint("Error!") - exit(0) - return (p, q) - def indicatrice_euler(self, p, q): - return((p - 1) * (q - 1)) - def bezout(self, a, b): - if a == 0 and b == 0: - return (0, 0, 0) - if b == 0: - return (a // abs(a), 0, abs(a)) - (u, v, p) = self.bezout(b, a % b) - return (v, (u - v * (a // b)), p) - def inv_modulo(self, x, m): - (u, _, p) = self.bezout(x, m) - return u % abs(m) - def __init__(self, n, e): - self.accueil() - try: - (p, q) = self.fermat(n, e) - except: - slowprint("\n[-] Sorry This RSA public key isn't a valide candidate for a Fermat Attack\n") - exit() - print("\n\t[+] Factorization = {} * {}\n".format(p,q)) - phi = indicatrice_euler(p,q) - self.d = inv_modulo(e, phi) - print("[+] d = {}\n".format(d)) - -Fermat(n,limit) diff --git a/RSA_hasted.py b/RSA_hasted.py deleted file mode 100644 index a19fd37..0000000 --- a/RSA_hasted.py +++ /dev/null @@ -1,138 +0,0 @@ -import sys -import platform,os -from urllib2 import * -from platform import system -def slowprint(s): - for c in s : - sys.stdout.write(c) - sys.stdout.flush() - time.sleep(4. / 100) - -clear = "" -if "Windows" in platform.system(): - clear = "cls" -if "Linux" in platform.system(): - clear = "clear" -os.system(clear) -is_windows = sys.platform.startswith('win') - -# Console Colors -if is_windows: - # Windows deserves coloring too :D - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - try: - import win_unicode_console , colorama - win_unicode_console.enable() - colorama.init() - #Now the unicode will work ^_^ - except: - print("[!] Error: Coloring libraries not installed, no coloring will be used") - G = Y = B = R = W = G = Y = B = R = W = '' - - -else: - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - - -def banner(): - print("""%s - -_____ ________________ _____ _____ -\ \ / /\ \ _____\ \ / |_ - \ | | / \ /\ \ / / \ | / \\ - \ \ / / | \_\ | | | /___/|| /\ \\ - \ | / | ___/ ____\ \ | ||| | | \\ - / | \ | \ ____ / /\ \|___|/| \/ \\ - / /|\ \ / /\ \/ \| |/ \ \ |\ /\ \\ - |____|/ \|____| /_____/ |\______||\____\ /____/| | \_____\ \_____\\ - | | | | | | | | || | || | | | | | | | - |____| |____| |_____|/ \|_____| \|___||____|/ \|_____|\|_____| - %s%s -[ Version : 0.2 ]\033[92m -[ Author : X-Vector ]\033[96m -[ Github : github.com/X-Vector ]\033[93m -[ Twitter : twitter.com/@XVector11 ]\033[95m -[ Facebook: facebook.com/X.Vector1 ]\033[95m -[ GreeteZ : Karem Ali ]\033[94m - - """ % (R, W,R)) -banner() - - - -# N1 = 79608037716527910392060670707842954224114341083822168077002144855358998405023007345791355970838437273653492726857398313047195654933011803740498167538754807659255275632647165202835846338059572102420992692073303341392512490988413552501419357400503232190597741120726276250753866130679586474440949586692852365179 -# C1 = 34217065803425349356447652842993191079705593197469002356250751196039765990549766822180265723173964726087016890980051189787233837925650902081362222218365748633591895514369317316450142279676583079298758397507023942377316646300547978234729578678310028626408502085957725408232168284955403531891866121828640919987 -# N2 = 58002222048141232855465758799795991260844167004589249261667816662245991955274977287082142794911572989261856156040536668553365838145271642812811609687362700843661481653274617983708937827484947856793885821586285570844274545385852401777678956217807768608457322329935290042362221502367207511491516411517438589637 -# C2 = 48038542572368143315928949857213341349144690234757944150458420344577988496364306227393161112939226347074838727793761695978722074486902525121712796142366962172291716190060386128524977245133260307337691820789978610313893799675837391244062170879810270336080741790927340336486568319993335039457684586195656124176 -# N3 = 95136786745520478217269528603148282473715660891325372806774750455600642337159386952455144391867750492077191823630711097423473530235172124790951314315271310542765846789908387211336846556241994561268538528319743374290789112373774893547676601690882211706889553455962720218486395519200617695951617114702861810811 -# C3 = 55139001168534905791033093049281485849516290567638780139733282880064346293967470884523842813679361232423330290836063248352131025995684341143337417237119663347561882637003640064860966432102780676449991773140407055863369179692136108534952624411669691799286623699981636439331427079183234388844722074263884842748 - - -C1 = input(">>> c1 = ") -C2 = input(">>> c2 = ") -C3 = input(">>> c3 = ") -N1 = input(">>> n1 = ") -N2 = input(">>> n2 = ") -N3 = input(">>> n3 = ") - - -slowprint("\n[+] Please Wait ... \033[95m\n") - -import functools -import itertools - -def chinese_remainder(n, a): - sum = 0 - prod = functools.reduce(lambda a, b: a*b, n) - for n_i, a_i in zip(n, a): - p = prod // n_i - sum += a_i * mul_inv(p, n_i) * p - return sum % prod - -def mul_inv(a, b): - b0 = b - x0, x1 = 0, 1 - if b == 1: return 1 - while a > 1: - q = a // b - a, b = b, a%b - x0, x1 = x1 - q * x0, x0 - if x1 < 0: x1 += b0 - return x1 - -def inv_pow(c, e): - low = -1 - high = c+1 - while low + 1 < high: - m = (low + high) // 2 - p = pow(m, e) - if p < c: - low = m - else: - high = m - m = high - assert pow(m, e) == c - return m - -N = [N1, N2, N3] -C = [C1, C2, C3] -e = len(N) -try: - a = chinese_remainder(N, C) - for n, c in zip(N, C): - assert a % n == c - m = inv_pow(a, e) - decode = (hex(m)[2:]).replace('L','') - slowprint("[+] The PlainText = ") - print decode.decode("hex") - slowprint("\n[+] Thanx For Using X-RSA Tool <3 \033[95m\n") -except: - slowprint("[-] False Attack !! ") diff --git a/RSA_hasted2.py b/RSA_hasted2.py deleted file mode 100644 index e2a9fd2..0000000 --- a/RSA_hasted2.py +++ /dev/null @@ -1,131 +0,0 @@ -import sys -import platform,os -from urllib2 import * -from platform import system -def slowprint(s): - for c in s : - sys.stdout.write(c) - sys.stdout.flush() - time.sleep(4. / 100) - -clear = "" -if "Windows" in platform.system(): - clear = "cls" -if "Linux" in platform.system(): - clear = "clear" -os.system(clear) -is_windows = sys.platform.startswith('win') - -# Console Colors -if is_windows: - # Windows deserves coloring too :D - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - try: - import win_unicode_console , colorama - win_unicode_console.enable() - colorama.init() - #Now the unicode will work ^_^ - except: - print("[!] Error: Coloring libraries not installed, no coloring will be used") - G = Y = B = R = W = G = Y = B = R = W = '' - - -else: - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - - -def banner(): - print("""%s - -_____ ________________ _____ _____ -\ \ / /\ \ _____\ \ / |_ - \ | | / \ /\ \ / / \ | / \\ - \ \ / / | \_\ | | | /___/|| /\ \\ - \ | / | ___/ ____\ \ | ||| | | \\ - / | \ | \ ____ / /\ \|___|/| \/ \\ - / /|\ \ / /\ \/ \| |/ \ \ |\ /\ \\ - |____|/ \|____| /_____/ |\______||\____\ /____/| | \_____\ \_____\\ - | | | | | | | | || | || | | | | | | | - |____| |____| |_____|/ \|_____| \|___||____|/ \|_____|\|_____| - %s%s -[ Version : 0.2 ]\033[92m -[ Author : X-Vector ]\033[96m -[ Github : github.com/X-Vector ]\033[93m -[ Twitter : twitter.com/@XVector11 ]\033[95m -[ Facebook: facebook.com/X.Vector1 ]\033[95m -[ GreeteZ : Karem Ali ]\033[94m - - """ % (R, W,R)) -banner() - - -#Example -""" -N1 = 79608037716527910392060670707842954224114341083822168077002144855358998405023007345791355970838437273653492726857398313047195654933011803740498167538754807659255275632647165202835846338059572102420992692073303341392512490988413552501419357400503232190597741120726276250753866130679586474440949586692852365179 -C1 = 34217065803425349356447652842993191079705593197469002356250751196039765990549766822180265723173964726087016890980051189787233837925650902081362222218365748633591895514369317316450142279676583079298758397507023942377316646300547978234729578678310028626408502085957725408232168284955403531891866121828640919987 -N2 = 58002222048141232855465758799795991260844167004589249261667816662245991955274977287082142794911572989261856156040536668553365838145271642812811609687362700843661481653274617983708937827484947856793885821586285570844274545385852401777678956217807768608457322329935290042362221502367207511491516411517438589637 -C2 = 48038542572368143315928949857213341349144690234757944150458420344577988496364306227393161112939226347074838727793761695978722074486902525121712796142366962172291716190060386128524977245133260307337691820789978610313893799675837391244062170879810270336080741790927340336486568319993335039457684586195656124176 -N3 = 95136786745520478217269528603148282473715660891325372806774750455600642337159386952455144391867750492077191823630711097423473530235172124790951314315271310542765846789908387211336846556241994561268538528319743374290789112373774893547676601690882211706889553455962720218486395519200617695951617114702861810811 -C3 = 55139001168534905791033093049281485849516290567638780139733282880064346293967470884523842813679361232423330290836063248352131025995684341143337417237119663347561882637003640064860966432102780676449991773140407055863369179692136108534952624411669691799286623699981636439331427079183234388844722074263884842748 -e = 3 -""" -C1 = input(">>> c1 = ") -C2 = input(">>> c2 = ") -C3 = input(">>> c3 = ") -N1 = input(">>> n1 = ") -N2 = input(">>> n2 = ") -N3 = input(">>> n3 = ") -e = input(">>> e = ") - -slowprint("\n[+] Please Wait ... \033[95m\n") - -try: - def mulinv(a,b): - b0 = b - x0, x1 = 0, 1 - if b == 1: return 1 - while a > 1: - q = a / b - a, b = b, a%b - x0, x1 = x1 - q * x0, x0 - if x1 < 0: x1 += b0 - return x1 - def powinv(x,n): - high = 1 - while high ** n < x: - high *= 2 - low = high/2 - while low < high: - mid = (low + high) // 2 - if low < mid and mid**n < x: - low = mid - elif high > mid and mid**n > x: - high = mid - else: - return mid - return mid + 1 - - def hasted(n1,n2,n3,chipher_text_1,chipher_text_2,chipher_text_3,e): - n = [n1,n2,n3] - a = [chipher_text_1,chipher_text_2,chipher_text_3] - sum = 0 - prod = reduce(lambda a, b: a*b, n) - for n_i, a_i in zip(n, a): - p = prod / n_i - sum += a_i * mulinv(p, n_i) * p - return powinv(sum % p,e) - - decode = (hex(hasted(N1,N2,N3,C1,C2,C3,e))[2:]).replace('L','') - slowprint("[+] The PlainText = ") - print decode.decode("hex") - slowprint("[+] Thanx For Using X-RSA Tool <3 \033[95m\n") -except: - slowprint("[-] False Attack !! \n") diff --git a/RSA_multiPrime1.py b/RSA_multiPrime1.py deleted file mode 100644 index fb584a9..0000000 --- a/RSA_multiPrime1.py +++ /dev/null @@ -1,141 +0,0 @@ -import sys -import platform,os -from urllib2 import * -from platform import system -def slowprint(s): - for c in s : - sys.stdout.write(c) - sys.stdout.flush() - time.sleep(4. / 100) - -clear = "" -if "Windows" in platform.system(): - clear = "cls" -if "Linux" in platform.system(): - clear = "clear" -os.system(clear) -is_windows = sys.platform.startswith('win') - -# Console Colors -if is_windows: - # Windows deserves coloring too :D - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - try: - import win_unicode_console , colorama - win_unicode_console.enable() - colorama.init() - #Now the unicode will work ^_^ - except: - print("[!] Error: Coloring libraries not installed, no coloring will be used") - G = Y = B = R = W = G = Y = B = R = W = '' - - -else: - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - - -def banner(): - print("""%s - -_____ ________________ _____ _____ -\ \ / /\ \ _____\ \ / |_ - \ | | / \ /\ \ / / \ | / \\ - \ \ / / | \_\ | | | /___/|| /\ \\ - \ | / | ___/ ____\ \ | ||| | | \\ - / | \ | \ ____ / /\ \|___|/| \/ \\ - / /|\ \ / /\ \/ \| |/ \ \ |\ /\ \\ - |____|/ \|____| /_____/ |\______||\____\ /____/| | \_____\ \_____\\ - | | | | | | | | || | || | | | | | | | - |____| |____| |_____|/ \|_____| \|___||____|/ \|_____|\|_____| - %s%s -[ Version : 0.2 ]\033[92m -[ Author : X-Vector ]\033[96m -[ Github : github.com/X-Vector ]\033[93m -[ Twitter : twitter.com/@XVector11 ]\033[95m -[ Facebook: facebook.com/X.Vector1 ]\033[95m -[ GreeteZ : Karem Ali ]\033[94m - - """ % (R, W,R)) -banner() - - -from Crypto.PublicKey import RSA -from Crypto.Util import asn1 -import binascii -primes = [] -def prime_factors(n): - i = 2 - while i * i <= n: - if n % i: - i += 1 - else: - n //= i - primes.append(i) - if n > 1: - primes.append(n) - return primes - - -c = input(">>> c = ") -n = input(">>> n = ") -e = input(">>> e = ") - -#https://www.alpertron.com.ar/ECM.HTM - -#Example - -#e=65537 -#c=948626122185577940383278469624490186926710623520188126435501983438183336907186255794618530332289368021291789118954846772779855308711280972165698348170051078399678590370050111906031923460860172422708816752545784573579942145045344015318568999211937934532123400243017531300488990627956390410869348218289952 -#primes = [2162771687,2180377501,2181902579,2183410837,2234829049,2259158687,2366491411,2494545509,2528730847,2591025083,2603976511,2691605771,2714412037,2808388853,2847171653,2870886637,2890555183,2939087189,3000625669,3175105811,3226441579,3265841311,3499273711,3544821197,3611944027,3677851391,3692380933,3696854989,4067996287,4133178029,4212919157,4224110131] - -os.system(clear) -banner() - -print("[!!] Ok , Now :\n1 - You Can Factorize (n) Auto With X-RSA \t[ it Take Much Time ]\n2 - You Can Factorize (n) in This Site https://www.alpertron.com.ar/ECM.HTM \t [ Recommend ]\n") -check = int(input(">>> ")) -if check == 1: - slowprint("\n[+] Please Wait ... \033[95m") - slowprint("\n[+] it Take Much Time ... \033[95m\n") - prime_factors(n) -elif check == 2: - slowprint("\n[+] How Many Prime Number = \033[95m\n") - count = int(input(">>> ")) - print "\n" - for i in range(count): - print "Enter The",(i+1),"Number" - i = int(input(">>> ")) - primes.append(i) - -phi = 1 -for prime in primes: - phi *= (prime-1) - - -def egcd(a, b): - if a == 0: - return (b, 0, 1) - g, y, x = egcd(b%a,a) - return (g, x - (b//a) * y, y) - -def modinv(a, m): - g, x, y = egcd(a, m) - if g != 1: - raise Exception('No modular inverse') - return x%m -d = modinv(e, phi) -m = pow(c, d, n) -def hex_pair(x): - return ('0' * (len(x) % 2)) + x -m_hex = '{:x}'.format(m) -m_hex = hex_pair(m_hex) -msg = binascii.unhexlify(m_hex) -slowprint("\n[+] The PlainText = ") -print(msg.decode(errors="ignore")) diff --git a/RSA_wiener.py b/RSA_wiener.py deleted file mode 100644 index 0d526ce..0000000 --- a/RSA_wiener.py +++ /dev/null @@ -1,137 +0,0 @@ -import sys -import platform,os -from urllib2 import * -from platform import system - -def slowprint(s): - for c in s : - sys.stdout.write(c) - sys.stdout.flush() - time.sleep(4. / 100) - -clear = "" -if "Windows" in platform.system(): - clear = "cls" -if "Linux" in platform.system(): - clear = "clear" -os.system(clear) -is_windows = sys.platform.startswith('win') - -# Console Colors -if is_windows: - # Windows deserves coloring too :D - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - try: - import win_unicode_console , colorama - win_unicode_console.enable() - colorama.init() - #Now the unicode will work ^_^ - except: - print("[!] Error: Coloring libraries not installed, no coloring will be used") - G = Y = B = R = W = G = Y = B = R = W = '' - - -else: - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - - -def banner(): - print("""%s - -_____ ________________ _____ _____ -\ \ / /\ \ _____\ \ / |_ - \ | | / \ /\ \ / / \ | / \\ - \ \ / / | \_\ | | | /___/|| /\ \\ - \ | / | ___/ ____\ \ | ||| | | \\ - / | \ | \ ____ / /\ \|___|/| \/ \\ - / /|\ \ / /\ \/ \| |/ \ \ |\ /\ \\ - |____|/ \|____| /_____/ |\______||\____\ /____/| | \_____\ \_____\\ - | | | | | | | | || | || | | | | | | | - |____| |____| |_____|/ \|_____| \|___||____|/ \|_____|\|_____| - %s%s -[ Version : 0.2 ]\033[92m -[ Author : X-Vector ]\033[96m -[ Github : github.com/X-Vector ]\033[93m -[ Twitter : twitter.com/@XVector11 ]\033[95m -[ Facebook: facebook.com/X.Vector1 ]\033[95m -[ GreeteZ : Karem Ali ]\033[94m - - """ % (R, W,R)) -banner() - -#Example : -""" -n = 744818955050534464823866087257532356968231824820271085207879949998948199709147121321290553099733152323288251591199926821010868081248668951049658913424473469563234265317502534369961636698778949885321284313747952124526309774208636874553139856631170172521493735303157992414728027248540362231668996541750186125327789044965306612074232604373780686285181122911537441192943073310204209086616936360770367059427862743272542535703406418700365566693954029683680217414854103 -e = 57595780582988797422250554495450258341283036312290233089677435648298040662780680840440367886540630330262961400339569961467848933132138886193931053170732881768402173651699826215256813839287157821765771634896183026173084615451076310999329120859080878365701402596570941770905755711526708704996817430012923885310126572767854017353205940605301573014555030099067727738540219598443066483590687404131524809345134371422575152698769519371943813733026109708642159828957941 -c = 305357304207903396563769252433798942116307601421155386799392591523875547772911646596463903009990423488430360340024642675941752455429625701977714941340413671092668556558724798890298527900305625979817567613711275466463556061436226589272364057532769439646178423063839292884115912035826709340674104581566501467826782079168130132642114128193813051474106526430253192254354664739229317787919578462780984845602892238745777946945435746719940312122109575086522598667077632 - -""" - -n = input(">>> n = ") -e = input(">>> e = ") -c = input(">>> c = ") - -slowprint("\n[+] Please Wait ... \033[95m\n") - -import random - -def division_euclidienne(a, b): - return (a // b, a % b) -def fraction_continue(n, d): - developpement = [] - a = n - b = d - while b != 0: - (q,r) = division_euclidienne(a,b) - developpement.append(q) - a = b - b = r - return (developpement) -def reduites_fraction_continue(a): - l=len(a) - reduites=[] - h0 = 1 - h1 = 0 - k0 = 0 - k1 = 1 - count = 0 - while count < l: - h = a[count] * h1 + h0 - h0 = h1 - h1 = h - k = a[count] * k1 + k0 - k0 = k1 - k1 = k - reduites.append((k,h)) - count += 1 - return (reduites) -def wiener(n, e): - fc = fraction_continue(e, n) - reduites = reduites_fraction_continue(fc) - message_clair = random.randint(10**1,10**5) - message_chiffre = pow(message_clair, e, n) - l = len(reduites) - i = 0 - while i < l and pow(message_chiffre, reduites[i][1], n) != message_clair: - i += 1 - if i != l: - slowprint("[+] Getting d ... \033[95m\n") - return (reduites[i][1]) - else: - print("[-] Sorry it's Not Wiener Attack\n") - exit(0) - -d = wiener(n,e) -slowprint("[+] Getting PlainText ... \033[95m\n") -decode = pow(c,d,n) -output = (hex(decode)[2:].replace('L','')).decode("hex") -slowprint("[+] The PlainText = \033[95m") -print output diff --git a/RSA2.py b/banner.py similarity index 62% rename from RSA2.py rename to banner.py index b82d75c..3468f20 100644 --- a/RSA2.py +++ b/banner.py @@ -1,10 +1,12 @@ -# p , q , e , c is required +# author : X-Vector +# Tested on Kali Linux / Parrot Os / Ubuntu +# Simple script for RSA Attack import sys import platform,os -from urllib2 import * from platform import system +import time def slowprint(s): - for c in s : + for c in s + '\n': sys.stdout.write(c) sys.stdout.flush() time.sleep(4. / 100) @@ -17,14 +19,14 @@ def slowprint(s): os.system(clear) is_windows = sys.platform.startswith('win') +# Windows deserves coloring too :D +G = '\033[92m' # green +Y = '\033[93m' # yellow +B = '\033[94m' # blue +R = '\033[91m' # red +W = '\033[0m' # white # Console Colors if is_windows: - # Windows deserves coloring too :D - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white try: import win_unicode_console , colorama win_unicode_console.enable() @@ -35,17 +37,8 @@ def slowprint(s): G = Y = B = R = W = G = Y = B = R = W = '' -else: - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - - def banner(): print("""%s - _____ ________________ _____ _____ \ \ / /\ \ _____\ \ / |_ \ | | / \ /\ \ / / \ | / \\ @@ -57,37 +50,10 @@ def banner(): | | | | | | | | || | || | | | | | | | |____| |____| |_____|/ \|_____| \|___||____|/ \|_____|\|_____| %s%s -[ Version : 0.2 ]\033[92m +[ Version : 0.4 ]\033[92m [ Author : X-Vector ]\033[96m [ Github : github.com/X-Vector ]\033[93m [ Twitter : twitter.com/@XVector11 ]\033[95m [ Facebook: facebook.com/X.Vector1 ]\033[95m [ GreeteZ : Karem Ali ]\033[94m - """ % (R, W,R)) -banner() - -import gmpy - - -#c = 29846947519214575162497413725060412546119233216851184246267357770082463030225 -#p = 238324208831434331628131715304428889871 -#q = 296805874594538235115008173244022912163 -#e = 3 - -c = input(">>> c = ") -p = input(">>> p = ") -q = input(">>> q = ") -e = input(">>> e = ") -slowprint("\n[+] Please Wait ... \033[95m\n") -try: - n = p*q - x = (p-1)*(q-1) - d = gmpy.invert(e,x) - m = hex(pow(c,d,n))[2:] - decode = m.decode('hex') - slowprint("[+] The PlainText = ") - print(decode) - -except: - slowprint("[-] False Attack !! ") diff --git a/public.py b/public.py deleted file mode 100644 index b51e2d7..0000000 --- a/public.py +++ /dev/null @@ -1,152 +0,0 @@ -import sys -import platform,os -from urllib2 import * -from platform import system -def slowprint(s): - for c in s : - sys.stdout.write(c) - sys.stdout.flush() - time.sleep(4. / 100) - -clear = "" -if "Windows" in platform.system(): - clear = "cls" -if "Linux" in platform.system(): - clear = "clear" -os.system(clear) -is_windows = sys.platform.startswith('win') - -# Console Colors -if is_windows: - # Windows deserves coloring too :D - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - try: - import win_unicode_console , colorama - win_unicode_console.enable() - colorama.init() - #Now the unicode will work ^_^ - except: - print("[!] Error: Coloring libraries not installed, no coloring will be used") - G = Y = B = R = W = G = Y = B = R = W = '' - - -else: - G = '\033[92m' # green - Y = '\033[93m' # yellow - B = '\033[94m' # blue - R = '\033[91m' # red - W = '\033[0m' # white - - -def banner(): - print("""%s - -_____ ________________ _____ _____ -\ \ / /\ \ _____\ \ / |_ - \ | | / \ /\ \ / / \ | / \\ - \ \ / / | \_\ | | | /___/|| /\ \\ - \ | / | ___/ ____\ \ | ||| | | \\ - / | \ | \ ____ / /\ \|___|/| \/ \\ - / /|\ \ / /\ \/ \| |/ \ \ |\ /\ \\ - |____|/ \|____| /_____/ |\______||\____\ /____/| | \_____\ \_____\\ - | | | | | | | | || | || | | | | | | | - |____| |____| |_____|/ \|_____| \|___||____|/ \|_____|\|_____| - %s%s -[ Version : 0.1 ]\033[92m -[ Author : X-Vector ]\033[96m -[ Github : github.com/X-Vector ]\033[93m -[ Twitter : twitter.com/@XVector11 ]\033[95m -[ Facebook: facebook.com/X.Vector1 ]\033[95m -[ GreeteZ : Karem Ali ]\033[94m - - """ % (R, W,R)) -banner() - -import argparse -from Crypto.PublicKey import RSA - -k = raw_input("Enter Path of Public Key : ") -try: - key=RSA.importKey(open(k, 'r').read()) - slowprint("[+] Please Wait ... ") - n = key.n - e = key.e - - from CryptoLib.RSA import RSA - import binascii - - - factordb = RSA.attacks.factordb(n) - q = factordb[0] - p = factordb[1] - - slowprint("[+] Getting n,p,q,d,e ... ") - - - print("\n[+] n = {}".format(str(n))) - print("\n[+] e = {}".format(str(e))) - print("\n[+] p = {}".format(str(p))) - print("\n[+] q = {}".format(str(q))) - - import random - import Crypto.PublicKey.RSA - class Wiener(object): - def accueil(self): - slowprint("") - - def division_euclidienne(self, a, b): - return (a // b, a % b) - def fraction_continue(self, n, d): - developpement = [] - a = n - b = d - while b != 0: - (q,r) = self.division_euclidienne(a,b) - developpement.append(q) - a = b - b = r - return (developpement) - def reduites_fraction_continue(self, a): - l=len(a) - reduites=[] - h0 = 1 - h1 = 0 - k0 = 0 - k1 = 1 - count = 0 - while count < l: - h = a[count] * h1 + h0 - h0 = h1 - h1 = h - k = a[count] * k1 + k0 - k0 = k1 - k1 = k - reduites.append((k,h)) - count += 1 - return (reduites) - def wiener(self, n, e): - fc = self.fraction_continue(e, n) - reduites = self.reduites_fraction_continue(fc) - message_clair = random.randint(10**1,10**5) - message_chiffre = pow(message_clair, e, n) - l = len(reduites) - i = 0 - while i < l and pow(message_chiffre, reduites[i][1], n) != message_clair: - i += 1 - if i != l: - return (reduites[i][1]) - else: - print("\t[-] This RSA public key isn't a valid candidate to a Wiener Attack\n") - exit(0) - def __init__(self, n, e): - self.accueil() - self.d = self.wiener(n, e) - print ("[+] d = {}".format(self.d)) - Wiener(n,e) - slowprint("\n[+] Thanx For Using X-RSA Tool <3 \033[95m\n") -except: - slowprint("[-] False Attack !! ") diff --git a/requirement.txt b/requirement.txt index eb39d22..26ce7c1 100644 --- a/requirement.txt +++ b/requirement.txt @@ -1,6 +1,6 @@ Crypto gmpy2 gmpy -PyCrypto SymPy requests +future diff --git a/utilis.py b/utilis.py new file mode 100644 index 0000000..878715d --- /dev/null +++ b/utilis.py @@ -0,0 +1,93 @@ +import binascii +import gmpy2 + + + +def Convert(decimal): + hex_ = hex(decimal).replace("0x","").replace("L","") + ascii = binascii.a2b_hex(hex_) + print("\nPlainText in Decimal :",decimal) + print("PlainText in hex :",hex_) + print("PlainText in ascii :",ascii.decode("utf-8")) + +def egcd(b, n): + (x0, x1, y0, y1) = (1, 0, 0, 1) + while n != 0: + (q, b, n) = (b // n, n, b % n) + (x0, x1) = (x1, x0 - q * x1) + (y0, y1) = (y1, y0 - q * y1) + return (b, x0, y0) +def modinv(a,m): + g, x, y = egcd(a, m) + if g != 1: + raise Exception('modular inverse does not exist') + else: + return x % m + + +def mul_inv(a, b): + b0 = b + x0, x1 = 0, 1 + if b == 1: return 1 + while a > 1: + q = a // b + a, b = b, a%b + x0, x1 = x1 - q * x0, x0 + if x1 < 0: x1 += b0 + return x1 + +def inv_pow(c, e): + low = -1 + high = c+1 + while low + 1 < high: + m = (low + high) // 2 + p = pow(m, e) + if p < c: + low = m + else: + high = m + m = high + assert pow(m, e) == c + return m +def division_euclidienne(a, b): + return (a // b, a % b) +def fraction_continue(n, d): + developpement = [] + a = n + b = d + while b != 0: + (q,r) = division_euclidienne(a,b) + developpement.append(q) + a = b + b = r + return (developpement) +def reduites_fraction_continue(a): + l=len(a) + reduites=[] + h0 = 1 + h1 = 0 + k0 = 0 + k1 = 1 + for count in range(l): + h = a[count] * h1 + h0 + h0 = h1 + h1 = h + k = a[count] * k1 + k0 + k0 = k1 + k1 = k + reduites.append((k,h)) + return (reduites) + +def floorSqrt(n): + x = n + y = (x + 1) // 2 + while y < x: + x = y + y = (x + n // x) // 2 + return x + + + + + +