Python Forum

Full Version: Ceaser cipher program
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi eveyone,

I'm currently working on a ceasar cipher project. I pretty much have the basic structure down and the program is able to auto-encrypt the input ( given text ) by +1 position. I can change the positioning either by "int(input)"option or by directly adding to the code. Now I would like to change the position of each letter separately as follows: +1 +2 +3 +4 +5 etc.

Example 1 with input option:
def ascii_code(message):
    print("Decryption with ASCII-output")
    for symbol in message:
        print(ord(symbol), end = " ")

    print("\n")

def caesar(message):
    print("Caesar Decryption:")
    shift = int(input("Please enter the shift value: "))
    for letter in message:
        if letter.isupper():
            print(chr((ord(letter)+ shift - 65) % 26 + 65), end = " ")
        else:
            print(chr((ord(letter)+ shift - 97) % 26 + 97), end = " ")


    print("\n")

enter = input("Please enter your message: ")
print("The original message is:", enter, "\n")

ascii_code(enter)
caesar(enter)
Example 2 with manual entry option with shift value +1:
def ascii_code(message):
    print("Decryption with ASCII-output")
    for symbol in message:
        print(ord(symbol), end = " ")

    print("\n")

def caesar(message):
    print("Caesar Decryption:")
    shift = 1 
    for letter in message:
        if letter.isupper():
            print(chr((ord(letter)+ shift - 65) % 26 + 65), end = " ")
        else:
            print(chr((ord(letter)+ shift - 97) % 26 + 97), end = " ")


    print("\n")

enter = input("Please enter your message: ")
print("The original message is:", enter, "\n")

ascii_code(enter)
caesar(enter)
My message is: Iamsecret. Currently the output is Jbntfsfu using +1. I would like the encryption to be +1 J / +2 c / +3 p and so on. I've tried numerous routes, but i keep Wall. Do i have to add another loop to the function or do I change the current loop? Any hints, help would be much appreciated.


Thanks
I tried using code from w3schools tutorial here: https://www.w3resource.com/python-exerci...ise-25.php
here's the code (modified fro your message and shift value:
#https://gist.github.com/nchitalov/2f2b03e5cf1e19da1525
def caesar_encrypt(realText, step):
	outText = []
	cryptText = []
	
	uppercase = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
	lowercase = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

	for eachLetter in realText:
		if eachLetter in uppercase:
			index = uppercase.index(eachLetter)
			crypting = (index + step) % 26
			cryptText.append(crypting)
			newLetter = uppercase[crypting]
			outText.append(newLetter)
		elif eachLetter in lowercase:
			index = lowercase.index(eachLetter)
			crypting = (index + step) % 26
			cryptText.append(crypting)
			newLetter = lowercase[crypting]
			outText.append(newLetter)
	return outText

code = caesar_encrypt('Iamsecret.', 1)
print()
print(code)
print()
The results are the same as what you get, but in a list, and
no 'c' at end
Output:
theirs: ['J', 'b', 'n', 't', 'f', 'd', 's', 'f', 'u'] yours: J b n t f d s f u c
Hi Larz60+

thanks for the quick reply. That has the same output as my original code though. The user must be able to add his/her own message to encrypt. What is most important though is that the output should to be the following:

example:

I a m s e c r e t
+1 +2 +3 +4 +5 +6 +7 +8 +9

output:
J c p w j i y m c

I can't figure this out.
Take a look at what your code does each step. First, you assign the value 1 to shift. Then, each iteration of the loop, you add the value of shift to the next letter. So the first letter has 1 added to it as shift equals 1, the second letter has 1 added to it because shift still equals 1, the third letter has 1 added to it because shift still equals 1, etc...

But you want to have 1 added to the first, 2 added to the second, 3 added to the third, etc... So what needs to change each loop? How would you change it, and where in the code would be the best place to change it?
my code was just to show you that your code was almost ok, however you have one additional output.
as far as input is concerned, put your code lines 20 - 24 into a loop, preferably all within a function.
Hi guys, yea I got it. Not sure if this is the optimal way, but it works. Thanks again for the help :)

def caesar(message):
    print("Caesar Decryption:")
    value = 1
    for letter in message:
        if letter.isupper():
            print(chr((ord(letter)+ value - 65) % 26 + 65  ), end = " ")
        else:
            print(chr((ord(letter)+ value - 97 ) % 26 + 97 ), end = " ")
        value = value + 1
    print("\n")

enter = input("Please enter your message: ")
caesar(enter)