By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,795 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,795 IT Pros & Developers. It's quick & easy.

Encryption and Decryption from a dictionary.

P: 58
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:
Expand|Select|Wrap|Line Numbers
  1. #dics
  2. 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@'}
  3. Decryption=dict(zip(Encryption.values(), Encryption.keys()))
  4.  
  5. #functions#
  6.  
  7.  
  8.  
  9. # f1 enc #
  10. def encrypt(x):
  11.     try:
  12.         #open spot for output#
  13.         lol=""
  14.         ##
  15.         for letter in x:
  16.             lol += Encryption[letter]
  17.         print lol
  18.     except KeyError:
  19.             print 'These keys are not in the dictionary!'
  20.  
  21.  
  22. # f2 dec #
  23. def decrypt(y):
  24.     try:
  25.         lol=""
  26.         for mail in y:
  27.             lol += Decryption[mail]
  28.         print lol
  29.     except KeyError:
  30.         print 'These keys are not in the dictionary!'
  31.  
  32. #men#
  33. def menu():
  34.     while 1:
  35.         t=raw_input('Do you want to Encrypt or to Decrypt?[e,d] ').lower()
  36. #case 1#
  37. #Enc#
  38.         while t=='e':
  39.             try:
  40.                 print 'Enter Text below'
  41.                 x=raw_input('').lower()
  42.                 if len(x)<10:
  43.                     print 'Print only long sentences' 
  44.  
  45.                 else:
  46.                     encrypt(x)
  47.             except KeyError:
  48.                 print 'These keys are not in the dictionary!'
  49. #case 2#
  50. #Dec#
  51.         while t=='d':
  52.             try:
  53.                 print 'Enter Text below'
  54.                 y=raw_input('').lower()
  55.                 decrypt(y)
  56.             except KeyError:
  57.                 print 'These keys are not in the dictionary!'
  58. #b2men#
  59. menu()
  60.  
  61.  
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)
Sep 30 '08 #1
Share this Question
Share on Google+
5 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
You are iterating on the string in decrypt(). That means you are considering only one letter at a time. Try this instead:
Expand|Select|Wrap|Line Numbers
  1. def decrypt(y):
  2.     for key in Decryption:
  3.         if key in y:
  4.             while True:
  5.                 try:
  6.                     i = y.index(key)
  7.                     y = y[:i]+Decryption[key]+y[i+len(key):]
  8.                 except:
  9.                     break
  10.     return y
Example:
Expand|Select|Wrap|Line Numbers
  1. >>> s = encrypt('temple of doom')
  2. >>> s
  3. 'joq%aon[d:w]nt|;d^faonh4x0r2{par:queh4x0r2maw{par:{par:[d:w]'
  4. >>> decrypt(s)
  5. 'temple of doom'
  6. >>> 
Sep 30 '08 #2

P: 58
Oh, so this is how it's done...
the return is bugging it =/ changed it to print
Sep 30 '08 #3

bvdet
Expert Mod 2.5K+
P: 2,851
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:
Expand|Select|Wrap|Line Numbers
  1. s = encrypt('temple of doom')
  2. print s
  3. print decrypt(s)
Sep 30 '08 #4

P: 58
i need to decrypt binary, which example will work in that case?
is there a shortcut to create a Bin dictionary?
Oct 1 '08 #5

P: 58
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
Oct 1 '08 #6

Post your reply

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