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()]
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: - 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"}
-
-
def main():
-
print "This program will encode your messages using a Caesar Cipher"
-
print
-
message = raw_input("Enter the message: ")
-
codedMessage = ""
-
for ch in message:
-
codedMessage += cipher[ch]
-
print "The coded message is:", codedMessage
-
-
main()
Have you looked at translate() in the string module? - >>> import string
-
>>> m = string.maketrans('abcdefghijklmnopqrstuvwxyz', 'cdefghijklmnopqrstuvwxyzab')
-
>>> a = 'dhyek jfyts'
-
>>> string.translate(a, m)
-
'fjagm lhavu'
-
>>>
-
message="dhyek jfyts"
-
key=3
-
cipher=[ord(i) + key for i in message]
-
print ''.join(map(chr,cipher))
-
-
message="dhyek jfyts"
-
key=3
-
cipher=[ord(i) + key for i in message]
-
print ''.join(map(chr,cipher))
-
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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
|
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>
|
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]
|
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;
| |
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>
|
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..
|
by: hidrkannan |
last post by:
aDict = {}
aDict = 5
aDict = 'st'
aDict = 12345
def outputMethod(**aDict):
print aDict
print aDict
|
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
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |