Friday, January 31, 2014

My First Python Encryptor



As part of my curriculum at Champlain College I was required to take Introduction to Computer Theory. This introduced me to the Python programming language for what was really the first time. My first impression of Python was that I  loved it. I instantly saw the potential that a language like this (or any scripting language such as perl/bash etc.) truly has. 

As my final project for the class I was able to choose to either write a research paper or create my own Python program. Despite my side-love for writing I chose to try something new and write my own program. After a little bit of thought I knew that I wanted to create a simple string encryptor. This seemed to me to be within my skill level, yet at the same time something that would challenge me. I put ALOT of time into this piece of code and taught myself more Python outside of class then was necessary. It was worth it though for at the end I had a working Python program that I was proud of and could truly call my own.

I wrote the code in a way that it could be expanded upon by adding more encryption/decryption methods. This particularly version is simply my one encryption method that I invented wrapped up into a user-input type text interface.

How it works:
1. Asks user to use the program
2. Asks user if they want to encrypt or decrypt a string
3. Asks the user for their desired method
4. If encryption, parses a string seeded with a unique key through an encryption algorithm
5. If decryption parses a encrypted string seeded with the key through a decryption algorithm

Things to keep in mind:
-My invented encryption algorithm is named "bloat"
-Due to the character encoding this program currently only runs in the IDLE environment
-This code was written using Python 3.3.3 and as such will only work when ran using Python version 3.

 Source Code:

#!/usr/bin/env python3
import random
import string
import sys
from collections import deque
#sys.setdefaultencoding("UTF-8")
#Bloat encryption invented by Hunter Gregal
#Encrypt bloat
def bloat_encrypt(destring,enstring):
    m=(string.ascii_lowercase)
 
    #Asks for a key from the user
    key=str(input("Please enter a key to encrypt the string. This key will be required in order to decrypt it as well.\n: "))
    #gives key numerical value
    keylist=list(key)
    i=len(key) - 1
    c=0
    while i:
        c= c + ord(keylist[i])
        i=i-1
     
    #encodes destring with key
    destring=str(destring)
    destringlist=list(destring)
    i=len(destring) -1
    while i:
        n = ord(destringlist[i]) #gives each letter a number value
        destringlist[i]=chr(n+c) #increase each letter value by key value and converts it to its unicode char
        i=i-1
    destring=''.join(destringlist)

   #Further obfuscates destring by salting it  
    while destring:
     
        #Reverses string and adds random letters
        x=(len(destring)-1)
        enstring = (enstring + destring[x] + random.choice(m))
        destring = destring[0:x]
    print(enstring)    
    return destring,enstring
#Decrypt bloat
def bloat_decrypt(destring,enstring):
    key=str(input("Please enter a key to decrypt the string with.\n:"))
    #Reverses string and removes salt
    destring=''
    x=list(enstring)
    x=x[::2]
    x.reverse()
    destring=''.join(x)
    #gives key numerical value
    keylist=list(key)
    i=len(key) - 1
    c=0
    while i:
        c= c + ord(keylist[i])
        i=i-1
    #Decrypts string with the key
    destring=str(destring)
    destringlist=list(destring)
    i=len(destring) -1
    while i:
        m = ord(destringlist[i])
        destringlist[i]=chr(m-c)
        i=i-1
    destring=''.join(destringlist)
 
    print(destring)
    return destring,enstring
 



Running in IDLE: 

print("Python string encryptor brought to you by Hunter Gregal")
print("\n\n\n")
#Define enstring/destring ahead of time
enstring=''
destring=''
go=input("Would you like to use the program? Y/N\n: ")
while go == "Y" or go == "y":  #Run or breaks the program depending on input
    choice=input("Would you like to decrypt or encrypt a string? [decrypt]/[encrypt]\n: ") #Asks user if they want to encrypt or decrypt
    if choice == "encrypt" or choice == "Encrypt": #If user chooses encrypt....
        method=input("What encryption method would you like to use?\n [bloat]\n: ")
        destring=input("What string would you like to encrypt?\n: ")
 
        if method == "bloat" or method == "Bloat": #If user chooses bloat run bloat_encrypt function
            bloat_encrypt(destring,enstring)
 
     
    elif choice =="decrypt" or choice == "Decrypt": #If user chooses decrypt ....
        method=input("What is the decryption method you would like to use?\n [bloat]\n: ")
        enstring=input("What is the string that you would like to decrypt?\n: ")
 
        if method == "bloat" or method == "Bloat": #If users chooses bloat run bloat_decrypt function
            bloat_decrypt(destring,enstring)
 
 
        go=input("Would you like to use the program again? Y/N\n: ") #Keep running?
input("Press any key to exit")


2 comments:

  1. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.

    Python Training in Chennai | Python Training Institutes in Chennai

    ReplyDelete
  2. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
    Python Training in Bangalore

    ReplyDelete