Source

From Fiamma
Jump to navigationJump to search
  1. !/usr/bin/python
  1. TOFIX: short option for mode, works only with q; "--m"

import sys, getopt # Command line arguments import binascii # Convert ascii to bin and back

  1. from past import autotranslate
  2. autotranslate(['secretsharing']);

from secretsharing import PlaintextToHexSecretSharer

verbose=0;

  1. Function that prints according to verbosity.# {{
  2. pr(text, minimum_level_of_verbosity)

def pr(text, level):

   global verbose;
   #print("VerbLevel: ", str(verbose));
   #print("Level: ", str(level));
   if int(verbose)>=level:
       print(text);
  1. }}
  1. Encode given text to binary numbers.# {{

def textToBinary(text):

   #print("=");
   #print("Encoding the following text to binary notation:");
   #print(text);
  1. https://stackoverflow.com/questions/4523551/python-ascii-to-binary
   #resultingBinary=str(bin(int(binascii.hexlify(text.encode('ascii', 'strict')), 16)))[2:]
   #resultingBinary=str(bin(int(binascii.hexlify(text.encode('ascii', 'strict')), 16)))[2:]
   resultingBinary=str(bin(int(binascii.hexlify(text), 16)))[2:]
   return(resultingBinary);# }}
  1. Decode given binary numbers to text # {{

def binToText(bin):

   #print("=");
   #print("Encoding the following text to binary notation:");
   #print(text);
  1. https://stackoverflow.com/questions/4523551/python-ascii-to-binary
   #resultingBinary=str(bin(int(binascii.hexlify(text.encode('ascii', 'strict')), 16)))[2:]
   #resultingBinary=str(bin(int(binascii.hexlify(text.encode('ascii', 'strict')), 16)))[2:]
   n = int('0b'+bin, 2)
   print(n);
   originalText=binascii.unhexlify('%x' % n);
   print(originalText);
   return(originalText);# }}

def help():# {{

   print('test.py -m [encrypt=e|decypt=d] -i <inputText> -v [0|1|2]');
   print('\tm: mode. Can be encrypt|decrypt=e|d');
   print('\ti: inputText. ');
   print('\tv: verbosity. 0 = default = not verbose. 1=verbose. 2=very verbose;. ');
   sys.exit(2)# }}

def getBinaryShares(shares):# {{

   binaryShares=[];
   for i in shares:
       binaryShares.append(textToBinary(i));
   return binaryShares# }}

def getBackTextShares(binShares):# {{

   textShares=[];
   for i in binShares:
       textShares.append(binToText(i));
   return textShares 
  1. }}

def getSecrets(text, m, n):

   shares = PlaintextToHexSecretSharer.split_secret(text, m, n)
   return shares;

def getTextFromSecrets(shares):

   result=PlaintextToHexSecretSharer.recover_secret(shares);
   return result;

def main(argv):

   global verbose;
    1. {{ ARGUMENTS PARSING
   inputText = 
   mode=
   try:
       opts, args = getopt.getopt(argv,"hmi:v",["input=", "mode=", "verbose="])
   except getopt.GetoptError:
       print("GetoptError");
       help();
   for opt, arg in opts:
       if opt == '-h':
           help();
       elif opt in ("-i", "--input"):
           inputText = arg;
       elif opt in ("-m", "--mode"):
           if arg in ("encrypt", "e"):
               mode = "e"
           elif arg in ("decrypt", "d"):
               mode = "d"
           else:
               print("For mode use 'encrypt'/'decrypt' or their short version 'e'/'d'\n-");
               help();
       elif opt in ("-v", "--verbose"):
           if arg in ("0"): #Redundant
               verbose = "0"
           elif arg in ("1", "y"):
               verbose = "1"
           elif arg in ("2", "yy"):
               verbose = "2"
           else:
               print("For verbosity, use [-v|--verbose]=[0|1|2], 0 is default, 1=verbose, 2=very\n-");
               help();
  1. }}
    1. {{ OUTPUT BASIC INFO
   pr('Verbose:'+ str(verbose), 1);
   pr('Mode is:' + mode, 1);
   pr('Input text: '+ inputText, 2);
   pr('--', 1);
  1. }}
   shares=getSecrets(inputText, 2, 3);
   binShares=getBinaryShares(shares);
   textShares=getBackTextShares(binShares);
   print(getTextFromSecrets(
       list( textShares[i] for i in [0, 2] ))
       );
  1. Now let's try with secret sharing

if __name__ == "__main__":

  main(sys.argv[1:])