1
1
import random
2
2
import sys
3
3
from sympy import isprime , mod_inverse
4
- from typing import Tuple , List
4
+ from typing import List , Tuple
5
+
5
6
6
7
def generate_prime_candidate (length : int ) -> int :
7
8
"""
@@ -50,8 +51,8 @@ def generate_keys(keysize: int) -> Tuple[Tuple[int, int], Tuple[int, int]]:
50
51
except TypeError as ex :
51
52
print (f"Type error generating keys: { ex } " , file = sys .stderr )
52
53
sys .exit (1 )
53
- except Exception as ex :
54
- print (f"Unexpected error generating keys: { ex } " , file = sys .stderr )
54
+ except ArithmeticError as ex :
55
+ print (f"Arithmetic error generating keys: { ex } " , file = sys .stderr )
55
56
sys .exit (1 )
56
57
57
58
def gcd (a : int , b : int ) -> int :
@@ -81,8 +82,11 @@ def encrypt(pk: Tuple[int, int], plaintext: str) -> List[int]:
81
82
except TypeError as ex :
82
83
print (f"Type error during encryption: { ex } " , file = sys .stderr )
83
84
return None
84
- except Exception as ex :
85
- print (f"Unexpected error during encryption: { ex } " , file = sys .stderr )
85
+ except ValueError as ex :
86
+ print (f"Value error during encryption: { ex } " , file = sys .stderr )
87
+ return None
88
+ except OverflowError as ex :
89
+ print (f"Overflow error during encryption: { ex } " , file = sys .stderr )
86
90
return None
87
91
88
92
def decrypt (pk : Tuple [int , int ], ciphertext : List [int ]) -> str :
@@ -102,8 +106,11 @@ def decrypt(pk: Tuple[int, int], ciphertext: List[int]) -> str:
102
106
except TypeError as ex :
103
107
print (f"Type error during decryption: { ex } " , file = sys .stderr )
104
108
return None
105
- except Exception as ex :
106
- print (f"Unexpected error during decryption: { ex } " , file = sys .stderr )
109
+ except ValueError as ex :
110
+ print (f"Value error during decryption: { ex } " , file = sys .stderr )
111
+ return None
112
+ except OverflowError as ex :
113
+ print (f"Overflow error during decryption: { ex } " , file = sys .stderr )
107
114
return None
108
115
109
116
if __name__ == '__main__' :
@@ -134,6 +141,6 @@ def decrypt(pk: Tuple[int, int], ciphertext: List[int]) -> str:
134
141
except TypeError as ex :
135
142
print (f"Type error: { ex } " , file = sys .stderr )
136
143
sys .exit (1 )
137
- except Exception as ex :
138
- print (f"Unexpected error: { ex } " , file = sys .stderr )
144
+ except ArithmeticError as ex :
145
+ print (f"Arithmetic error: { ex } " , file = sys .stderr )
139
146
sys .exit (1 )
0 commit comments