440,715 Members | 748 Online Need help? Post your question and get tips & solutions from a community of 440,715 IT Pros & Developers. It's quick & easy.

# Cipher/decipher program

 P: 2 I'm trying to make a simple program that will convert to and from a cipher that's already set. Expand|Select|Wrap|Line Numbers foo = 'a = hz\nb = ye\nc = uo\nd = pd\ne = qi\nf = jy\ng = ru\nh = sw\ni = ln\nj = ae\nk = na\nl = rt\nm = wu\nn = is\no = nd\np = hw\nq = op\nr = kb\ns = vf\nt = fc\nu = xr\nv = ex\nw = mn\nx = gb\ny = bu\nz = ie\n. =  *right wing slash* \n, =  *hand wave* \n" =  *hand curve* \n! =  *double hand jerk* \n? =  *wing wave* \n... =  *squiggly hand motion* ' repl={} reverse_repl={} import sys #Generate replacement tables for str in foo.split("\n"):     parts=str.split(" = ")     repl[parts]=parts     repl[parts.lower()]=parts     reverse_repl[parts]=parts def encipher():     outstr = ''     instr = sys.stdin.readline()     i = 0     for i in range(len(instr)):         c = instr[i]         if (instr[i:i+3]=='...'):             outstr+=repl['...']             i+=3         else:             if (c in repl):                 outstr+=repl[c]                 i+=1             else:                 outstr+=c                 i+=1     print outstr def decipher():     outstr = ''     instr = sys.stdin.readline()     instr=instr[:-1]     i = 0     c = instr[i]     while (i') Couple of things I couldn't get working:I couldn't separate the '...' from the '.' The '...' will give the correct equivalent from the list when enciphering, but will also follow it with 2 instances of '.' For deciphering, it works up until it goes up against a character that's not in "foo" like 123 or a space. Thanks for your help in advance. Dec 14 '08 #1
4 Replies

 Expert Mod 2.5K+ P: 2,851 It appears that you want to replace each character with a unique 2 character string with a special exception for "...". I don't understand what *squiggly hand motion* is, but I'll assume that it is 2 characters. If a character is not in repl, double it so we be consistent. Your code: Expand|Select|Wrap|Line Numbers     for i in range(len(instr)):         c = instr[i]         if (instr[i:i+3]=='...'):             outstr+=repl['...']             i+=3         else:             if (c in repl):                 outstr+=repl[c]                 i+=1             else:                 outstr+=c                 i+=1 Variable i is updated each iteration. If you assign i in the body of the loop, you will lose the assignment. Try the following instead: Expand|Select|Wrap|Line Numbers     while i < len(instr):         c = instr[i]         if instr[i:i+3] == '...':             outstr += repl['...']             i+=3         elif c in repl:             outstr += repl[c]             i+=1         else:             outstr += c*2             i+=1 To get the code to work, I replaced your code: Expand|Select|Wrap|Line Numbers     while (i

 P: 2 Thanks for the quick reply. I actually worked a bit on my code while I waited so my code looks pretty similar to yours. My code looks like this right now: Expand|Select|Wrap|Line Numbers foo = 'a = hz\nb = ye\n... =  *squiggly hand motion* \nc = uo\nd = pd\ne = qi\nf = jy\ng = ru\nh = sw\nI = xegy\ni = ln\nj = ae\nk = na\nl = rt\nm = wu\nn = is\no = nd\np = hw\nq = op\nr = kb\ns = vf\nt = fc\nu = xr\nv = ex\nw = mn\nx = gb\ny = bu\nz = ie\n. =  *right wing slash* \n, =  *hand wave* \n" =  *hand curve* \n! =  *double hand jerk* \n? =  *wing wave* ' repl={} reverse_repl={} import sys #Generate replacement tables for str in foo.split("\n"):     parts=str.split(" = ")     repl[parts]=parts     repl[parts.lower()]=parts     reverse_repl[parts]=parts def encipher():     outstr = ''     instr = sys.stdin.readline()     i = 0     n = len(instr)     while i < n:         c = instr[i]          if instr[i:i+3] == '...':              outstr += repl['...']              i+=3          elif c in repl:              outstr += repl[c]              i+=1          else:              outstr += c             i+=1     print outstr def decipher():     outstr = ''     instr = sys.stdin.readline()     i = 0     n = len(instr)     while i < n:         if instr[i:i+2] in reverse_repl:             outstr+=reverse_repl[instr[i:i+2]]             i+=2         else:             outstr+=instr[i]             i+=1     print outstr if (sys.argv=="-e"):     encipher() else:     decipher() raw_input('press Return to end program>') Enciphering works perfectly (that "..." fix was great), but now with deciphering, there are a couple values that take up more than 2 letters. These include: *right wing slash* = . *hand wave* = , *hand curve* = " *double hand jerk* = ! *wing wave* = ? *squiggly hand motion* = ... xegy = I Is there a more efficient way than searching for each one individually? (Maybe from an array or something?) Dec 14 '08 #3

 Expert Mod 2.5K+ P: 2,851 You should consider processing these special cases separately. Example: Expand|Select|Wrap|Line Numbers dd = {'*right wing slash*': '.',       '*hand wave*': ',',       '*hand curve*': '"',       '*double hand jerk*': '!',       '*wing wave*': '?',       '*squiggly hand motion*': '...',       'xegy': 'I'}   def fix_str(s):     for key in dd:         s = s.replace(key, dd[key])     return s Expand|Select|Wrap|Line Numbers >>> print fix_str("*right wing slash**hand wave**hand curve*") .," >>>  Dec 14 '08 #4

 P: 9 2 digit encryption...the closest thing to this will be the BIFID cipher try reading this: http://en.wikipedia.org/wiki/Bifid_cipher it should give you a good idea on the algorithm Mar 2 '09 #5 