954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

ceaser cypher

0
By avgprogramerjoe on Aug 16th, 2007 11:25 am

This code takes a string a cyphers it by shifting it's position in the alphabet. I've gotten it so it can pretty much take paragraphs. It's quite neat to play with.
For example the above when shifted to 5, would be:
Ymnx htij yfpjx f xywnsl f hDumjwx ny gD xmnkynsl ny,x utxnynts ns ymj fqumfgjy= N,Aj ltyyjs ny xt ny hfs uwjyyD rzhm yfpj ufwflwfumx= Ny,x vznyj sjfy yt uqfD Bnym=

import string
numbers = ""
x = 0
while x < 11:
    numbers = numbers + str(x)
    x += 1
shift = int(raw_input("Pick shift key from 1-9"))
choice = raw_input("would you like to encode or decode?")
letters = string.ascii_letters + string.punctuation + numbers
word = (raw_input("Please enter text"))
print letters
encoded = ''
if choice == "encode":
    for letter in word:
        if letter == ' ':
            encoded = encoded + ' '
        else:
            x = letters.index(letter) + shift
            encoded = encoded + letters[x]
if choice == "decode":
    for letter in word:
        if letter == ' ':
            encoded = encoded + ' '
        else:
            x = letters.index(letter) - shift
            encoded = encoded + letters[x]
print encoded

Removed [ code][ inlinecode] ... [ /inlinecode][ /code] tags.

Note, you could use
letters = string.ascii_letters + string.punctuation + string.digits

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

A caeser cypher with a shift of 13 is known as ROT13 just incase you didn't know that and it's the easiest cypher I know of to code besides an XOR

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You