| | |
Cypher Question
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2009
Posts: 2
Reputation:
Solved Threads: 0
I was wondering if someone could help me with modifying a program. I first must write a program that not only encrypts, but decrypts a message as well. This part I think I have figured out (although it is probabaly a little bloated) here it is:
( I hope I enclosed that right)
Anyway, now I have to modify it so that when we "drop off the end" of the alaphabet, the cypher shift goes in a circular fashion, ie: after z comes a again. I have found other solutions, but think I have gone a little over my head with my if/elif statements, and I can't get them to work properly.
Could someone point me in the right direction as to where to take this next, and also, (I feel dumb asking this), WHY would you want the shift to go in a circular fashion? Thanks for any and all advice
Ps. I am using Python 2.6.1, which came with the book, and have no prior programming experience.
Python Syntax (Toggle Plain Text)
#Cypher.py #A program that encrypts/decrypts a message import string def main(): print"This program will either encrypt or decrypt a message." mess = raw_input("Please enter your message: ") meth = raw_input("Enter 0 for encryption, 1 for decryption: ") while meth >= "2": print "Please enter 0 or 1 ONLY." meth = raw_input("Enter 0 for encryption, 1 for decryption: ") key = raw_input("What key index do you want to use: ") mesCode = [] if meth == "0": for ch in(mess): ch = chr(ord(ch)+ eval(key)) mesCode.append(ch) elif meth == "1": for ch in(mess): ch = chr(ord(ch) - eval(key)) mesCode.append(ch) print string.join(mesCode,"") main()
( I hope I enclosed that right)
Anyway, now I have to modify it so that when we "drop off the end" of the alaphabet, the cypher shift goes in a circular fashion, ie: after z comes a again. I have found other solutions, but think I have gone a little over my head with my if/elif statements, and I can't get them to work properly.
Could someone point me in the right direction as to where to take this next, and also, (I feel dumb asking this), WHY would you want the shift to go in a circular fashion? Thanks for any and all advice
Ps. I am using Python 2.6.1, which came with the book, and have no prior programming experience.
Last edited by BigPappyJappy; Mar 12th, 2009 at 4:53 pm.
You want to go into a circle so you get printable characters. Here is an example of Python's builtin rotation encoder, it starts in the middle of the 26 alpha character sets ...
python Syntax (Toggle Plain Text)
# example of simple encoding/decoding using Python's 'rot13' # alphabet is shifted by 13 positions to nopqrstuvwxyzabcdefghijklm # so original 'a' becomes 'n' or 'A' becomes 'N' and so on # (non-alpha characters are not affected) text = "Peanuts88-q" print "original =", text encoded = text.encode('rot13') print "encoded =", encoded decoded = encoded.encode('rot13') print "decoded =", decoded """ output --> original = Peanuts88-q encoded = Crnahgf88-d decoded = Peanuts88-q """
May 'the Google' be with you!
![]() |
Similar Threads
- how to make a good and active community (Social Media and Online Communities)
- Question about crypt function (PHP)
- acceleration and brakes in a car game (C++)
Other Threads in the Python Forum
- Previous Thread: Create lists of directories and files
- Next Thread: Cannot import pygame for python 2.6
| Thread Tools | Search this Thread |
Tag cloud for Python
ansi assignment avogadro backend beginner binary bluetooth character cmd code copy customdialog data decimals dictionary drive dynamic error examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime program programming progressbar projects push py2exe pygame pyglet pyqt python random recursion recursive refresh schedule script scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






