Member Avatar for R3ap3R

'''
Hi there, I am totally new to this site as well as Python. Luckily Python is a not too hard programming language to start with.

I am trying to create a Caesar Cipher with ROT13 program that encodes user text ('a' - 'z' and 'A' - 'Z') to 13 (ROT13) places (where a turns to n...) and then also decodes the raw text to the original user input.

I have searched many forums and looked at online as well as textbook examples and I have come up with the following code (I am also not using external libraries):
'''

small =
crypalph = []

ROT13 = 13
for x in range(0,26):
crypalph.append(small[(x+ROT13)%26])

message = raw_input ("Please enter some text: \t")
cryptmessage =''


for x in message:
if small.count(x):
cryptmessage += crypalph[small.index(x.lower())]
else:
cryptmessage += x

print "__________________________________________"
print "\nEncrypted message: \t\t" + cryptmessage

print "__________________________________________"
raw_input ("\nPress [Enter] to decode '" + cryptmessage + "'")
message =''

for x in cryptmessage:
if small.count(x):
message += crypalph[small.index(x)]
else:
message += x

print "__________________________________________"
print "\nDecrypted message: \t\t" + message + '\n'

raw_input ("\n\nPress [Enter] to continue")

'''
The thing is that this program can only encode and decode lower case letters thus far. I want to know 2 things.

Firstly, can someone explain to me exactly what each line does because I only understand little things here and there.

Secondly, how can I also let the program encode Uppercase letters as well?

Help would be greatly appreciated!
'''

Member Avatar for R3ap3R
small = ['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']
crypalph = []

ROT13 = 13
for x in range(0,26):
  crypalph.append(small[(x+ROT13)%26])

message = raw_input ("Please enter some text: \t")
cryptmessage =''


for x in message:
  if small.count(x):
    cryptmessage += crypalph[small.index(x.lower())]
  else:
    cryptmessage += x

print "__________________________________________"
print "\nEncrypted message: \t\t" + cryptmessage

print "__________________________________________"
raw_input ("\nPress [Enter] to decode '" + cryptmessage + "'")
message =''

for x in cryptmessage:
  if small.count(x):
    message += crypalph[small.index(x)]
  else:
    message += x

print "__________________________________________"
print "\nDecrypted message: \t\t" + message + '\n'

raw_input ("\n\nPress [Enter] to continue")

This is very simple.

Extend the Small list with Uppercase letters. A-Z Just a-z
And on line 14. take away

x.lower()

Just put

x

And the job is done. I just tried it and it work fine.
Good job. :)

Member Avatar for R3ap3R

Thank you richieking
I have tried your method but it did not work for me, so I tried to edit the program a bit and now it looks like this:

alphabet = ['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','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']
crypalph = []

ROT13 = 13
for x in range(0,52):
  crypalph.append(alphabet[(x + ROT13) % 52])

#----------------------------------------------------------------Ask user input

user_input = raw_input ("Please enter some text: \t")

#-----------------------------------------------------Define blank cryptmessage

encryptmessage =''

#------------------------------------------------------------Encrypt user input

for x in user_input:
  if alphabet.count(x):
    encryptmessage += crypalph[alphabet.index(x)]
  else:
    encryptmessage += x

#------------------------------------------------------------------------------

print "__________________________________________"
print "\nEncrypted message: \t\t" + encryptmessage

print "__________________________________________"
raw_input ("\nPress [Enter] to decode '" + encryptmessage + "'")

#----------------------------------------------------------Define blank message

message =''

#-----------------------------------------------------------Decrypt the message

for x in encryptmessage:
  if alphabet.count(x):
    user_input += crypalph[alphabet.index(x)]
  else:
    user_input += x

#------------------------------------------------------------------------------

print "__________________________________________"
print "\nDecrypted message: \t\t" + user_input + '\n'

raw_input ("\n\nPress [Enter] to continue")

The problem now is that if the input is 'Abc' and the encoded part is 'Nop' (which is right), the decoded part prints out 'AbcaBC'. I have NO clue as why it does this. Can you please assist again? And if it is not much trouble, can you please describe what each line does in the program? exapmle:

for x in range(0,52):
  crypalph.append(alphabet[(x + ROT13) % 52])
for x in user_input:
  if alphabet.count(x):
    encryptmessage += crypalph[alphabet.index(x)]
  else:
    encryptmessage += x

Thank you!

Hay Job done.

Just had to rush this one for you. I have too much on me but i hope you will be happy with this. Well a litle upvoting will be great
:)

alphabet = ['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','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']
crypalph = []

ROT13 = 13
for x in range(0,52):
  crypalph.append(alphabet[(x + ROT13) % 52])

#----------------------------------------------------------------Ask user input

user_input = raw_input ("Please enter some text: \t").strip() # Clean the input

#-----------------------------------------------------Define blank cryptmessage

encryptmessage =''
sub=len(user_input) # Get the lenght tou use for slice

#------------------------------------------------------------Encrypt user input

for x in user_input:
  if x in alphabet:
    encryptmessage += crypalph[alphabet.index(x)]
  else:
    encryptmessage += x

#------------------------------------------------------------------------------

print "__________________________________________"
print "\nEncrypted message: \t\t" + encryptmessage

print "__________________________________________"
raw_input ("\nPress [Enter] to decode '" + encryptmessage + "'")

#----------------------------------------------------------Define blank message

message =''

#-----------------------------------------------------------Decrypt the message

for x in encryptmessage:
  if x in alphabet:
    user_input += crypalph[alphabet.index(x)]
  else:
    user_input += x

#------------------------------------------------------------------------------
fx=[]            # New list to Help Take the eccess data.
for w in user_input:  # Run Through to get each data
    for z in w:       # This step could be done oneliner But i thought you may want to
     fx.append(z)     # Now how everything works better
user_input=fx[:-sub]  #<-- This is were you slice the excess
user_input="".join(user_input)  #<-- Join stuff for out put
print "__________________________________________"
print "\nDecrypted message: \t\t" + user_input + '\n'

raw_input ("\n\nPress [Enter] to continue")

#OUT PUT
Please enter some text: 	Richie the King
__________________________________________

Encrypted message: 		evpuvr Gur XvAt
__________________________________________

Press [Enter] to decode 'evpuvr Gur XvAt'
__________________________________________

Decrypted message: 		Richie the King



Press [Enter] to continue
Member Avatar for R3ap3R

Thank you a lot richieking :)
I really do appreciate the effort!

You are welcome. Please close the thread as solved. PM me anytime.

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.