-1

I have been asked just as a challenge to create a program of which encrypts an input. I have looked into creating a program but there isn't much around on how to do it. It seems like it shouldn't be too complicated, but I haven't been taught any of it in my lessons. I read this post too Get character position in alphabet but didn't have much luck! I have this so far:

import sys
import os
import time
import string
def Cryption():
    ####################
    encrypt = 'encrypt'
    decrypt = 'decrypt'
    ####################
    s = raw_input("Would you like to encrypt or decrypt? Enter your answer")
    if encrypt in s:
        print("Loading encryption sector...")
        time.sleep(2)
        enc = raw_input("Please input the string you would like to encrypt")
        print ("Your decrypted word is: " + enc)
    if decrypt in s:
        print("Loading decryption sector...")
        time.sleep(2)
        dec = raw_input("Please input the string you would like to decrypt")
    else:
        print("Your input was invalid, please re-enter your choice!")
        r = raw_input("Press enter to restart your program")
        Cryption()
Cryption()

I was thinking if I took the input added 5 onto each letter value then re-printed the product. Which functions would I use to add 5 onto the order in the alphabet? ord()? And if so could someone point me in the direction of how to use it? Thanks in advance!

Community
  • 1
  • 1
user3411623
  • 29
  • 1
  • 6

1 Answers1

0
import string


def encrypt_s(shift):
    lower = string.ascii_lowercase
    # shift forward current index of each character + the shift
    dict_map = {k:lower[(lower.index(k)+shift)%26] for k in lower}
    return dict_map

def decrypt_s(shift):
    lower = string.ascii_lowercase
    # shift forward current index of each character - the shift to get original string
    dict_map = {k:lower[(lower.index(k)-shift)%26] for k in lower}
    return dict_map


def Cryption():

    ####################
    encrypt = 'encrypt'
    decrypt = 'decrypt'
    ####################
    s = raw_input("Would you like to encrypt or decrypt? Enter your answer")
    if encrypt in s:
        print("Loading encryption sector...")
        time.sleep(2)
        enc = raw_input("Please input the string you would like to encrypt").lower()
        enc_s = encrypt_s(5)
        enc = "".join([enc_s[char] if char in enc_s else char for char in enc])
        print ("Your encrypted word is: ",enc)
    elif decrypt in s:
        print("Loading decryption sector...")
        time.sleep(2)
        dec = raw_input("Please input the string you would like to decrypt").lower()
        dec_s = decrypt_s(5)
        dec = "".join([dec_s[char] if char in dec_s else char for char in dec])
        print ("Your decrypted word is: ",dec)
    else:
        print("Your input was invalid, please re-enter your choice!")
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321