|
8 | 8 | __author__ = "Lukas Zorn" |
9 | 9 | __copyright__ = "Copyright 2021 Lukas Zorn" |
10 | 10 | __license__ = "GNU GPLv3" |
11 | | -__version__ = "0.2.1" |
12 | | -__maintainer__ = "Lukas Zorn" |
13 | | -__status__ = "Development" |
14 | 11 |
|
15 | 12 |
|
16 | 13 | # RSA keypair generation |
@@ -138,37 +135,43 @@ def decryption(private_key, c): |
138 | 135 |
|
139 | 136 |
|
140 | 137 | # RSA Brute-force |
141 | | -def brute_force_by_key(any_key): |
| 138 | +def brute_force_by_key(any_key, p_n=None): |
142 | 139 | print(tabulate([['RSA Brute-Force Angriff (schlüsselbasiert)']], tablefmt='fancy_grid')) |
143 | 140 |
|
144 | 141 | # Unpack the key into its components |
145 | 142 | a, n = any_key |
146 | 143 |
|
147 | | - # Choose multiple integers x such that 0 ≤ x < n |
148 | | - x, y, z = random.sample(range(n), 3) |
| 144 | + # Test for half of all possible plaintexts or 5 |
| 145 | + if p_n is None: |
| 146 | + p_n = n // 2 if n < 50 else 5 |
| 147 | + |
| 148 | + # Choose an integer p_n such that 1 ≤ p_n < n |
| 149 | + if p_n not in range(1, n): |
| 150 | + print(f'Für die Variable p_n = {p_n} muss gelten 1 ≤ {p_n} < {n}.') |
| 151 | + return -1 |
| 152 | + |
| 153 | + # Choose p_n plaintexts p such that 0 ≤ p < n |
| 154 | + p = random.sample(range(n), p_n) |
149 | 155 |
|
150 | 156 | # Encryption |
151 | | - x_c = (x ** a) % n |
152 | | - y_c = (y ** a) % n |
153 | | - z_c = (z ** a) % n |
| 157 | + c = [(x ** a) % n for x in p] |
154 | 158 |
|
155 | 159 | # Brute-force |
156 | 160 | b = [] |
157 | | - for v in range(n): |
158 | | - if x != (x_c ** v) % n: |
159 | | - continue |
160 | | - if y != (y_c ** v) % n: |
161 | | - continue |
162 | | - if z != (z_c ** v) % n: |
163 | | - continue |
164 | | - b.append(v) |
| 161 | + for (p_x, c_x) in zip(p, c): |
| 162 | + b_x = [] |
| 163 | + for n_x in range(n): |
| 164 | + if p_x == (c_x ** n_x) % n: |
| 165 | + b_x.append(n_x) |
| 166 | + b.append(b_x) |
| 167 | + b = sorted(set(b[0]).intersection(*b)) |
165 | 168 |
|
166 | 169 | # Calculation path output |
167 | 170 | if len(b) < 1: |
168 | 171 | print( |
169 | 172 | f'Das Gegenstück für den Schlüssel K = {{{a}, {n}}} konnte nicht ermittelt werden.', end='\n\n') |
170 | 173 | return -1 |
171 | 174 | print( |
172 | | - f'Mögliche Gegenstücke für den Schlüssel K = {{{a}, {n}}} mit den Testwerten x = {x}, y = {y} und z = {z} sind:') |
| 175 | + f'Mögliche Gegenstücke für den Schlüssel K = {{{a}, {n}}} sind:') |
173 | 176 | print(tabulate(zip(*(b, [n] * len(b))), headers=['b', 'n'], tablefmt='pretty'), end='\n\n') |
174 | 177 | return b[0], n |
0 commit comments