473,606 Members | 2,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

python cipher using a dictionary

6 New Member
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("Ente r 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 10197
bvdet
2,851 Recognized Expert Moderator Specialist
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("Ente r 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 Recognized Expert Contributor
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 Recognized Expert Contributor
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.maketran s() 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 Recognized Expert Contributor
Nice idea but I still like string.maketran s() 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.translat e() 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
3578
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 respectively In the sample code you see that I have class Item and class Dict class Dict contains a dictionary called items. The items dictionary will contain instances of Item that are keyed off of the Item name. In __main__ I create two...
7
1505
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? Sincerely Yours, pujo
8
2906
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, encapsulated properties. SOme of those are calculated when get is called. These kind of properties are actually Cell classes which have a Calculation class attached. All Cell properties are stored in a Dictionary <nameEnum, Cell>
14
28284
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
1167
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*/ struct Vector{ double x,y,z;
3
4932
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 "D:/softwares/Apache2.2/htdocs"> AddHandler mod_python .py PythonHandler mptest PythonDebug On </Directory>
3
1675
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
2391
by: hidrkannan | last post by:
aDict = {} aDict = 5 aDict = 'st' aDict = 12345 def outputMethod(**aDict): print aDict print aDict
6
10166
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 are 2 parts, I don't know how to setup the get/sets. I tried searching but could not find any examples. I'd appreciate an example of how to get/ set the two parts of a dictionary via a property of a class. tia
0
8015
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7951
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8439
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
6770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5966
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3930
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2448
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1553
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.