Python

No description

fork
loading
Files
  • main.py
main.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Quentin Bishop
# 11 Dec 2018

import random
import math

def trial(n, d, r):
  a = random.randint(2, n-1)
  val = pow(a,d,n) -1
  if val%n == 0:
    return True
  for s in range(0, r):
    val = pow(a,d*(2**s), n) + 1
    if val%n == 0:
      return True
  return False

def dr(n):
  r = 0
  n= n-1
  while (n)%2 == 0:
    n = n//2
    r += 1
  d = n
  return(d,r)

def is_prime(n):
  if n%2 == 0:
    return False
  d,r = dr(n)
  for k in range(10):
    if trial(n,d,r) == False:
      return False
  return True

def generate_primes(e):
  p1 = 2**(e-1)+1+random.randint(1,2**(e-6))*2
  while not is_prime(p1):
    p1+=2
  p2 = 2**(e-1)+1+random.randint(1,2**(e-6))*2
  while not is_prime(p2):
    p2+=2
  return p1,p2

def d_solver(e,m):
  greens = []
  greens.append(m//e)
  front = m
  last = front - greens[0]*e
  count = 1
  while last != 1:
    front = (front-last)/greens[count-1]
    greens.append(int(front//last))
    last = front-greens[count]*last
    count+=1
  count = 0
  blue = 1
  red = -greens[-1]
  for x in range(len(greens)-1):
    temp = red
    red = blue+red*-greens[len(greens)-x-2]
    blue = temp
  while red < 0:
    red+=m
  return red

def s2n(alpha ,text):
  count = 0
  num = 0
  for x in text[::-1]:
    num += alpha.index(x)*pow(len(alpha),count)
    count+=1
  return num

def n2s(alpha, num , n):
  chars = []
  count = n
  for x in range(n+1):
    a = num // pow(len(alpha),count)
    b = num % pow(len(alpha),count)
    count -= 1
    chars.append(alpha[a])
    num = b
  return chars
  

def encode(nums, e, m):
  print("Encoded To: ")
  encrypted = []
  for number in nums:
    temp = pow(number, e, m)
    encrypted.append(temp)
    print(temp)
  return encrypted

def decode(encrypted, d, m):
  print("Decoded To: ")
  decrypted = []
  for number in encrypted:
    temp = pow(number, d, m)
    decrypted.append(temp)
    print(temp)
  return decrypted


def run(power, text):
  '''
  p = 12146506381645834038938637694315560145605522481099001678525961130513503330983463286913894837422547560452046476688310536561557005074571465528726771144222297

  q = 6841181471228144056720961233011919562929461148315947269150349289240990263876119140774193696991680295609812474830147505476097737728291793488537259523362993'''

  m = 2494279923802253606287472092668310204875649561581672691813431640440219048972006082731965879

  #e = 65537
  alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ., []0123456789"
  #totm = (p-1)*(q-1)
  

  '''while(math.gcd(e,m) != 1):  
    e+= 2
  d = d_solver(e, totm)'''
  d = 65537
  n = 0
  while(len(alpha)**(n+1) < m-1):
    n+=1
  print(n)
  '''
  nums = []
  for x in range(n-len(text)%n):
    text = text+'Z'
  count = 0
  print(text)
  for x in range(int(len(text)/n)):
    nums.append(s2n(alpha, text[count:count+n]))
    count += n  
  encoded = encode(nums, e, m)
  print(encoded)
  '''

  encoded = [129348710293561219045692384213098475065291837410293856104574368752354612394871029586108276]
  decoded = decode(encoded, d, m)

  master = []
  for v in decoded:
    master.append("".join(n2s(alpha, v, n-1)))
  print("".join(master))

run(512, "RSAENCRYPTIONISNAMEDAFTERRONRIVESTADISHAMIRANDLEONARDADLEMAN")
'''
p = 6878459694922660449573958344093036106826978764663091757522278541171002075825387881261218953549357558125154568021896000484547633061883313833541705375612849 

q = 7017693634471364511662475249766261357470331266442915363257390022599358335108372975983871696584309039899366581212015910922401797423939565355774608027547759 

m = 48270922816026598154405983813556401404019974982778163090941399490970552656182570083633733757248228735008677104809287802044685027005977076591483908293176709641654912262747801602561438391534465854949071877669299431251310886819410334698249356300151998546621506144019003019172020663596910738612128874579241555391 

e = 65537
'''