Encryption and Decryption from a dictionary.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Netwatcher
    New Member
    • Sep 2008
    • 58

    Encryption and Decryption from a dictionary.

    well, i started messing around with dictionaries, yet, most of the pages i found about them always talk about getting only one word out of it and turning it vice versa, i've been playing with that code for a few hours:
    Code:
    #dics
    Encryption={',':'hy{;','  ':'h4x0r2','':'zomg','?':'bko','a':'ika','b':'d0v','c':'ino', 'd':'maw', 'e':'aon', 'f':'que', 'g':'kip', 'h':'an[', 'i':'bf}', 'j':'ana&', 'k':'%d#', 'l':'d^f', 'm':'[d:w]', 'n':'ko[p', 'o':'{par:', 'p':'nt|;', 'q':'bz&$', 'r':'le{}', 's':'ak+', 't':'joq%', 'u':'f%(', 'v':'@!', 'w':'hg^*', 'x':'yu#', 'y':'fy%s', 'z':'mos@'}
    Decryption=dict(zip(Encryption.values(), Encryption.keys()))
    
    #functions#
    
    
    
    # f1 enc #
    def encrypt(x):
        try:
            #open spot for output#
            lol=""
            ##
            for letter in x:
                lol += Encryption[letter]
            print lol
        except KeyError:
                print 'These keys are not in the dictionary!'
            
    
    # f2 dec #
    def decrypt(y):
        try:
            lol=""
            for mail in y:
                lol += Decryption[mail]
            print lol
        except KeyError:
            print 'These keys are not in the dictionary!'
    
    #men#
    def menu():
        while 1:
            t=raw_input('Do you want to Encrypt or to Decrypt?[e,d] ').lower()
    #case 1#
    #Enc#
            while t=='e':
                try:
                    print 'Enter Text below'
                    x=raw_input('').lower()
                    if len(x)<10:
                        print 'Print only long sentences' 
    
                    else:
                        encrypt(x)
                except KeyError:
                    print 'These keys are not in the dictionary!'
    #case 2#
    #Dec#
            while t=='d':
                try:
                    print 'Enter Text below'
                    y=raw_input('').lower()
                    decrypt(y)
                except KeyError:
                    print 'These keys are not in the dictionary!'
    #b2men#
    menu()
    trying to get an output from the reversed dictionary is just not working, keeps giving me KeyErrors, even i enter only 1 word that is IN the dictionary...
    maybe it's because didn't use conventional letters, but still, how do i decrypt?
    plz help
    #Note: the Encryption works perfectly, the Decryption Doesn't.
    I'm using python 2.5 (python 3 is weird O|o)
    Last edited by Netwatcher; Sep 30 '08, 06:34 AM. Reason: Grammeration (XD)
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You are iterating on the string in decrypt(). That means you are considering only one letter at a time. Try this instead:
    Code:
    def decrypt(y):
        for key in Decryption:
            if key in y:
                while True:
                    try:
                        i = y.index(key)
                        y = y[:i]+Decryption[key]+y[i+len(key):]
                    except:
                        break
        return y
    Example:
    Code:
    >>> s = encrypt('temple of doom')
    >>> s
    'joq%aon[d:w]nt|;d^faonh4x0r2{par:queh4x0r2maw{par:{par:[d:w]'
    >>> decrypt(s)
    'temple of doom'
    >>>

    Comment

    • Netwatcher
      New Member
      • Sep 2008
      • 58

      #3
      Oh, so this is how it's done...
      the return is bugging it =/ changed it to print

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by Netwatcher
        Oh, so this is how it's done...
        the return is bugging it =/ changed it to print
        There probably is a better way to do it. :)

        I generally prefer to return an object from a function then format the output as required. Using an IDE makes testing code much easier. In your case:
        Code:
        s = encrypt('temple of doom')
        print s
        print decrypt(s)

        Comment

        • Netwatcher
          New Member
          • Sep 2008
          • 58

          #5
          i need to decrypt binary, which example will work in that case?
          is there a shortcut to create a Bin dictionary?

          Comment

          • Netwatcher
            New Member
            • Sep 2008
            • 58

            #6
            Originally posted by Netwatcher
            i need to decrypt binary, which example will work in that case?
            is there a shortcut to create a Bin dictionary?


            Oh never-mind, i got it working:D,
            now i just need to enter all the uppercase letters and symbols into the dictionary :p

            Comment

            Working...