\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% Differential \newcommand\dd[2][]{\mathrm{d}^{#1}{#2}} % use as \dd, \dd{x}, or \dd[2]{x}\\% Poor man's siunitx \newcommand\unit[1]{\mathrm{#1}} \newcommand\num[1]{#1} \newcommand\qty[2]{#1~\unit{#2}}\\\newcommand\per{/} \newcommand\squared{{}^2} \newcommand\cubed{{}^3} % % Scale \newcommand\milli{\unit{m}} \newcommand\centi{\unit{c}} \newcommand\kilo{\unit{k}} \newcommand\mega{\unit{M}} % % Percent \newcommand\percent{\unit{\%}} % % Angle \newcommand\radian{\unit{rad}} \newcommand\degree{\unit{{}^\circ}} % % Time \newcommand\second{\unit{s}} \newcommand\s{\second} \newcommand\minute{\unit{min}} \newcommand\hour{\unit{h}} % % Distance \newcommand\meter{\unit{m}} \newcommand\m{\meter} \newcommand\inch{\unit{in}} \newcommand\foot{\unit{ft}} % % Force \newcommand\newton{\unit{N}} \newcommand\kip{\unit{kip}} % kilopound in "freedom" units - edit made by Sri % % Mass \newcommand\gram{\unit{g}} \newcommand\g{\gram} \newcommand\kilogram{\unit{kg}} \newcommand\kg{\kilogram} \newcommand\grain{\unit{grain}} \newcommand\ounce{\unit{oz}} % % Temperature \newcommand\kelvin{\unit{K}} \newcommand\K{\kelvin} \newcommand\celsius{\unit{{}^\circ C}} \newcommand\C{\celsius} \newcommand\fahrenheit{\unit{{}^\circ F}} \newcommand\F{\fahrenheit} % % Area \newcommand\sqft{\unit{sq\,\foot}} % square foot % % Volume \newcommand\liter{\unit{L}} \newcommand\gallon{\unit{gal}} % % Frequency \newcommand\hertz{\unit{Hz}} \newcommand\rpm{\unit{rpm}} % % Voltage \newcommand\volt{\unit{V}} \newcommand\V{\volt} \newcommand\millivolt{\milli\volt} \newcommand\mV{\milli\volt} \newcommand\kilovolt{\kilo\volt} \newcommand\kV{\kilo\volt} % % Current \newcommand\ampere{\unit{A}} \newcommand\A{\ampere} \newcommand\milliampereA{\milli\ampere} \newcommand\mA{\milli\ampere} \newcommand\kiloampereA{\kilo\ampere} \newcommand\kA{\kilo\ampere} % % Resistance \newcommand\ohm{\Omega} \newcommand\milliohm{\milli\ohm} \newcommand\kiloohm{\kilo\ohm} % correct SI spelling \newcommand\kilohm{\kilo\ohm} % "American" spelling used in siunitx \newcommand\megaohm{\mega\ohm} % correct SI spelling \newcommand\megohm{\mega\ohm} % "American" spelling used in siunitx % % Inductance \newcommand\henry{\unit{H}} \newcommand\H{\henry} \newcommand\millihenry{\milli\henry} \newcommand\mH{\milli\henry} % % Power \newcommand\watt{\unit{W}} \newcommand\W{\watt} \newcommand\milliwatt{\milli\watt} \newcommand\mW{\milli\watt} \newcommand\kilowatt{\kilo\watt} \newcommand\kW{\kilo\watt} % % Energy \newcommand\joule{\unit{J}} \newcommand\J{\joule} % % Composite units % % Torque \newcommand\ozin{\unit{\ounce}\,\unit{in}} \newcommand\newtonmeter{\unit{\newton\,\meter}} % % Pressure \newcommand\psf{\unit{psf}} % pounds per square foot \newcommand\pcf{\unit{pcf}} % pounds per cubic foot \newcommand\pascal{\unit{Pa}} \newcommand\Pa{\pascal} \newcommand\ksi{\unit{ksi}} % kilopound per square inch \newcommand\bar{\unit{bar}} \end{aligned}\end{align} \]

Mar 14, 2025 | 498 words | 5 min read

13.1.2. Task 2#

Learning Objectives#

  • Extract a hidden binary message from an image using the Least Significant Bit (LSB) technique.

  • Recover the ciphertext and plaintext message.

Task Instructions#

Save the flowcharts for each of your tasks in tp3_team_2_teamnumber.pdf. You will also need to include these flowcharts in your final report.

Step 1: Binary Extraction Function#

Review your function from Section 11.2.2 that extracts binary data from the least significant bit of each pixel in an image. Previously, the encoded message started at the first bit in the image. Now, the message will start at a random bit offset. Update your function if necessary to ensure it can extract the message regardless of any offset.

Step 2: Decryption Functions#

For each of the ciphers, Beaufort, Caesar, and Rail Fence, create a function that decrypts ciphertext into plaintext. The functions should take two arguments: the ciphertext message and the key. For the Caesar cipher, the key should be an integer representing the shift value. For Rail Fence, the key must be a positive integer representing the number of rails. These functions should return the decrypted plaintext message. Details on each cipher are available at Section 12.1.1. You may want to start from the encryption functions you created in Section 12.2.2 and Section 13.1.1. Test your functions by encrypting and decrypting messages using the same key.

Note

Teams of 2 only need to implement the Beaufort cipher. Teams of 3 should implement the Beaufort cipher and one other cipher (either Rail Fence or Caesar). Teams of 4 or more should implement all three ciphers.

Step 3: Main Function#

Create a main function that collects the following inputs from the user:

  • a choice of decryption method (Caesar, Rail Fence, or Beaufort)

  • a decryption key

  • the message start sequence

  • the message end sequence

  • the path to an input image file

The function should then:

  1. Extract the binary message from the input image using the function you updated in Step 1: Binary Extraction Function.

  2. Convert the binary to ciphertext. You can reuse the function you created in Section 11.2.3.

  3. Decrypt the ciphertext to plaintext using the chosen cipher and one of the functions you wrote for Step 2: Decryption Functions.

  4. Display the binary message, ciphertext, and plaintext to the user.

Use the files provided in the Table 13.5 to test your code.

Save your program as tp3_team_2_teamnumber.py.

Table 13.5 Image Files#

Image File Name

Cipher

Key

Start Sequence

End Sequence

Plaintext

ref_gry_r.png

Rail Fence

2

7

4

Blue Topaz

ref_col_r.png

Rail Fence

4

1

3

Emerald Green

ref_gry_b.png

Beaufort

glass

55

55

crystal

ref_col_b.png

Beaufort

Shine

333

334

diamond

ref_gry_c.png

Caesar

169

5

1

Red Ruby

ref_col_c.png

Caesar

777

40

04

Engineering

Sample Output#

Use the values in Table 13.6 below to test your program.

Table 13.6 Test Cases#

Case

cipher choice

cipher key

start_seq

end_seq

input image path

1

rail fence

2

7

4

ref_gry_r.png

2

rail fence

4

1

3

ref_col_r.png

3

beaufort

glass

55

55

ref_gry_b.png

4

beaufort

Shine

333

334

ref_col_b.png

5

caesar

169

5

1

ref_gry_c.png

6

caesar

777

40

04

ref_col_c.png

7

caesar

778

40

04

ref_col_c.png

8

caesar

777

99

04

ref_col_c.png

Ensure your program’s output matches the provided samples exactly. This includes all characters, white space, and punctuation. In the samples, user input is highlighted like this for clarity, but your program should not highlight user input in this way.

Case 1 Sample Output

$ python3 tp3_team_2_teamnumber.py Enter the cipher you want to use for encryption: rail fence Enter the key for the cipher: 2 Enter the start sequence: 7 Enter the end sequence: 4 Enter the path of the input image: ref_gry_r.png Extracted Binary Message: 01000010 01110101 00100000 01101111 01100001 01101100 01100101 01010100 01110000 01111010 Recovered Cipher Text: Bu oaleTpz Converted text: Blue Topaz

Case 2 Sample Output

$ python3 tp3_team_2_teamnumber.py Enter the cipher you want to use for encryption: rail fence Enter the key for the cipher: 4 Enter the start sequence: 1 Enter the end sequence: 3 Enter the path of the input image: ref_col_r.png Extracted Binary Message: 01000101 01100100 01101110 01101101 01101100 00100000 01100101 01100101 01100001 01000111 01100101 01110010 01110010 Recovered Cipher Text: Ednml eeaGerr Converted text: Emerald Green

Case 3 Sample Output

$ python3 tp3_team_2_teamnumber.py Enter the cipher you want to use for encryption: beaufort Enter the key for the cipher: glass Enter the start sequence: 55 Enter the end sequence: 55 Enter the path of the input image: ref_gry_b.png Extracted Binary Message: 01100101 00110100 01101101 01100001 00111001 01100111 01100001 Recovered Cipher Text: e4ma9ga Converted text: crystal

Case 4 Sample Output

$ python3 tp3_team_2_teamnumber.py Enter the cipher you want to use for encryption: beaufort Enter the key for the cipher: Shine Enter the start sequence: 333 Enter the end sequence: 334 Enter the path of the input image: ref_col_b.png Extracted Binary Message: 01110000 00111001 01101001 01100010 00110000 01100110 01100101 Recovered Cipher Text: p9ib0fe Converted text: diamond

Case 5 Sample Output

$ python3 tp3_team_2_teamnumber.py Enter the cipher you want to use for encryption: caesar Enter the key for the cipher: 169 Enter the start sequence: 5 Enter the end sequence: 1 Enter the path of the input image: ref_gry_c.png Extracted Binary Message: 01000101 01110010 01110001 00100000 01000101 01101000 01101111 01101100 Recovered Cipher Text: Erq Ehol Converted text: Red Ruby

Case 6 Sample Output

$ python3 tp3_team_2_teamnumber.py Enter the cipher you want to use for encryption: caesar Enter the key for the cipher: 777 Enter the start sequence: 40 Enter the end sequence: 04 Enter the path of the input image: ref_col_c.png Extracted Binary Message: 01000010 01101011 01100100 01100110 01101011 01100010 01100010 01101111 01100110 01101011 01100100 Recovered Cipher Text: Bkdfkbbofkd Converted text: Engineering

Case 7 Sample Output

$ python3 tp3_team_2_teamnumber.py Enter the cipher you want to use for encryption: caesar Enter the key for the cipher: 778 Enter the start sequence: 40 Enter the end sequence: 04 Enter the path of the input image: ref_col_c.png Extracted Binary Message: 01000010 01101011 01100100 01100110 01101011 01100010 01100010 01101111 01100110 01101011 01100100 Recovered Cipher Text: Bkdfkbbofkd Converted text: Dmfhmddqhmf

Case 8 Sample Output

$ python3 tp3_team_2_teamnumber.py Enter the cipher you want to use for encryption: caesar Enter the key for the cipher: 777 Enter the start sequence: 99 Enter the end sequence: 04 Enter the path of the input image: ref_col_c.png Start or end sequence not found in the image.

Table 13.7 Deliverables#

Deliverables

Description

tp3_team_2_teamnumber.py

Your completed Python code.

tp3_team_2_teamnumber.pdf

Flowchart(s) for this task.