Compare commits

..

No commits in common. 'd66419d906fda70a8f14519612b08c46ca72094d' and 'c054da67f1850a25ca2cdcfcf36a409f2d3588c4' have entirely different histories.

@ -1,3 +1,5 @@
#pyinstaller -F -i "icon.ico" McEliece_console_v2.py
import hashlib import hashlib
import getpass import getpass
@ -7,8 +9,8 @@ def main():
def safe_start(): def safe_start():
try: try:
start_menu() start_menu()
except (KeyboardInterrupt, EOFError): except:
print("\nExiting...") print("\nUnknown error (maybe ctrl+c), emergency exit!")
def start_menu(): def start_menu():
f = True f = True
@ -27,7 +29,7 @@ def start_menu():
input() input()
def menu(): def menu():
import cryptosystem_core as ME_core import cryptosystem_core_v2 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"

@ -1,50 +0,0 @@
from cryptosystem_core import *
def main(): #comment "return" for testing
core = McEliece_core()
n, k = core.get_config()
print(n, k)
core.change_config(5, 3) #check comments in class implementation to understand possible values
n, k = core.get_config()
print(n, k)
print()
core.generate_keys() #true random
print(core.get_pubkey())
print(core.get_privkey_S())
print(core.get_privkey_p())
print()
core.generate_keys(sum([ord(i) for i in list("password")])) #very simple seed
G = core.get_pubkey()
S = core.get_privkey_S()
p = core.get_privkey_p()
print(G)
print(S)
print(p)
print()
core.change_config(5, 3) #unset all keys inside core
core.set_privkey_S(S)
core.set_privkey_p(p)
core.restore_pubkey()
print(core.get_pubkey() == G)
core.change_config(5, 3) #unset all keys inside core
core.set_pubkey(G)
core.set_privkey_p(p)
core.restore_privkey_S()
print(core.get_privkey_S() == S)
core.change_config(5, 3) #unset all keys inside core
core.set_pubkey(G)
core.set_privkey_S(S)
core.restore_privkey_p()
print(core.get_privkey_p() == p)
print()
for j in range(2):
text = [i + 1 for i in range(5)]
msg = core.encrypt(text)
print(msg)
text = core.decrypt(msg)
print(text)
print()
print("All tests are finished!")
if __name__ == "__main__":
main()

@ -11,6 +11,56 @@ import numpy as np
import galois import galois
import random import random
#import cryptosystem_core_v2 as ME_core
#core = ME_core.McEliece_core()
def main(): #comment "return" for testing
return
core = McEliece_core()
n, k = core.get_config()
print(n, k)
core.change_config(5, 3) #check comments in class implementation to understand possible values
n, k = core.get_config()
print(n, k)
print()
core.generate_keys() #true random
print(core.get_pubkey())
print(core.get_privkey_S())
print(core.get_privkey_p())
print()
core.generate_keys(sum([ord(i) for i in list("password")])) #very simple seed
G = core.get_pubkey()
S = core.get_privkey_S()
p = core.get_privkey_p()
print(G)
print(S)
print(p)
print()
core.change_config(5, 3) #unset all keys inside core
core.set_privkey_S(S)
core.set_privkey_p(p)
core.restore_pubkey()
print(core.get_pubkey() == G)
core.change_config(5, 3) #unset all keys inside core
core.set_pubkey(G)
core.set_privkey_p(p)
core.restore_privkey_S()
print(core.get_privkey_S() == S)
core.change_config(5, 3) #unset all keys inside core
core.set_pubkey(G)
core.set_privkey_S(S)
core.restore_privkey_p()
print(core.get_privkey_p() == p)
print()
for j in range(2):
text = [i + 1 for i in range(5)]
msg = core.encrypt(text)
print(msg)
text = core.decrypt(msg)
print(text)
print()
print("All tests are finished!")
class McEliece_core: class McEliece_core:
def __init__(self): def __init__(self):
self._order = 256 #p^m = 2**8; encryption of each byte self._order = 256 #p^m = 2**8; encryption of each byte
@ -30,7 +80,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 raise Exception()
else: else:
self._n = n self._n = n
self._k = k self._k = k
@ -65,7 +115,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 raise Exception()
else: else:
self._G = G self._G = G
def set_privkey_S(self, S): def set_privkey_S(self, S):
@ -74,7 +124,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 raise Exception()
else: else:
self._S = S self._S = S
self._S_inv = S_inv self._S_inv = S_inv
@ -96,7 +146,7 @@ class McEliece_core:
try: try:
S_inv = np.linalg.inv(S) S_inv = np.linalg.inv(S)
except: except:
raise raise Exception()
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 +184,14 @@ class McEliece_core:
out += self._encrypt_one(text) out += self._encrypt_one(text)
return out return out
except: except:
raise raise Exception()
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 raise Exception()
#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 +249,7 @@ class McEliece_core:
try: try:
msg = self._unpad_message(msg) msg = self._unpad_message(msg)
except: except:
raise raise Exception()
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)
@ -212,3 +262,6 @@ class McEliece_core:
if msg[-i] != last_value: if msg[-i] != last_value:
raise Exception() raise Exception()
return msg[: -last_value] return msg[: -last_value]
if __name__ == "__main__":
main()

Loading…
Cancel
Save