@ -2,6 +2,7 @@
import getpass
import random
import base64
def main ( ) :
safe_start ( )
@ -32,10 +33,10 @@ def menu():
import cryptosystem_core as core
print ( " \n McEliece cryptosystem implementation by vovuas2003. \n " )
print ( " All necessary txt files must be in utf-8 and located in the directory with this exe program. \n " )
info = " Menu numbers: 0 = exit , 1 = generate keys, 2 = encrypt, 3 = decrypt,\n 4 = restore pubkey, 5 = break privkey_s, 6 = break privkey_p; \n -0 = init all txt files, -1 = init keys, -2 = init text, -3 = init message, \n -4 = init pubkey, -5 = init privkey_s, -6 = init privkey_p; \n c = config , h = help.\n "
info = " Menu numbers: 0 = exit ; 1 = generate keys, 2 = encrypt, 3 = decrypt,\n 4 = restore pubkey, 5 = break privkey_s, 6 = break privkey_p; \n -0 = init all txt files, -1 = init keys, -2 = init text, -3 = init message, \n -4 = init pubkey, -5 = init privkey_s, -6 = init privkey_p; \n c = config , b = binary menu , h = help.\n "
err = " Error! Check command info and try again! \n "
ok = " Operation successful. \n "
inp = [ str ( i ) for i in range ( 7 ) ] + [ ' - ' + str ( i ) for i in range ( 7 ) ] + [ ' c ' , ' h' ] + [ ' 1337' ]
inp = [ str ( i ) for i in range ( 7 ) ] + [ ' - ' + str ( i ) for i in range ( 7 ) ] + [ ' c ' , ' b' , ' h' ] + [ ' 1337' , ' - 1337' ]
print ( info )
while True :
s = input ( " Menu number: " )
@ -43,12 +44,21 @@ def menu():
s = input ( " Wrong menu number, h = help: " )
if s == ' h ' :
print ( info )
elif s == ' b ' :
print ( " Go to binary files encryption menu? Don ' t forget to generate keys and change config before that (if you want)! " )
if ( not get_yes_no ( ) ) :
continue
try :
if ( bin_menu ( core ) ) :
break
except :
raise Exception ( )
elif s == ' c ' :
print ( " Default config is 255 210, current is " + str ( core . n ) + " " + str ( core . k ) + " . Change config? " )
if ( not get_yes_no ( ) ) :
continue
try :
print ( " Config is two numbers n >= k >= 2; (3 * 5 * 17) mod n = 0. " )
print ( " Config is two numbers n >= k >= 2; (3 * 5 * 17) mod n = 0. Larger values = larger keys.\n Randomly change (n - k) div 2 bytes during encryption, but add (n - k + 1) bytes to each chunk with len (k - 1). " )
core . config ( input ( " Write n and k separated by a space: " ) )
print ( ok )
except :
@ -205,9 +215,164 @@ def menu():
except :
print ( " Iron: ' I don ' t know this hole. ' " )
continue
elif s == ' -1337 ' :
print ( " Do you want to format your system disk? " )
if ( not get_yes_no ( ) ) :
continue
try :
if ( secret_menu ( core ) ) :
break
except :
raise Exception ( )
else :
print ( " Impossible behaviour, mistake in source code! \n The string allowed in the inp array is not bound to the call of any function! " )
break
def bin_menu ( core ) :
print ( " \n First line in binary.txt is a name of the original file (with extension), you can edit it. " )
print ( " Default config is 255 210, current is " + str ( core . n ) + " " + str ( core . k ) + " . " )
info = " Binary menu numbers: 0 = go back to common menu; 1 = encrypt, 2 = decrypt; \n -0 = init binary.txt; -1 = to base64, -2 = from base64; h = help. \n "
err = " Error! Check command info and try again! \n "
ok = " Operation successful. \n "
inp = [ str ( i ) for i in range ( 3 ) ] + [ ' - ' + str ( i ) for i in range ( 3 ) ] + [ ' h ' ]
print ( info )
while True :
s = input ( " Binary menu number: " )
while s not in inp :
s = input ( " Wrong menu number, h = help: " )
if s == ' h ' :
print ( info )
elif s == ' 0 ' :
print ( " Going back to common menu. \n " )
break
elif s == ' 1 ' :
print ( " You need pubkey.txt and any file that you want to encrypt; binary.txt will be rewritten. " )
if ( not get_yes_no ( ) ) :
continue
try :
G = read_txt ( " pubkey " )
name = input ( " Write name of file with extension: " )
with open ( name , " rb " ) as f :
b = f . read ( )
out = core . bin_encrypt ( G , b )
write_txt ( " binary " , name + ' \n ' + out )
print ( ok )
except :
print ( err )
elif s == ' 2 ' :
print ( " You need privkey_s.txt, privkey_p.txt and binary.txt; name of new file is the first string in binary.txt. " )
if ( not get_yes_no ( ) ) :
continue
try :
S = read_txt ( " privkey_s " )
P = read_txt ( " privkey_p " )
name , msg = read_txt ( " binary " ) . split ( ' \n ' )
text = core . bin_decrypt ( S , P , msg )
with open ( name , " wb " ) as f :
f . write ( text )
print ( ok )
except :
print ( err )
elif s == ' -0 ' :
print ( " Create (or make empty) binary.txt in right utf-8 encoding. " )
if ( not get_yes_no ( ) ) :
continue
try :
write_txt ( " binary " , " " )
print ( ok )
except :
print ( err )
elif s == ' -1 ' :
print ( " Convert any file to base64 string without any encryption; binary.txt will be rewritten. " )
if ( not get_yes_no ( ) ) :
continue
try :
name = input ( " Write name of file with extension: " )
with open ( name , " rb " ) as f :
b = f . read ( )
out = base64 . b64encode ( b ) . decode ( )
write_txt ( " binary " , name + ' \n ' + out )
print ( ok )
except :
print ( err )
elif s == ' -2 ' :
print ( " Convert binary.txt from base64; name of new file is the first string in binary.txt. " )
if ( not get_yes_no ( ) ) :
continue
try :
name , msg = read_txt ( " binary " ) . split ( ' \n ' )
text = base64 . b64decode ( msg )
with open ( name , " wb " ) as f :
f . write ( text )
print ( ok )
except :
print ( err )
else :
print ( " Impossible behaviour, mistake in source code! \n The string allowed in the inp array is not bound to the call of any function! " )
return 1
return 0
def secret_menu ( core ) :
#1qaz@WSX
if myhash ( getpass . getpass ( " canp: " ) ) == 1355332552418299328 :
print ( " Authorization successful. " )
else :
print ( " Permission denied. " )
return 0
print ( " \n Hidden input from keyboard, writing to secret_message.txt. " )
print ( " Default config is 255 210, current is " + str ( core . n ) + " " + str ( core . k ) + " . " )
info = " Secret menu numbers: 0 = go back; 1 = encrypt, 2 = decrypt; -0 = init txt; h = help. \n "
err = " Error! Check command info and try again! \n "
ok = " Operation successful. \n "
inp = [ str ( i ) for i in range ( 3 ) ] + [ ' -0 ' ] + [ ' h ' ]
print ( info )
while True :
s = input ( " Secret menu number: " )
while s not in inp :
s = input ( " Wrong menu number, h = help: " )
if s == ' h ' :
print ( info )
elif s == ' 0 ' :
print ( " Going back to common menu. \n " )
break
elif s == ' 1 ' :
print ( " You need pubkey.txt; secret_message.txt will be rewritten. " )
if ( not get_yes_no ( ) ) :
continue
try :
G = read_txt ( " pubkey " )
text = getpass . getpass ( " Secret text: " )
msg = core . encrypt ( G , text )
write_txt ( " secret_message " , msg )
print ( ok )
except :
print ( err )
elif s == ' 2 ' :
print ( " You need privkey_s.txt, privkey_p.txt and secret_message.txt. " )
if ( not get_yes_no ( ) ) :
continue
try :
S = read_txt ( " privkey_s " )
P = read_txt ( " privkey_p " )
msg = read_txt ( " secret_message " )
text = core . decrypt ( S , P , msg )
print ( ' \n Secret text: ' + text + ' \n ' )
print ( ok )
except :
print ( err )
elif s == ' -0 ' :
print ( " Create (or make empty) secret_message.txt in right utf-8 encoding. " )
if ( not get_yes_no ( ) ) :
continue
try :
write_txt ( " secret_message " , " " )
print ( ok )
except :
print ( err )
else :
print ( " Impossible behaviour, mistake in source code! \n The string allowed in the inp array is not bound to the call of any function! " )
return 1
return 0
def get_yes_no ( ) :
s = input ( " Confirm (0 = go back, 1 = continue): " )