471,338 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 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 9772
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

Post your reply

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

Similar topics

1 post views Thread by none | last post: by
7 posts views Thread by ajikoe | last post: by
3 posts views Thread by jitender001001 | last post: by
reply views Thread by rosydwin | last post: by

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.