So i have looked in this forum and grabbed a piece of code that let me encrypt text using a cypher. I have changed it so that you can input your own text to be encrypted and a little more.

I am 15 and this is my 2nd day learning python so if this is a easy fix for some ppl bear the last sentance in mind :)

message = input("Input: ")
key = 11
coded_message = ""

for ch in message:
    code_val  = ord(ch) + key
    if ch.isalpha():
        if code_val > ord('z'):
            code_val -= ord('z') - ord('a')
        coded_message = coded_message + chr(code_val)
    else:
        coded_message = coded_message + ch
# print ("Input: " + message)
print ("Output: " + coded_message)

My question is how do i make a code that will get the crypted text and turn it into the original text.

Recommended Answers

All 2 Replies

You seem to have some bug, basically you decode with 26 - key to get original message. It is better to learn to use functions.

#Python 2 compatibility
from __future__ import print_function
try:
    input = raw_input
except:
    pass

def caesar(message, key):
    coded_message = ""

    for ch in message.lower():
        code_val  = (ord(ch) - ord('a') + key) % 26
        if ch.isalpha():
            coded_message = coded_message + chr(code_val + ord('a'))
        else:
            coded_message = coded_message + ch
    return coded_message

# print ("Input: " + message)
message = input("Input: ")
coded = caesar(message, 11)
print ("Output: " + coded)
print("Decoded: " + caesar(coded, 26-11))

Ok thankyou, i learn by looking at code then figuring out what makes it tick then i try to replicate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.