fix: Подкорректировал работу с исключениями

nihonium
Nihonium 1 month ago
parent 893bf71528
commit d66419d906
Signed by: nihonium
GPG Key ID: 0251623741027CFC

@ -1,5 +1,3 @@
#pyinstaller -F -i "icon.ico" McEliece_console_v2.py
import hashlib import hashlib
import getpass import getpass
@ -9,8 +7,8 @@ def main():
def safe_start(): def safe_start():
try: try:
start_menu() start_menu()
except: except (KeyboardInterrupt, EOFError):
print("\nUnknown error (maybe ctrl+c), emergency exit!") print("\nExiting...")
def start_menu(): def start_menu():
f = True f = True
@ -29,7 +27,7 @@ def start_menu():
input() input()
def menu(): def menu():
import cryptosystem_core_v2 as ME_core import cryptosystem_core as ME_core
core = ME_core.McEliece_core() core = ME_core.McEliece_core()
global_info = "All files are interpreted as raw bytes and must be located in the directory with this executable file.\nDefault filenames with .bin extension: pubkey, privkey_S, privkey_p, plaintext, ciphertext, ciphered_string.\nDon't forget to import (or generate) keys before encryption/decryption and after changing config!\nYou can restore any one key from two another (don't forget to import before).\n" global_info = "All files are interpreted as raw bytes and must be located in the directory with this executable file.\nDefault filenames with .bin extension: pubkey, privkey_S, privkey_p, plaintext, ciphertext, ciphered_string.\nDon't forget to import (or generate) keys before encryption/decryption and after changing config!\nYou can restore any one key from two another (don't forget to import before).\n"
info = "Menu numbers: 0 = exit, s = print short info, h = print this info, g = print global info, c = change config;\n1 = generate keys, 10 = unsafe generate keys (seed = hash(password)),\n11 = export pubkey, 12 = export privkey_S, 13 = export privkey_p,\n14 = import pubkey, 15 = import privkey_S, 16 = import privkey_p,\n17 = restore pubkey, 18 = restore privkey_S, 19 = restore privkey_p;\n2 = encrypt,\n21 = encrypt non-default filename, 22 = encrypt string from keyboard, 23 = encrypt hided string.\n3 = decrypt,\n31 = decrypt non-default filename, 32 = decrypt string and show on screen\n" info = "Menu numbers: 0 = exit, s = print short info, h = print this info, g = print global info, c = change config;\n1 = generate keys, 10 = unsafe generate keys (seed = hash(password)),\n11 = export pubkey, 12 = export privkey_S, 13 = export privkey_p,\n14 = import pubkey, 15 = import privkey_S, 16 = import privkey_p,\n17 = restore pubkey, 18 = restore privkey_S, 19 = restore privkey_p;\n2 = encrypt,\n21 = encrypt non-default filename, 22 = encrypt string from keyboard, 23 = encrypt hided string.\n3 = decrypt,\n31 = decrypt non-default filename, 32 = decrypt string and show on screen\n"

@ -30,7 +30,7 @@ class McEliece_core:
raise Exception() raise Exception()
rs = galois.ReedSolomon(n, k, field = self._GF) rs = galois.ReedSolomon(n, k, field = self._GF)
except: except:
raise Exception() raise
else: else:
self._n = n self._n = n
self._k = k self._k = k
@ -65,7 +65,7 @@ class McEliece_core:
G = [G[i - self._n : i] for i in range(self._n, self._n * self._k + self._n, self._n)] G = [G[i - self._n : i] for i in range(self._n, self._n * self._k + self._n, self._n)]
G = self._GF(G) G = self._GF(G)
except: except:
raise Exception() raise
else: else:
self._G = G self._G = G
def set_privkey_S(self, S): def set_privkey_S(self, S):
@ -74,7 +74,7 @@ class McEliece_core:
S = self._GF(S) S = self._GF(S)
S_inv = np.linalg.inv(S) S_inv = np.linalg.inv(S)
except: except:
raise Exception() raise
else: else:
self._S = S self._S = S
self._S_inv = S_inv self._S_inv = S_inv
@ -96,7 +96,7 @@ class McEliece_core:
try: try:
S_inv = np.linalg.inv(S) S_inv = np.linalg.inv(S)
except: except:
raise Exception() raise
self._S = S self._S = S
self._S_inv = S_inv self._S_inv = S_inv
def restore_privkey_p(self): def restore_privkey_p(self):
@ -134,14 +134,14 @@ class McEliece_core:
out += self._encrypt_one(text) out += self._encrypt_one(text)
return out return out
except: except:
raise Exception() raise
def decrypt(self, msg): def decrypt(self, msg):
try: try:
msg = [msg[i - self._n : i] for i in range(self._n, len(msg) + self._n, self._n)] msg = [msg[i - self._n : i] for i in range(self._n, len(msg) + self._n, self._n)]
msg = [self._decrypt_one(self._GF(i)) for i in msg] msg = [self._decrypt_one(self._GF(i)) for i in msg]
return [i for j in msg for i in j] return [i for j in msg for i in j]
except: except:
raise Exception() raise
#End of top-level functions, please do NOT use functions below without understanding! #End of top-level functions, please do NOT use functions below without understanding!
def _generate_S(self): def _generate_S(self):
S = self._GF.Random((self._k, self._k)) S = self._GF.Random((self._k, self._k))
@ -199,7 +199,7 @@ class McEliece_core:
try: try:
msg = self._unpad_message(msg) msg = self._unpad_message(msg)
except: except:
raise Exception() raise
return msg return msg
def _pad_message(self, msg): def _pad_message(self, msg):
last_value = self._k - (len(msg) % self._k) last_value = self._k - (len(msg) % self._k)

Loading…
Cancel
Save