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
```
You might also like to view...
Job requirements for PC technicians frequently have a weight lifting requirement of 100 pounds
Indicate whether the statement is true or false
Most encryption algorithms are ____________________, which encrypt data in single "chunks" of a certain length at a time.
Fill in the blank(s) with the appropriate word(s).
Offline files and folders are available on which versions of Windows? (Select TWO)
A. Windows 7 Ultimate B. Windows 7 Home Premium C. Windows XP Home D. Windows Vista Home Premium 64-bit E. Windows XP Professional
Prices for computer projection devices are US$199 and up, with ____________________ projectors costing a bit more than LCD projectors with similar features.
Fill in the blank(s) with the appropriate word(s).