Myfe - 02/04/22
Tags: python, programming, opensource, mongodb
Published: 2022-04-02 19:48:55

So for the last few days I just felt like quitting this project, because I just didn't feel like continuing it.
But then I decided to continue doing this so here I am again.

So first, I decided to make a file called `cryptography.py` and add a few encryption and decryption functions in there so I can use it in the quizzes for cryptography.

Then I thought of some encryption and decryption methods, and I got the following:
- Hex
- Base64
- Caeser Cipher
- sha256
- md5
- Binary

I realised that I could do Hex-ASCII, ASCII-Hex, Base64-ASCII, ASCII-Base64, Caeser Cipher-ASCII, ASCII-Caeser Cipher, ASCII-sha256, ASCII-md5, Binary-ASCII, and ASCII-Binary.

So I searched Google for the functions :
```py
import base64
import hashlib
import string

alphabet = string.ascii_lowercase

def hextoascii(text):
  return bytes.fromhex(text).decode('utf-8')

def asciitohex(text):
  return text.encode('utf-8').hex()

def base64toascii(text):
  base64_bytes = text.encode('ascii')
  message_bytes = base64.b64decode(base64_bytes)
  message = message_bytes.decode('ascii')
  return message

def asciitobase64(text):
  message_bytes = text.encode('ascii')
  base64_bytes = base64.b64encode(message_bytes)
  base64_message = base64_bytes.decode('ascii')
  return base64_message

def asciitocaesercipher(text,s):
  result = ""
  for i in range(len(text)):
    char = text[i]
    if (char.isupper()):
       result += chr((ord(char) + s-65) % 26 + 65)
    else:
       result += chr((ord(char) + s - 97) % 26 + 97)
  return result

def caeserciphertoascii(encrypted_message, key):
  decrypted_message = ""
  for c in encrypted_message:
    if c in alphabet:
      position = alphabet.find(c)
      new_position = (position - key) % 26
      new_character = alphabet[new_position]
      decrypted_message += new_character
    else:
      decrypted_message += c
  return decrypted_message

def asciitosha256(hash_string):
  hashed_string = hashlib.sha256(hash_string.encode('utf-8')).hexdigest()
  return hashed_string

def asciitomd5(text):
  result = hashlib.md5(text.encode())
  return result

def asciitobinary(text):
  thelist = [bin(ord(x))[2:].zfill(8) for x in text]
  binary = " ".join(thelist)
  return binary

def binarytoascii(binary):
  binary = binary.split()
  return ''.join([chr(int(x, 2)) for x in binary])
```

Next, I shifted to `lists.py` and I added a dictionary called `degreescosts` and added the cost of starting the degree, which is 30:
```py
degreescosts = {'cryptography': 30}
```

After that, I went to `functions.py` and made a function called `addmoney`, because I thought I'd need it soon:
```py
def addmoney(username, amount):
  user = getuser(username)
  money = user['Money']
  money = money + amount
  del user['Money']
  user['Money'] = money
  profilescol.delete_one({"Username": username})
  profilescol.insert_many([user])
  return user
```

Adding on to the code before, I created another function called `adddegree`, in which you can start your degree getting process:
```py
def adddegree(username, jobname):
  jobname = jobname.lower()
  if len(getpreparingdegrees(username)) == 3:
    return "You can't prepare for more than 3 degrees."
  if jobname not in degrees:
    return "This is not a real degree!"
  user = getuser(username)
  if degreescosts[jobname] > user['Money']:
    return f"You don't have enough money to start preparing for {jobname}"
  addmoney(username, -1*degreescosts[jobname])
  degree = [{
    "Username": username,
    "Type": jobname,
    "Status": 0
  }]
  degreescol.insert_many(degree)
```

This is it for today, but in the next blog I'll be the covering the frontend of starting the degree!
Make sure to follow me here and on [GitHub](https://github.com/VulcanWM), and star the [repository](https://github.com/VulcanWM/myfe)
Thanks for reading!