-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbrutalForce.py
More file actions
49 lines (41 loc) · 1.27 KB
/
brutalForce.py
File metadata and controls
49 lines (41 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from random import randrange
from totiente import totiente
from primalityFermat import primalityFermat
from primalityMillerRabin import primalityMillerRabin
from genericsFunctions import MDC
from genericsFunctions import MDCEst
from genericsFunctions import testMod
from cipher import cipherMsg
import math
def brutalForce(nBits):
cMsgTpDeciph = ""
aInfos = cipherMsg(True,nBits)# Busca a mensagem criptografada
nValueN = aInfos[0]
nValueE = aInfos[1]
aLetMsg = aInfos[2]
# Fatorar valor de nValueN com Teste de Primalidade Até a Raiz de N
nP = 1
nQ = 1
nSqrt = math.sqrt(nValueN)
nCnt = 3
while nCnt <= nSqrt:
# Caso valor encontrando seja um divisor exato, encontrou o primeiro valor
if nValueN % nCnt == 0:
nP = ( nValueN // nCnt )
nQ = nCnt
break
nCnt += 2
#Salva os valores para calcular o Phi(N)
nPhiP = nP - 1
nPhiQ = nQ - 1
#seta o valor pego na força bruta
nValuePhi = nPhiP * nPhiQ
nValueD = MDCEst( nValueE, nValuePhi)
#busca o valor congruente caso D seja negativo
if nValueD < 0:
nValueD = nValueD % nValuePhi
# Descriptografa a mensagem
for nMsg in aLetMsg:
nValueCry = testMod(nMsg,nValueD,nValueN)# Executa a Potência Modular
cMsgTpDeciph += chr(nValueCry)# Converte de ASCII
print( "===== Decipher: {} =====".format(cMsgTpDeciph))