Tutorial: Creating a visual encryption key in python 3.6(Part 2)

in #utopian-io6 years ago

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
  • Deal with the standard library random class. Seeing how you could use it in various ways in an encryption program.

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

-And also* Part 1 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.

Due to the review comment from @portugalcoin in the last of this series I would be posting the code from my last tutorial series in code format so this code can be copied and tested with ease. Hence this tutorial will consist of both the code from this tutorial and the code from the last

Here is the overall encryption code :

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[2]
    cube['101'] = databank[7]
    cube['111'] = databank[3]
    cube['100'] = databank[6]
    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[7]
    cube['011'] = databank[5]
    cube['111'] = databank[4]
    cube['001'] = databank[6]
    decryption_key += "B"
  elif x == str("D1"):
    cube['000'] = databank[7]
    cube['101'] = databank[0]
    cube['001'] = databank[3]
    cube['100'] = databank[4]
    decryption_key += "D"
  else:
    print("Error, Invalid Command")
  return decryption_key
#Use this function to automatedly encrypt the cube
def random_encrypt(cube):
  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(encryption)
  print("""Use this key to decrypt you code
  Note that you must use the exact bracket you see and you must space your code using the commas as seen""")
  return encryption
  
#You still have to figure out how you're going to make the user interface for the decryption process.
key = random_encrypt(cube_1)

Although the code is available in this tutorial, I would recommend that you write this code personally line by line for better understanding.

In the last post we dealt with defining the cube and definitely each component of encryption which in this method of encryption is the rotation function.
def rotation(command, key)
Which takes two arguments command and key.
Today were going to be creating a function that randomly encrypts the cube using 8 simultaneous random rotations.
This function will be called the random_encrypt(cube)
And it takes one function called cube
If you go upwards into our code you will see that we import the random class. This is a built in class part of pythons standard library in this version of python. Within this class we will be using a function called choice that randomly chooses a string, int or float within a list. We create this function in this order.

  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(encryption)
  print("""Use this key to decrypt you code
  Note that you must use the exact bracket you see and you must space your code using the commas as seen""")
  return encryption

The key variable stores all the possibilities of rotational function that our encryption cube can process and the random.choice function chooses one randomly from this list.

If you observe well you will also see an encryption variable which contains all the code used by the function to randomly encrypt the message. This function will be necessary in the next part of the code where we create our process for de-encryption.

I would recommend that to master this skill you try out other methods of random encryption using you creativity. For example try to use the randint function and use each value generated to index a value from the key variable. If you're successful share your result in the comment section but if you can't pull through I'd be showing you how you could do this in the next tutorial.

Conclusion
Practice makes perfect. Try out this code and run it several times noticing how it gives various and different outputs everytime you run it. In the next tutorial we will learn how to decrypt this encrypted message.

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.
This contribution might not be considered due to the below:

  • Include proof of work under the shape of a gist or your own github repository containing your code.

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

Coin Marketplace

STEEM 0.29
TRX 0.12
JST 0.033
BTC 62831.42
ETH 3123.16
USDT 1.00
SBD 3.85