Tutorial: Creating an visual cube encryption program with python 3.6(Part 3)

in #utopian-io6 years ago (edited)

images.png
image source
Repository: Python, Open Source Repository

Software: For software download python 3.0 compatible with your OS here

Difficulty : Intermediate
What you will learn:
In this tutorial you will learn how to

  • Make an encryption key with cpython
  • Subsequent to the previous in this tutorial series, we will be dealing with the decryption process

For my previous post in the python tutorial series
-Click Here* Creating a simple calculator using python 3.8(cpython)

- Part 1 of this series
- Part 2 of this series
This previous post covers common bugs encountered when trying to write this code and how to fix them. It shows how to write this basic code from scratch.

Writing an encryption key with python 3.8(cpython)
Note: This tutorial is for people who have a basic knowledge of the python programming language as it does not explain every function individually.
So let's sharply go through the code we've written so far.

print("""Crypto Cube Game
Developed by Yalzeee""")
# This is the Crypto-Cube encryption game developed by yalzeee
#First things first I print the intructions on how to use the encryption program
print("""Enter 
\'U\' or \'U1\' to move the top of the cube
\'R\' or \'R1\'to move the right of the cube
\'L\' or \'L1\'to move the left of the cube
\'D\' or \'D1\'to move the bottom of the cube
\'F\' or \'F1\'to move the front of the cube and
\'B\' or \'B1\'to move the back of the cube
""")
import random  
#I import the random class so i can make an automatic encryption method
#Define the message within each cube
#For this program i will be using two cubes because message i want to encrypt is "Sololearnyalzeee" which is 16 characters
cube_1 = {"000":"s",
 "010":"o",
  '110':"l", 
  '100':"o", 
  '001':"l",
  '011':"e",
  '111':"a", 
  '101':"r"}
cube_2 = {'000':"n",
 '010':"y",
 '110':"a",
 '100':"l",
 '001':"z",
 '011':"e", 
 '111':"e", 
 '101':"e"}
#Define a function to print my code in a orderly manner

def print_orderly(dict):
  return print(dict["000"] + dict["010"] +  dict["110"] + dict["100"] + dict["001"] + dict["011"] + dict["111"] + dict["101"])
#Test it out
print_orderly(cube_1)
#Define the rotation function
def rotation(command, cube):
  decryption_key = []
  x = command
  databank = [cube["000"], cube["010"], cube["110"], cube["100"], cube["001"], cube["011"], cube["111"], cube["101"]]  
  if x == str("F"):
    cube['000'] = databank[3]
    cube['010'] = databank[0]
    cube['110'] = databank[1]
    cube['100'] = databank[2]
    decryption_key += "U1"
  elif x == str("R"):
    cube['110'] = databank[3]
    cube['101'] = databank[6]
    cube['111'] = databank[2]
    cube['100'] = databank[7]
    decryption_key += "R1"
  elif x == str("U"):
    cube['011'] = databank[1]
    cube['010'] = databank[2]
    cube['110'] = databank[6]
    cube['111'] = databank[5]
    decryption_key += 'U1'
  elif x == str("L"):
    cube['000'] = databank[1]
    cube['010'] = databank[5]
    cube['011'] = databank[4]
    cube['001'] = databank[0]
    decryption_key += "L1"
  elif x == str("D"):
    cube['000'] = databank[4]
    cube['101'] = databank[3]
    cube['001'] = databank[7]
    cube['100'] = databank[0]
    decryption_key += "D1"
  elif x == str("B"):
    cube['101'] = databank[4]
    cube['011'] = databank[6]
    cube['111'] = databank[7]
    cube['001'] = databank[5]
    decryption_key += "B1"
  elif x == str("U1"):
    cube['011'] = databank[6]
    cube['010'] = databank[5]
    cube['110'] = databank[1]
    cube['111'] = databank[2]
    decryption_key += "U"
  elif x == str("F1"):
    cube['000'] = databank[1]
    cube['010'] = databank[2]
    cube['110'] = databank[3]
    cube['100'] = databank[0]
    decryption_key += "F"
  elif x == str("R1"):
    cube['110'] = databank[6]
    cube['101'] = databank[3]
    cube['111'] = databank[7]
    cube['100'] = databank[2]
    decryption_key += "R"
  elif x == str("L1"):
    cube['000'] = databank[4]
    cube['010'] = databank[0]
    cube['011'] = databank[1]
    cube['001'] = databank[5]
    decryption_key += "L"
  elif x == str("B1"):
    cube['101'] = databank[6]
    cube['011'] = databank[4]
    cube['111'] = databank[5]
    cube['001'] = databank[7]
    decryption_key += "B"
  elif x == str("D1"):
    cube['000'] = databank[3]
    cube['101'] = databank[4]
    cube['001'] = databank[0]
    cube['100'] = databank[7]
    decryption_key += "D"
  else:
    print("Error, Invalid Command")
  return decryption_key
#Use this function to automatedly encrypt the cube
def random_encrypt(cube):
  key = input("Enter the encryption key") ["U","R","L","D","F","B","U1","R1","L1","D1","F1","B1"]
  a = random.choice(key)
  b = random.choice(key)
  c = random.choice(key)
  d = random.choice(key)
  e = random.choice(key)      
  f = random.choice(key)
  g = random.choice(key)
  h = random.choice(key)
  
  rotation(a,cube)
  rotation(b,cube)
  rotation(c,cube)
  rotation(d,cube)
  rotation(e,cube)
  rotation(f,cube)
  rotation(g,cube)
  rotation(h,cube)
  encryption = [h,g,f,e,d,c,b,a]
  print
  return encryption
  
#You still have to figure out how you're going to make the user interface for the decryption process.

Today we're going to be dealing with the decryption process and we're going to be creating a function called decrypt.
We're going to be using boolean functions for this and we'll define a process for each process of rotation.
We will write the code in this format

def decrypt(encryption, cube):
  if encryption[0] == "U":
    rotation("U1",cube)
  elif encryption[0] == "F":
    rotation("F1",cube)
  elif encryption[0] == "R":
    rotation("R1",cube)
  elif encryption[0] == "L":
    rotation("L1",cube)
  elif encryption[0] == "B":
    rotation("B1",cube)
  elif encryption[0] == "D":
    rotation("D1",cube)
  elif encryption[0] == "B1":
    rotation("B",cube)
  elif encryption[0] == "U1":
    rotation("U",cube)
  elif encryption[0] == "F1'":
    rotation("F",cube)
  elif encryption[0] == "D1'":
    rotation("D",cube)
  elif encryption[0] == "R1'":
    rotation("R",cube)
  elif encryption[0] == "L1'":
    rotation("L",cube) 
  else:
    print("One of your inputs is wrong bruh")
    if encryption[1] == "U":
      rotation("U1",cube)
    elif encryption[1] == "F":
      rotation("F1",cube)
    elif encryption[1] == "R":
      rotation("R1",cube)
    elif encryption[1] == "L":
      rotation("L1",cube)
    elif encryption[1] == "B":
      rotation("B1",cube)
    elif encryption[1] == "D":
      rotation("D1",cube)
    elif encryption[1] == "B1":
      rotation("B",cube)
    elif encryption[1] == "U1":
      rotation("U",cube)
    elif encryption[1] == "F1'":
      rotation("F",cube)
    elif encryption[1] == "D1'":
      rotation("D",cube)
    elif encryption[1] == "R1'":
      rotation("R",cube)
    elif encryption[1] == "L1'":
      rotation("L",cube) 
    else:
      print("One of your inputs is wrong bruh")
      if encryption[2] == "U":
        rotation("U1",cube)
      elif encryption[2] == "F":
        rotation("F1",cube)
      elif encryption[2] == "R":
        rotation("R1",cube)
      elif encryption[2] == "L":
        rotation("L1",cube)
      elif encryption[2] == "B":
        rotation("B1",cube)
      elif encryption[2] == "D":
        rotation("D1",cube)
      elif encryption[2] == "B1":
        rotation("B",cube)
      elif encryption[2] == "U1":
        rotation("U",cube)
      elif encryption[2] == "F1'":
        rotation("F",cube)
      elif encryption[2] == "D1'":
        rotation("D",cube)
      elif encryption[2] == "R1'":
        rotation("R",cube)
      elif encryption[2] == "L1'":
        rotation("L",cube) 
      else:
        print("One of your inputs is wrong bruh")
        if encryption[3] == "U":
          rotation("U1",cube)
        elif encryption[3] == "F":
          rotation("F1",cube)
        elif encryption[3] == "R":
          rotation("R1",cube)
        elif encryption[3] == "L":
          rotation("L1",cube)
        elif encryption[3] == "B":
          rotation("B1",cube)
        elif encryption[3] == "D":
          rotation("D1",cube)
        elif encryption[3] == "B1":
          rotation("B",cube)
        elif encryption[3] == "U1":
          rotation("U",cube)
        elif encryption[3] == "F1'":
          rotation("F",cube)
        elif encryption[3] == "D1'":
          rotation("D",cube)
        elif encryption[3] == "R1'":
          rotation("R",cube)
        elif encryption[3] == "L1'":
          rotation("L",cube) 
        else:
          print("One of your inputs is wrong bruh")
          if encryption[4] == "U":
            rotation("U1",cube)
          elif encryption[4] == "F":
            rotation("F1",cube)
          elif encryption[4] == "R":
            rotation("R1",cube)
          elif encryption[4] == "L":
            rotation("L1",cube)
          elif encryption[4] == "B":
            rotation("B1",cube)
          elif encryption[4] == "D":
            rotation("D1",cube)
          elif encryption[4] == "B1":
            rotation("B",cube)
          elif encryption[4] == "U1":
            rotation("U",cube)
          elif encryption[4] == "F1'":
            rotation("F",cube)
          elif encryption[4] == "D1'":
            rotation("D",cube)
          elif encryption[4] == "R1'":
            rotation("R",cube)
          elif encryption[4] == "L1'":
            rotation("L",cube) 
          else:
            print("One of your inputs is wrong bruh")
            if encryption[5] == "U":
              rotation("U1",cube)
            elif encryption[5] == "F":
              rotation("F1",cube)
            elif encryption[5] == "R":
              rotation("R1",cube)
            elif encryption[5] == "L":
              rotation("L1",cube)
            elif encryption[5] == "B":
              rotation("B1",cube)
            elif encryption[5] == "D":
              rotation("D1",cube)
            elif encryption[5] == "B1":
              rotation("B",cube)
            elif encryption[5] == "U1":
              rotation("U",cube)
            elif encryption[5] == "F1'":
              rotation("F",cube)
            elif encryption[5] == "D1'":
              rotation("D",cube)
            elif encryption[5] == "R1'":
              rotation("R",cube)
            elif encryption[5] == "L1'":
              rotation("L",cube) 
            else:
              print("One of your inputs is wrong bruh")
              if encryption[6] == "U":
                rotation("U1",cube)
              elif encryption[6] == "F":
                rotation("F1",cube)
              elif encryption[6] == "R":
                rotation("R1",cube)
              elif encryption[6] == "L":
                rotation("L1",cube)
              elif encryption[6] == "B":
                rotation("B1",cube)
              elif encryption[6] == "D":
                rotation("D1",cube)
              elif encryption[6] == "B1":
                rotation("B",cube)
              elif encryption[6] == "U1":
                rotation("U",cube)
              elif encryption[6] == "F1'":
                rotation("F",cube)
              elif encryption[6] == "D1'":
                rotation("D",cube)
              elif encryption[6] == "R1'":
                rotation("R",cube)
              elif encryption[6] == "L1'":
                rotation("L",cube) 
              else:
                print("One of your inputs is wrong bruh")
                if encryption[7] == "U":
                  rotation("U1",cube)
                elif encryption[7] == "F":
                  rotation("F1",cube)
                elif encryption[7] == "R":
                  rotation("R1",cube)
                elif encryption[7] == "L":
                  rotation("L1",cube)
                elif encryption[7] == "B":
                  rotation("B1",cube)
                elif encryption[7] == "D":
                  rotation("D1",cube)
                elif encryption[7] == "B1":
                  rotation("B",cube)
                elif encryption[7] == "U1":
                  rotation("U",cube)
                elif encryption[7] == "F1'":
                  rotation("F",cube)
                elif encryption[7] == "D1'":
                  rotation("D",cube)
                elif encryption[7] == "R1'":
                  rotation("R",cube)
                elif encryption[7] == "L1'":
                  rotation("L",cube) 
                else:
                  print("One of your inputs is wrong bruh")

Hint: Instead of writing this lengthy code define the function using if encryption[0] == "U" and all the consecutive elif and else functions for encryption[0] then copy this code and paste it for every other index in the code.

Make sure you indent your code properly by placing every if below the if statement that comes before this. In other words put this code to spaces right below the previous code. If you miss a single indentation when you run your code you should have and indentation_error. Go back through your code and make sure every if statement is indented just as seen in the code above.
Run your program multiple times and using the rotation() function first and then the random_encrypt function. The code will print an encryption key you can use to decrypt your message. Also edit you code and change the message within the cube to whatever you like and run the program from the two cubes.
In the next of this tutorial series we will be moving onto making a visual representation of this program.

Proof of work
GITHUB Repository

Sort:  

Hello! I find your post valuable for the wafrica community! Thanks for the great post! @wafrica is now following you! ALWAYs follow @wafrica and use the wafrica tag!

Thank you for your contribution.
While I liked the content of your contribution, I would still like to extend few advices for your upcoming contributions:

  • Put the code with indentation.
  • It was interesting to see images of the application running.

Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thanks for the comment
I will add images of the output of my code successfully running in my next tutorial.

Hey @yalzeee
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.29
TRX 0.12
JST 0.034
BTC 62759.93
ETH 3112.27
USDT 1.00
SBD 3.87