decryption

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kholood
    New Member
    • Apr 2010
    • 2

    decryption

    hello
    I am trying to make python decrypt a long string into the message

    but it seems to be something wrong with my program


    I have not done any programming before
    and I hope that you help me to understand this one perfectly
    here is the quistion
    Test 1 - simple phrase, standard shift
    >>> decrypt('wkh txlfn eurzq ira mxpsv ryhu wkh odcb grj', 3)
    'the quick brown fox jumps over the lazy dog'


    and here what I have done so far
    Code:
    def decrypt(ciphertext, shift):
        """
        Decrypts some ciphertext encrypted with the Caesar cipher.  Uses the
        shift to do the decryption.
        """
    
        newtxt = 'wkh txlfn eurzq ira mxpsv ryhu wkh odcb grj'
        for i in range(len (newtxt)):
            if newtxt[i]== ciphertext:
                return newtxt [(i - shift )%len (newtxt)]
        return newtxt
    Last edited by bvdet; Apr 4 '10, 09:18 AM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Your code is not doing anything. newtxt is never equal to ciphertext. The shift can be accomplished using the built-in functions ord() and chr(). Strings are immutable, so you will have to build a new string. Here's a hint:
    Code:
    >>> s = "abcdef"
    >>> shift=3
    >>> "".join([chr(ord(letter)+shift) for letter in s])
    'defghi'
    >>>

    Comment

    Working...