Dec-28-2017, 08:43 PM
""" This module lets you encrypt and decrypt integers and strings, in four functions. Please name it crypt.py""" # 1 -- Setting the needs import random import json # A list containing low-case alphabets abc = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] # Setting the uppercase version of the alphabets (abc) list ABC = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] # 2 -- Defining the integer related functions # -- Defining the integer encrypter def encrInt(rnum, dir): ''' rnum: number to be encrypted ''' ''' dir: directory name, without '.json' ''' try: num = rnum if rnum: strrnum = str(rnum) nums = [] for n in strrnum: n = random.randint(0, 9) nums.append(str(n)) enum = '' for en in nums: enum += en enum = int(enum) enumdict = {} enumdict['key'] = dir enumdict['value'] = num enumdict['evalue'] = enum file = dir + '.json' with open(file, 'w') as jf: json.dump(enumdict, jf) return enum else: pass except: return "Invalid Int: %s" % rnum # -- Defining the integer decrypter def decrInt(dir): ''' dir: same directory name as in 'encrInt(rnum, dir)' ''' try: file = dir + ".json" with open(file) as jf: inFileDict = json.load(jf) except: return "Undefined key: %s" % dir else: dnum = inFileDict['value'] return dnum # -- Defining the integer decrypter, but loads and returns the encrypted value def decrInt_(dir): ''' dir: same directory name as in 'encrInt(rnum, dir)' ''' try: file = dir + ".json" with open(file) as jf: inFileDict = json.load(jf) except: return "Undefined key: %s" % dir else: ev = inFileDict['evalue'] return ev # 3 -- Defining the string related functions # -- Defining the string encrypter that encrypts only alphabets, and leave other characters like as they are def encrStr(rstr, dir): ''' rstr: string to be encrypted ''' ''' dir: directory name, without '.json' ''' tstr = rstr.strip() estr = '' if tstr: for char in rstr: if char in abc: char = random.choice(abc) estr += char elif char in ABC: char = random.choice(ABC) estr += char else: char = char estr += char estrdict = {} estrdict['key'] = dir estrdict['value'] = rstr estrdict['evalue'] = estr file = dir + '.json' with open(file, 'w') as jf: json.dump(estrdict, jf) return estr else: pass # -- Defining the string encrypter that encrypts the whole string def _encrStr(rstr, dir): ''' rstr: string to be encrypted ''' ''' dir: directory name, without '.json' ''' tstr = rstr.strip() estr = '' if tstr: for char in rstr: if char in abc: char = random.choice(abc) estr += char elif char in ABC: char = random.choice(ABC) estr += char else: char = '$' estr += char estrdict = {} estrdict['key'] = dir estrdict['value'] = rstr estrdict['evalue'] = estr file = dir + '.json' with open(file, 'w') as jf: json.dump(estrdict, jf) return estr else: pass # -- Defining the string decrypter def decrStr(dir): ''' dir: same directory name as in 'encrStr(rstr, dir)' ''' try: file = dir + '.json' with open(file) as jf: inFileDict = json.load(jf) except: return "Undefined key: %s" % dir else: dstr = inFileDict['value'] return dstr # -- Defining the string decrypter, but loads and returns the encrypted value def decrStr_(dir): ''' dir: same directory name as in 'encrStr(rstr, dir)' ''' try: file = dir + '.json' with open(file) as jf: inFileDict = json.load(jf) except: return "Undefined key: %s" % dir else: ev = inFileDict['evalue'] return ev