MCPcopy Create free account
hub / github.com/Apress/practical-cryptography-in-python / main

Function main

src/rsa_oracle_attack.py:164–223  ·  view source on GitHub ↗
(args)

Source from the content-addressed store, hash-verified

162 return m
163
164def main(args):
165 key_size, messagecount = [int(arg) for arg in args]
166 print("Running {} tests with key size {}".format(messagecount, key_size))
167
168 private_key = rsa.generate_private_key(
169 public_exponent=65537,
170 key_size=key_size,
171 backend=default_backend()
172 )
173 public_key = private_key.public_key()
174
175 oracle = FakeOracle(private_key)
176 attack_program = RSAOracleAttacker(public_key, oracle)
177
178 for i in range(messagecount):
179 message = b'test %d' % (i)
180
181 ###
182 # WARNING: PKCS #1 v1.5 is obsolete and has vulnerabilities
183 # DO NOT USE EXCEPT WITH LEGACY PROTOCOLS
184 ciphertext = public_key.encrypt(
185 message,
186 padding.PKCS1v15()
187 )
188 ciphertext_as_int = bytes_to_int(ciphertext)
189
190 print("\nWe're starting our attack run on message {}.".format(i))
191
192 recovered_as_int = attack_program.attack(ciphertext_as_int)
193 if int_to_bytes(recovered_as_int).endswith(message):
194 print("[PASS]")
195 else:
196 print(int_to_bytes(recovered_as_int))
197 print("[FAIL]")
198 return
199 print("\tRecovered: ", int_to_bytes(recovered_as_int))
200
201 i_total = 0
202 search_total = 0
203 runtime_total = 0.0
204 print( "\nSTATISTICS")
205 print( "-------------------------")
206 print( "{:6} {:10} {:10} {:10}".format(
207 "Test", "Iterations", "Searches", "Runtime"))
208 print( "-------------------------")
209 for test_index in range(messagecount):
210 test_stat = attack_program.stats[test_index]
211 print("{:4} {:10} {:10} {:10}".format(
212 test_index,
213 test_stat.i, test_stat.search_count, test_stat.runtime))
214 i_total += test_stat.i
215 search_total += test_stat.search_count
216 runtime_total += test_stat.runtime
217 i_avg = i_total/messagecount
218 search_avg = search_total/messagecount
219 runtime_avg = runtime_total/messagecount
220 print("\n")
221 print( "AVERAGE:")

Callers 1

Calls 5

attackMethod · 0.95
FakeOracleClass · 0.85
RSAOracleAttackerClass · 0.85
bytes_to_intFunction · 0.70
int_to_bytesFunction · 0.70

Tested by

no test coverage detected