python cipher using a dictionary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wagn31
    New Member
    • Jun 2007
    • 6

    python cipher using a dictionary

    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()]
    Last edited by wagn31; Jun 21 '07, 10:12 PM. Reason: spelling correction
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by wagn31
    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:
    Code:
    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?
    Code:
    >>> import string
    >>> m = string.maketrans('abcdefghijklmnopqrstuvwxyz', 'cdefghijklmnopqrstuvwxyzab')
    >>> a = 'dhyek jfyts'
    >>> string.translate(a, m)
    'fjagm lhavu'
    >>>

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      Code:
      message="dhyek jfyts"
      key=3
      cipher=[ord(i) + key for i in message]
      print ''.join(map(chr,cipher))

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by ghostdog74
        Code:
        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.

        Comment

        • ghostdog74
          Recognized Expert Contributor
          • Apr 2006
          • 511

          #5
          Originally posted by ilikepython
          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.

          Comment

          Working...