Python Forum
Use this integer and string encrypter and decrypter module in your projects
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use this integer and string encrypter and decrypter module in your projects
#1
""" 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
Reply
#2
It's customary to show how something is used, if you'd like someone to use it.

I didn't really study it, but if you're using the random module, it's probably not really encryption/decryption, right?  You're just converting any input into random nonsense.  And storing the "encrypted" value, plaintext, in a json file, doesn't really count as being encrypted.
Reply
#3
Output:
>>> from string import letters, uppercase, lowercase >>> lowercase 'abcdefghijklmnopqrstuvwxyz' >>> uppercase 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
Reply
#4
(Dec-28-2017, 10:04 PM)micseydel Wrote:
Output:
>>> from string import letters, uppercase, lowercase >>> lowercase 'abcdefghijklmnopqrstuvwxyz' >>> uppercase 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

I was just about to write the same
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
I didn't know that, thanks!

Ok nilamo! But now I know more about encryption and encoding, I just named it crypt because I didn't find any other name... Anyways thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo All 31 of my Python projects with source code steve_shambles 2 4,094 Mar-23-2020, 12:08 AM
Last Post: steve_shambles
  My first python projects (help with reviews and opinions) wilfredinni 0 7,986 Mar-06-2018, 02:33 PM
Last Post: wilfredinni
  5 mini programming projects for the python beginner kerzol81 4 36,112 Sep-26-2017, 02:36 PM
Last Post: VeenaReddy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020