Member Avatar for leegeorg07

hi again after watching the numb3rs season 3 finale i wanted to try and make a program to encode or decode a paragraph i have attached a .txt file with the codings how should i start?

all help welcomed.

thanks in advance leegeorg07

Recommended Answers

All 4 Replies

Let me start by saying that the bacon cipher sounds awesome. I love bacon and I love ciphers, so combining them should result in a salty, meaty secret that only I could enjoy. That's the only thing I like about it though because the cipher doesn't sound terribly strong.

Anyway, it looks like the first thing you need to do is convert your plain text into the cipher text, and since this is a monoalphabetic cipher (see how smart I am?) you can do that with a dictionary.

keyring = { 'a':'aaaaa', 'b','aaaab' ...etc }

# Take in the plain text
plaintext = raw_input("Enter your plain text")

# now print out the cipher text
for eachletter in plaintext:
  print keyring[plaintext]," ",

You will probably want to put in some code to trim out newlines and other crap from the plaintext that the user enters.

Now I was reading on Wikipedia and I don't understand everything about the cipher. There was some crap about printing things in two different typefaces which you'll have to explain to me.

The wikipedia article talks about the two fonts because the cipher is designed to be used as steganography (hiding in plain sight).

First you take your coded message
I'm lazy so my message is "apple".

Then you baconize it:

AAAAA ABBBB ABBBB ABABB AABAA

Then you use the a's and b's to apply the two fonts.

For this medium I'll use uppercase to represent a B and lower case for an A (though this oversimplifies it)

my 'plain' message:
python is a language that makes programming fun.

now aligned with the bacon:

python is a language that makes programming fun.
AAAAAA BB B BABBBBAB ABBA ABAA

(It would be better if my plain message was the same length as the baconized code.)

and uppercasing the B's (though the 'true' example would use a different font.

python IS A LaNGUAgE tHAt mAkes programming fun.

The coding is in which font is used for each letter. You decode the message by decoding A and B for the different fonts and then un-baconize to get the coded message.

I'd like to have a little philosophical discussion about this recurrent question: "Hey, I have this problem, how should I start ?", or "I'm stuck, what should I do ?".
First question: are there valuable reasons to be stuck on a problem ? I think there are, and I see at least 2 of them:

1) Lack of knowledge: there exist libraries somewhere, which you don't know, and which contain essential tools to solve your problem. If you've never heard of these libraries, you're going to waste a lot of time reinventing the wheel. This situation is frequent because thousands of good libraries exist, and you usually know only a few of them. So it means that it's always important to dig the web or ask other people if they know libraries which can help you. It can save you a lot of efforts. However, here your problem is small, at first sight we may think that the regular expression module can help you, so if you know it well, you have an advantage, but may be your problem can be solved even without this module.

2) Design questions: when the problem is complex, there are many ways to organize it's solution, you will have to define a whole collection of classes and modules, and you must take some time choosing which classes you're going to define. On your problem, we could think of defining a function to encode a string or a function to decode a string, or, alternatively, we could think of defining a class, say Encoder, with methods to encode or decode string. Here again, the problem is small so this choice is not very important, and you can easily change the design of your program.

Summing up, you don't have any good reason to be stuck at your problem ! What should you do in this situation ? I think the answer is: think algorithmically!
It means at least 2 things:

1) List the most important tasks that your program should accomplish.
2) Try to decompose each of these task into smaller tasks which takes you closer to your goal (there might be different ways to do this, and you must make choices).

Then you can repeat the task decomposition strategy until you reach basic tasks which you can solve.

Python is very well adapted to this srategy because it's syntax is close to that of algorithmic pseudo code. So let's see how we could use this strategy using true python code. I don't pretend it gives a good program, but it helps you doing something when you're stuck and you don't know what to do. After this algorithmic study, you can always reorganize the code in a different way because you know a way to solve the problem.

I define 2 main tasks:
1) encode a paragraph into a string of As and Bs
2) decode a string of As and Bs into a paragraph.
For each of my main tasks, I write a python function, and in the function body, I write a possible decomposition of these tasks into smaller subtasks. Note that I can do this decomposition without having the slightest idea about how to solve the subtask, its a top-down process.

def encodeToAB(paragraph):
  # Task: encode a paragraph into a string of A and B s.
  paragraph = dropAllNonAlphabeticCharacters(paragraph)
  paragraph = convertToUpperCase(paragraph)
  paragraph = substituteEveryLetterByItsCode(paragraph)
  return paragraph

def decodeFromAB(paragraph):
  # Task: decode a baconized string of A and B as the original text.
  L = cutStringInPiecesOfLength5(paragraph)
  for i, item in enumerate(L):
    L[i] = getLetterForCode(item)
  return "".join(L)

Each of the subtask is achieved by a function call, and I write the function bodies with the same strategy of decomposing the task into subtasks: I'm no longer stuck on the problem !

def dropAllNonAlphabeticCharacters(paragraph):
  # we can use a regex for this task
  pattern = aRegexWhichMatchesGroupsOfNonAlphabeticCharacters()
  paragraph = pattern.sub("", paragraph)
  return paragraph

def convertToUpperCase(paragraph):
  # hey, I know how to do this!
  return paragraph.upper()

def substituteEveryLetterByItsCode(paragraph):
  L = []
  for letter in paragraph:
    L.append(getCodeForLetter(letter))
  return "".join(L)

This step defines new subtasks, which I implement in the same way

def aRegexWhichMatchesGroupsOfNonAlphabeticCharacters():
  # to solve this task, I must know the re module
  import re
  return re.compile(r"[^a-zA-Z]+")

def getCodeForLetter(letter):
  return dictionaryOfCodes[letter]

dictionaryOfCodes = {
  'A': 'AAAAA',
  ...
}

def cutStringInPiecesOfLength5(paragraph):
  raise Exception("Hey there is a code snippet that does just this at daniweb !")

def getLetterForCode(item):
  return reversedDictOfCodes[item]

reversedDictOfCodes = {
  "AAAAA": "A",
  ...
}

Etc. When I'm done with this algorithmic study, I can think about how to reorganize and improve the code. There are many possible improvements:
1) The dictionary of codes could be read from the file.
2) The reversed dictionary should be computed by the program instead of being written by hand
3) The regular expression should be compiled only once at global level and not each time the method aRegexWhichMatchesGroupsOfNonAlphabeticCharacters is called

Etc.

Ok, I didn't solve your problem, but I hope I gave you some clues about "I have this problem, how shoud I start ?". Let's all meditate :)

commented: Very nice! That's about how I've done it for years. (maybe not always in Python, but the process) +2

Mh, I will print that philosophy and try to get something out of it
Thanks Gribouillis

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.