473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Cipher/decipher program

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
  1. 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* '
  2. repl={}
  3. reverse_repl={}
  4. import sys
  5. #Generate replacement tables
  6. for str in foo.split("\n"):
  7.     parts=str.split(" = ")
  8.     repl[parts[0]]=parts[1]
  9.     repl[parts[0].lower()]=parts[1]
  10.     reverse_repl[parts[1]]=parts[0]
  11. def encipher():
  12.     outstr = ''
  13.     instr = sys.stdin.readline()
  14.     i = 0
  15.     for i in range(len(instr)):
  16.         c = instr[i]
  17.         if (instr[i:i+3]=='...'):
  18.             outstr+=repl['...']
  19.             i+=3
  20.         else:
  21.             if (c in repl):
  22.                 outstr+=repl[c]
  23.                 i+=1
  24.             else:
  25.                 outstr+=c
  26.                 i+=1
  27.     print outstr
  28. def decipher():
  29.     outstr = ''
  30.     instr = sys.stdin.readline()
  31.     instr=instr[:-1]
  32.     i = 0
  33.     c = instr[i]
  34.     while (i<len(instr)):
  35.             if (instr[i:i+2]):
  36.                 outstr+=reverse_repl[instr[i:i+2]]
  37.                 i+=2
  38.             else:
  39.                 outstr+=c
  40.                 i+=1
  41.     print outstr
  42. if (sys.argv[1]=="-e"):
  43.     encipher()
  44. else:
  45.     decipher()
  46. raw_input('press Return to end program>')
Couple of things I couldn't get working:
  1. 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 '.'
  2. 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 6718
bvdet
2,851 Expert Mod 2GB
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
  1.     for i in range(len(instr)):
  2.         c = instr[i]
  3.         if (instr[i:i+3]=='...'):
  4.             outstr+=repl['...']
  5.             i+=3
  6.         else:
  7.             if (c in repl):
  8.                 outstr+=repl[c]
  9.                 i+=1
  10.             else:
  11.                 outstr+=c
  12.                 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
  1.     while i < len(instr):
  2.         c = instr[i]
  3.         if instr[i:i+3] == '...':
  4.             outstr += repl['...']
  5.             i+=3
  6.         elif c in repl:
  7.             outstr += repl[c]
  8.             i+=1
  9.         else:
  10.             outstr += c*2
  11.             i+=1


To get the code to work, I replaced your code:
Expand|Select|Wrap|Line Numbers
  1.     while (i<len(instr)):
  2.             if (instr[i:i+2]):
  3.                 outstr+=reverse_repl[instr[i:i+2]]
  4.                 i+=2
  5.             else:
  6.                 outstr+=c
  7.                 i+=1
with the following code:
Expand|Select|Wrap|Line Numbers
  1.     while i<len(instr):
  2.         if reverse_repl.has_key(instr[i:i+2]):
  3.             outstr += reverse_repl[instr[i:i+2]]
  4.         else:
  5.             outstr += instr[i][0]
  6.         i+=2
There were several places in your code where parentheses were not required. Avoid using str as a variable name, because the built-in function str() will be masked.
HTH --BV
Dec 14 '08 #2
slurpz
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
  1. 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* '
  2. repl={}
  3. reverse_repl={}
  4. import sys
  5. #Generate replacement tables
  6. for str in foo.split("\n"):
  7.     parts=str.split(" = ")
  8.     repl[parts[0]]=parts[1]
  9.     repl[parts[0].lower()]=parts[1]
  10.     reverse_repl[parts[1]]=parts[0]
  11. def encipher():
  12.     outstr = ''
  13.     instr = sys.stdin.readline()
  14.     i = 0
  15.     n = len(instr)
  16.     while i < n:
  17.         c = instr[i] 
  18.         if instr[i:i+3] == '...': 
  19.             outstr += repl['...'] 
  20.             i+=3 
  21.         elif c in repl: 
  22.             outstr += repl[c] 
  23.             i+=1 
  24.         else: 
  25.             outstr += c
  26.             i+=1
  27.     print outstr
  28. def decipher():
  29.     outstr = ''
  30.     instr = sys.stdin.readline()
  31.     i = 0
  32.     n = len(instr)
  33.     while i < n:
  34.         if instr[i:i+2] in reverse_repl:
  35.             outstr+=reverse_repl[instr[i:i+2]]
  36.             i+=2
  37.         else:
  38.             outstr+=instr[i]
  39.             i+=1
  40.     print outstr
  41. if (sys.argv[1]=="-e"):
  42.     encipher()
  43. else:
  44.     decipher()
  45. 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
bvdet
2,851 Expert Mod 2GB
You should consider processing these special cases separately. Example:
Expand|Select|Wrap|Line Numbers
  1. dd = {'*right wing slash*': '.',
  2.       '*hand wave*': ',',
  3.       '*hand curve*': '"',
  4.       '*double hand jerk*': '!',
  5.       '*wing wave*': '?',
  6.       '*squiggly hand motion*': '...',
  7.       'xegy': 'I'}
  8.  
  9. def fix_str(s):
  10.     for key in dd:
  11.         s = s.replace(key, dd[key])
  12.     return s
Expand|Select|Wrap|Line Numbers
  1. >>> print fix_str("*right wing slash**hand wave**hand curve*")
  2. .,"
  3. >>> 
Dec 14 '08 #4
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

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

Similar topics

6
by: Michael Sparks | last post by:
Hi, I suspect this is a bug with AMK's Crypto package from http://www.amk.ca/python/code/crypto , but want to check to see if I'm being dumb before posting a bug report. I'm looking at...
4
by: Carl Harris | last post by:
I am trying to write some code to: 1.Prompt a user for filenames 2.Open the files 3.Convert my plain text into a cipher text array/string bear in mind I am a novice! I have wriiten some code...
7
by: Piotr Turkowski | last post by:
Hi! Here you can get some notes about Vigenere Cipher: http://raphael.math.uic.edu/~jeremy/crypt/vignere.html Here's whole code of my program, function stats() is in polish, so you can omit...
9
by: Piotr Turkowski | last post by:
Hi, The code is here: http://free.ud.pl/~piotr/data/vigenere.zip Its my program for decrypting and encrypting text. Shot polish course: szyfrowanie (szyfruj) - encrypting (text i want to code...
1
by: mkazek | last post by:
hi, where i can find source codes to encipher/decipher files with Hill's cipher and Transpositon Cipher?? (preferably vc++ source code)
2
by: Julio C. Hernandez Castro | last post by:
Dear all, We have just developped a new block cipher called Raiden, following a Feistel Network structure by means of genetic programming. Our intention now consists on getting as much feedback...
1
by: beetle17 | last post by:
Plaintext: a  n i c e  d a y Key: -3 Ciphertext: X  k f Z b  a X v Cipher will accept commands from the user and perform the operations required by the commands. There are three different...
16
by: Cawas | last post by:
Cipher Lab produces some terminals to collect data where we can program using one implementation of plain C which I believe to be ANSI C89 compatible, although not fully. They have their on set of...
4
by: wagn31 | last post by:
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:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.