def h_operation(self,a,b,mod=None,fix=None): assert Aux.is_int(a) assert Aux.is_int(b) a = long(a) b = long(b) if mod is None: mod = Cipher.get_public_key(self)["n2"] if fix is None: c = a*b % mod else: c = a*b*fix % mod return str(c)
def encrypt(self,m): if type(m) == str: m = int(m) assert Aux.is_int(m) pub = Cipher.get_public_key(self) assert pub.has_key("n") assert pub.has_key("g") n = pub["n"] n2 = n*n if not pub.has_key("n2") else pub["n2"] if not pub.has_key("n2"): pub["n2"] = n2 g = pub["g"] r = pub["r"] if pub.has_key("r") else random.randrange(1,n) assert abs(m) < n if m < 0: g_m__n2 = self.__modinv(pow(g,-m,n2),n2) else: g_m__n2 = pow(g,m,n2) c = g_m__n2*pow(r,n,n2) % n2 return str(c)
def cmd_delayms(args): '''Command DELAYMS (wait for some milli-seconds)''' # Check for expected number of arguments if len(args) < 1: print_log(LOG.ERROR, TEXT.CMD_NO_ARGS.format("DELAYMS")) return False # Check for valid time value if not is_int(args[0]): print_log(LOG.ERROR, TEXT.DELAYMS_INVALID) return False delay_time_ms = int(args[0]) print_log(LOG.INFO, TEXT.DELAYMS.format(delay_time_ms)) sleep(delay_time_ms / 1000) return True
def cmd_cfrestimeout(args): '''Command CFGRESTIMEOUT (set Serial read timeout)''' global res_timeout # Check for expected number of arguments if len(args) < 1: print_log(LOG.ERROR, TEXT.CMD_NO_ARGS.format("CFGRESTIMEOUT")) return False # Check for valid timeout value if not is_int(args[0]): print_log(LOG.ERROR, TEXT.CFGRESTIMEOUT_INVALID) return False res_timeout = int(args[0]) / 1000 if ser is not None: ser.timeout = res_timeout print_log(LOG.INFO, TEXT.CFGRESTIMEOUT_SET.format(res_timeout)) return True
def decrypt(self,c): assert Aux.is_int(c) c = long(c) pub = Cipher.get_public_key(self) priv = Cipher.get_private_key(self) assert pub.has_key('n') assert pub.has_key('g') assert priv.has_key('lambda') n = pub['n'] n2 = n*n if not pub.has_key("n2") else pub["n2"] g = pub['g'] l = priv['lambda'] mi = pow(l,l-1,n) charmical_function = lambda u,n: (u-1)/n m = charmical_function(pow(c,l,n2),n)*mi % n return m
def encrypt(self, m): # # Encrypts a single integer # if type(m) == str: m = int(m) assert Aux.is_int(m) pub = Cipher.get_public_key(self) assert pub.has_key("p") assert pub.has_key("alpha") assert pub.has_key("beta") p = pub["p"] alpha = pub["alpha"] beta = pub["beta"] km = pub["km"] if pub.has_key("km") else None if self.exponential_mode: if m < 0: x = self.__modinv(pow(alpha, -m, p), p) else: x = pow(alpha, m, p) else: x = m if not km: i = random.randrange(2, p - 1) ke = pow(alpha, i, p) km = pow(beta, i, p) c = (x * km) % p return c, ke else: c = (x * km) % p return c
def cmd_connect(args): '''Command CONNECT (make a Serial Connection)''' global ser # Check for expected number of arguments if len(args) < 2: print_log(LOG.ERROR, TEXT.CMD_NO_ARGS.format("CONNECT")) return False # Get and check arguments serial_port = args[0] serial_bauds = args[1] if not is_int(serial_bauds): print_log(LOG.ERROR, TEXT.CONNECT_INVALID_BAUDS) return False serial_bauds = int(serial_bauds) # Try to open the serial port print_log(LOG.INFO, TEXT.CONNECT_OPENING.format(serial_port, serial_bauds)) ser = serial_open(serial_port, serial_bauds, res_timeout, 1.0) if (ser is None) or (not ser.isOpen()): print_log(LOG.INFO, TEXT.CONNECT_OPEN_FAIL) return False print_log(LOG.INFO, TEXT.CONNECT_OPEN) return True
def encrypt(pub,m,km=None): # # Encrypts a single integer # assert Aux.is_int(m) assert pub.has_key('p') assert pub.has_key('alpha') assert pub.has_key('beta') p = pub['p'] alpha = pub['alpha'] beta = pub['beta'] if not km: i = random.randrange(2,p-1) ke = Aux.square_and_multiply(alpha,i,p) km = Aux.square_and_multiply(beta,i,p) c = (m*km) % p return c,ke else: c = (m*km) % p return c,None