\[ \begin{align}\begin{aligned}\newcommand\blank{~\underline{\hspace{1.2cm}}~}\\% Bold symbols (vectors) \newcommand\bs[1]{\mathbf{#1}}\\% 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} \]

Oct 24, 2024 | 469 words | 5 min read

11.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_teamnumber.pdf. You will also need to include these flowcharts in your final report.

Step 1: Binary Extraction Function#

Review your function from Section 9.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, Vigenère, Caesar, and XOR, 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. These functions should return the decrypted plaintext message. Details on each cipher are available at Section 10.1.1. You may want to start from the encryption functions you created in Section 10.2.2 and Section 11.1.1. Test your functions by encrypting and decrypting messages using the same key.

Note

Teams of 2 only need to implement the Vigenère cipher. Teams of 3 should implement the Vigenère cipher and one other cipher (either XOR 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, XOR, or Vigenère)

  • 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 9.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 11.15 to test your code.

Save your program as tp3_team_2_teamnumber.py.

Table 11.15 Image Files#

Image File Name

Cipher

Key

Start Sequence

End Sequence

Plaintext

ref_gry_x.png

XOR

3RR0R

12

21

Sunny

ref_col_x.png

XOR

happy

44

77

ENGR 133

ref_gry_v.png

Vigenère

Up!

22

88

Boiler

ref_col_v.png

Vigenère

is fun

31

&&

Math 101

ref_gry_c.png

Caesar

159

5

1

Red Rose

ref_col_c.png

Caesar

777

40

04

Love ENGR

Sample Output#

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

Table 11.16 Test Cases#

Case

cipher choice

cipher key

start_seq

end_seq

input image path

1

xor

3RR0R

12

21

ref_gry_x.png

2

xor

happy

44

77

ref_col_x.png

3

vigenere

Up!

22

88

ref_gry_v.png

4

vigenere

is fun

31

&&

ref_col_v.png

5

caesar

159

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: xor Enter the key for the cipher: 3RR0R Enter the start sequence: 12 Enter the end sequence: 21 Enter the path of the input image: ref_gry_x.png Extracted Binary Message: 0110000000100111001111000101111000101011 Converted Binary Text: `'<^+ Converted text: Sunny

Case 2 Sample Output

$ python3 tp3_team_2_teamnumber.py Enter the cipher you want to use for encryption: xor Enter the key for the cipher: happy Enter the start sequence: 44 Enter the end sequence: 77 Enter the path of the input image: ref_col_x.png Extracted Binary Message: 0010110100101111001101110010001001011001010110010101001001000011 Converted Binary Text: -/7"YYRC Converted text: ENGR 133

Case 3 Sample Output

$ python3 tp3_team_2_teamnumber.py Enter the cipher you want to use for encryption: vigenere Enter the key for the cipher: Up! Enter the start sequence: 22 Enter the end sequence: 88 Enter the path of the input image: ref_gry_v.png Extracted Binary Message: 010101100110010001100011011001100111010001101100 Converted Binary Text: Vdcftl Converted text: Boiler

Case 4 Sample Output

$ python3 tp3_team_2_teamnumber.py Enter the cipher you want to use for encryption: vigenere Enter the key for the cipher: is fun Enter the start sequence: 31 Enter the end sequence: && Enter the path of the input image: ref_col_v.png Extracted Binary Message: 0101010101110011011011010110110100100000001101000011100000111001 Converted Binary Text: Usmm 489 Converted text: Math 101

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: 159 Enter the start sequence: 5 Enter the end sequence: 1 Enter the path of the input image: ref_gry_c.png Extracted Binary Message: 0101010101101000011001110010000001010101011100100111011001101000 Converted Binary Text: Uhg Urvh Converted text: Red Rose

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: 010010010110110001110011011000100010000001000010010010110100010001001111 Converted Binary Text: Ilsb BKDO Converted text: Love ENGR

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: 010010010110110001110011011000100010000001000010010010110100010001001111 Converted Binary Text: Ilsb BKDO Converted text: Knud DMFQ

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.