We can be trickier about how we encode text. Imagine that you (the sender) of the message and the receiver have a hidden sentence containing all the characters you might need in a coded message, like “The quick brown fox jumps over the lazy cat.” You can now just encode the position of the character you want in the picture (e.g., a “t” could be 0 and an “h” could be 1). You don’t even need six bits for this encoding. Rewrite the encoding and decoding function from the last exercise to use a hidden sentence for encoding.

Note: In the example the question gives, all the characters are encoded as their lower case values, and that’s how the answer below does it. However, one could leave this section out and still answer the spirit of the question.

```
def encodeMessage2(picture, message, codephrase):
codephrase = codephrase.lower()
#Go through all pixels and erase the lower two bits
for p in getPixels(picture):
# Clear out the red 2xLSB
r = getRed(p)
setRed(p,r-(r%4))
# Clear out the green 2xLSB
g = getGreen(p)
setGreen(p,g-(g%4))
# Clear out the blue 2xLSB
b = getBlue(p)
setBlue(p, b-(b%4))
pixels = getPixels(picture)
for charIndex in range(0, len(message)):
characterVal = (codephrase.find(message[charIndex].lower()))


#Convert the number into a binary representation
binaryValues = [0,0,0,0,0,0]
index = 5
while characterVal>0:
binaryValues[index] = ((characterVal%2))
characterVal = characterVal/2
index= index-1

#Encode the values into the picture
redVal = binaryValues[1]
redVal = redVal+ 2*binaryValues[0]

greenVal = binaryValues[3]
greenVal = greenVal+ 2*binaryValues[2]

blueVal = binaryValues[5]
blueVal = blueVal+ 2*binaryValues[4]

r = getRed(pixels[charIndex])
setRed(pixels[charIndex],r+redVal)
g = getGreen(pixels[charIndex])
setGreen(pixels[charIndex],g+greenVal)
b = getBlue(pixels[charIndex])
setBlue(pixels[charIndex],b+blueVal)

return picture

def decodeMessage2(picture, codephrase):
secretMessage = ""
charIndex = 0
for p in getPixels(picture):
r = getRed(p)%4
g = getGreen(p)%4
b = getBlue(p)%4

binaryValues = [0,0,0,0,0,0]

#Translate the values into binary
if not r==0:
if r==1:
binaryValues[1] = 1
elif r==2:
binaryValues[0] = 1
elif r==3:
binaryValues[0]=1
binaryValues[1]=1
if not g == 0:
if g==1:
binaryValues[3] = 1
elif g==2:
binaryValues[2] = 1
elif g==3:
binaryValues[2]=1
binaryValues[3]=1
if not b == 0:
if b==1:
binaryValues[5] = 1
elif b==2:
binaryValues[4] = 1
elif b==3:
binaryValues[4]=1
binaryValues[5]=1

binaryValues.reverse()

characterValue = 0
multiple = 1
for index in range(0, 6):
characterValue = characterValue+(multiple)*binaryValues[index]
multiple = multiple*2

secretMessage = secretMessage+ codephrase[characterValue]

return secretMessage
```

Computer Science & Information Technology

You might also like to view...

Threads execute in a ___________ fashion.

a. parallel b. serial c. complex d. none of the above

Computer Science & Information Technology

The Bergmanns are buying a new condo that is priced at $119,500. They will make a 20% down payment and finance the balance at 5.25% for 30 years. Determine the amount required for the down payment and the amount of their monthly payment. (Use the fixed-rate mortgage formula.) Please round your answers to the nearest cent. Down payment: $__________Monthly payment: $__________Using the monthly payment amount you have calculated , determine the total amount they will have paid for their condo in 30 years. Total amount: $__________

What will be an ideal response?

Computer Science & Information Technology