473,372 Members | 1,175 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

python cipher using a dictionary

6
i need to use a cipher but I have to used the assigned code in the ciphering i know how to do it, but i am not sure how to add my own dictionary. Here is what i have so far:


[cipher = {"a":"g", "b":"h", "c":"i", "d":"j", "e":"k", "f":"l", "g":"m", "h":"n",
"i":"o", "j":"p", "k":"q", "l":"r", "m":"s", "n":"t", "o":"u", "p":"v",
"q":"w", "r":"x", "s":"y", "t":"z", "u":"a", "v":"b", "w":"c", "x":"d",
"y":"e", "z":"f"}
import string
def main():
print "This program will encode your messages using a Caesar Cipher"
print
key = cipher
message = raw_input("Enter the message: ")
codedMessage = ""
for ch in message:
codedMessage = codedMessage + chr(ord(cipher)+ key)
print "The coded message is:", codedMessage

main()]
Jun 21 '07 #1
4 10174
bvdet
2,851 Expert Mod 2GB
i need to use a cipher but I have to used the assigned code in the ciphering i know how to do it, but i am not sure how to add my own dictionary. Here is what i have so far:


[cipher = {"a":"g", "b":"h", "c":"i", "d":"j", "e":"k", "f":"l", "g":"m", "h":"n",
"i":"o", "j":"p", "k":"q", "l":"r", "m":"s", "n":"t", "o":"u", "p":"v",
"q":"w", "r":"x", "s":"y", "t":"z", "u":"a", "v":"b", "w":"c", "x":"d",
"y":"e", "z":"f"}
import string
def main():
print "This program will encode your messages using a Caesar Cipher"
print
key = cipher
message = raw_input("Enter the message: ")
codedMessage = ""
for ch in message:
codedMessage = codedMessage + chr(ord(cipher)+ key)
print "The coded message is:", codedMessage

main()]
I modified a few things:
Expand|Select|Wrap|Line Numbers
  1. cipher = {"a":"g", "b":"h", "c":"i", "d":"j", "e":"k", "f":"l", "g":"m", "h":"n", "i":"o", "j":"p", "k":"q", "l":"r", "m":"s", "n":"t", "o":"u", "p":"v", "q":"w", "r":"x", "s":"y", "t":"z", "u":"a", "v":"b", "w":"c", "x":"d", "y":"e", "z":"f"}
  2.  
  3. def main():
  4.     print "This program will encode your messages using a Caesar Cipher"
  5.     print
  6.     message = raw_input("Enter the message: ")
  7.     codedMessage = ""
  8.     for ch in message:
  9.         codedMessage += cipher[ch]
  10.     print "The coded message is:", codedMessage
  11.  
  12. main()
Have you looked at translate() in the string module?
Expand|Select|Wrap|Line Numbers
  1. >>> import string
  2. >>> m = string.maketrans('abcdefghijklmnopqrstuvwxyz', 'cdefghijklmnopqrstuvwxyzab')
  3. >>> a = 'dhyek jfyts'
  4. >>> string.translate(a, m)
  5. 'fjagm lhavu'
  6. >>> 
Jun 21 '07 #2
ghostdog74
511 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. message="dhyek jfyts"
  2. key=3
  3. cipher=[ord(i) + key for i in message]
  4. print ''.join(map(chr,cipher))
  5.  
Jun 22 '07 #3
ilikepython
844 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. message="dhyek jfyts"
  2. key=3
  3. cipher=[ord(i) + key for i in message]
  4. print ''.join(map(chr,cipher))
  5.  
Nice idea but I still like string.maketrans() better. One, it doesn't loop back to the beggining of the alphabet when it reaches letters like "z", "x", and "y". Also, it will replace everything in the message, including punctuation and symbols. As long, as you use the same way to bring it back it you're fine, however.
Jun 22 '07 #4
ghostdog74
511 Expert 256MB
Nice idea but I still like string.maketrans() better. One, it doesn't loop back to the beggining of the alphabet when it reaches letters like "z", "x", and "y". Also, it will replace everything in the message, including punctuation and symbols. As long, as you use the same way to bring it back it you're fine, however.
The proper and more secure way to do encryption is to use well known encryption algos such as blowfish, aes etc, certainly NOT by using string.translate() or the ord,chr method.
The solutions provided, are just for educational purposes and not to be implemented in production.
Jun 22 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: none | last post by:
or is it just me? I am having a problem with using a dictionary as an attribute of a class. This happens in python 1.5.2 and 2.2.2 which I am accessing through pythonwin builds 150 and 148...
7
by: ajikoe | last post by:
Hello, I have an idea to build python module to speed up python code in some of field where pyrex shines such as numeric, code which needs a lot of looping etc. What do you think? ...
8
by: Papa.Coen | last post by:
After repeatedly calling/using a Dictionary I get a StackOverflowError. What am I doing wrong? The situation is as follows: I have ruler class, this class contains some member variables,...
14
by: erikcw | last post by:
Hi, I'm trying to turn o list of objects into a dictionary using a list comprehension. Something like entries = {} = d.id] for d in links]
0
by: nimitsis | last post by:
Hello I am trying to convert and manage a simple structure C ,to Python by using C. The code is the following : /* example.c*/ double sum(Vector c) { return c.x+c.y+c.z; } /*header.h*/...
3
by: joe jacob | last post by:
I configured apache to execute python scripts using mod_python handler. I followed below mentioned steps to configure apache. 1. In http.conf I added <Directory...
3
by: jitender001001 | last post by:
hi all i m new to python and i have a problem of printing python variable using os.system() in bash !/usr/bin/env python var = "/home/anonymous" os.system("echo $var) it is not working..
21
by: hidrkannan | last post by:
aDict = {} aDict = 5 aDict = 'st' aDict = 12345 def outputMethod(**aDict): print aDict print aDict
6
by: GiJeet | last post by:
hello, I'm trying to use a dictionary as a class member. I want to use a property to get/set the key/value of the dictionary but I'm confused as how to use a dictionary as a property. Since there...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.