\[ \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 | 545 words | 5 min read

11.1.1. Task 1#

Learning Objectives#

  • Encrypt a plaintext into ciphertext.

  • Encode the ciphertext into an image at a specified bit offset.

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: Encryption Functions#

Similarly to the Vigenère cipher, you implemented in Section 10.2.2, create functions that use the XOR and Caesar cipher to encrypt a string. The functions should take two arguments: the plaintext message to be encrypted and the key. For the Caesar cipher, the key should be an integer representing the shift value. These functions should return the encrypted ciphertext message. You can find details on the XOR and Caesar ciphers in Section 10.1.1.

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 2: Encoding Function#

Update the encoding function you wrote in Section 10.2.3 to embed the binary message into an image, so that it can insert the binary message at a specified bit offset. For example, if the offset is 5, the function should start encoding the message after the 5th bit of the image.

Step 3: Main Function#

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

  • a choice of encryption method (Caesar, XOR, or Vigenère)

  • a plaintext message

  • an encryption key

  • the message start sequence

  • the message end sequence

  • the bit offset for encoding the message

  • the path to an input image file

  • the path to an output image file

  • the path to a comparison image file

The function should encrypt the message using the chosen cipher, add the start and end sequences to the message, then convert the encrypted message to binary, and encode it into the image at the specified bit offset.

If the encoding is not successful, the main function should display an error message and stop. Otherwise, it should display a success message to the user and save and display the modified image.

Finally, building on the image comparison function you wrote in Section 10.2.1, the main function should compare the modified image with the comparison image, displaying the resulting image difference and printing whether the images are the same or different.

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

Save your program as tp3_team_1_teamnumber.py.

Table 11.13 Image Files#

Image File Name

Cipher

Key

Plaintext

Start Sequence

End Sequence

Bit Offset

ref_gry.png

None

None

None

None

None

None

ref_col.png

None

None

None

None

None

None

ref_gry_x.png

XOR

3RR0R

Sunny

12

21

13

ref_col_x.png

XOR

happy

ENGR 133

44

77

18

ref_gry_v.png

Vigenère

Up!

Boiler

22

88

2

ref_col_v.png

Vigenère

is fun

Math 101

31

&&

15

ref_gry_c.png

Caesar

159

Red Rose

5

1

11

ref_col_c.png

Caesar

777

Love ENGR

40

04

9

Sample Output#

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

Table 11.14 Test Cases#

Case

cipher choice

plaintext

cipher key

start_seq

end_seq

bit offset

input image path

output image path

compare image path

1

xor

Sunny

3RR0R

12

21

13

ref_gry.png

gry_x.png

ref_gry_x.png

2

xor

ENGR 133

happy

44

77

18

ref_col.png

col_x.png

ref_col_x.png

3

vigenere

Boiler

Up!

22

88

2

ref_gry.png

gry_v.png

ref_gry_v.png

4

vigenere

Math 101

is fun

31

&&

15

ref_col.png

col_v.png

ref_col_v.png

5

caesar

Red Rose

159

5

1

11

ref_gry.png

gry_c.png

ref_gry_c.png

6

caesar

Love ENGR

777

40

04

9

ref_col.png

col_c.png

ref_col_c.png

7

caesar

Love ENGR

778

40

04

9

ref_col.png

col_c.png

ref_col_c.png

8

caesar

Love ENGR

777

40

04

25

ref_col.png

col_c.png

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_1_teamnumber.py Enter the cipher you want to use for encryption: xor Enter the plaintext you want to encrypt: Sunny Enter the key for the cipher: 3RR0R Enter the start sequence: 12 Enter the end sequence: 21 Enter the bit offset before you want to start encoding: 13 Enter the path of the input image: ref_gry.png Enter the path for your encoded image: gry_x.png Enter the path of the image you want to compare: ref_gry_x.png Encrypted Message using XOR Cipher: `'<^+ Binary output message: 00110001 00110010 01100000 00100111 00111100 01011110 00101011 00110010 00110001 Message successfully encoded and saved to: gry_x.png The images are the same.

Case_1_gry_x.png

Fig. 11.1 Case_1_gry_x.png#

Case_1_plot_diff_image.png

Fig. 11.2 Case_1_plot_diff_image.png#

Case_1_plot_gry_x.png

Fig. 11.3 Case_1_plot_gry_x.png#

Case 2 Sample Output

$ python3 tp3_team_1_teamnumber.py Enter the cipher you want to use for encryption: xor Enter the plaintext you want to encrypt: ENGR 133 Enter the key for the cipher: happy Enter the start sequence: 44 Enter the end sequence: 77 Enter the bit offset before you want to start encoding: 18 Enter the path of the input image: ref_col.png Enter the path for your encoded image: col_x.png Enter the path of the image you want to compare: ref_col_x.png Encrypted Message using XOR Cipher: -/7"YYRC Binary output message: 00110100 00110100 00101101 00101111 00110111 00100010 01011001 01011001 01010010 01000011 00110111 00110111 Message successfully encoded and saved to: col_x.png The images are the same.

Case_2_col_x.png

Fig. 11.4 Case_2_col_x.png#

Case_2_plot_col_x.png

Fig. 11.5 Case_2_plot_col_x.png#

Case_2_plot_diff_image.png

Fig. 11.6 Case_2_plot_diff_image.png#

Case 3 Sample Output

$ python3 tp3_team_1_teamnumber.py Enter the cipher you want to use for encryption: vigenere Enter the plaintext you want to encrypt: Boiler Enter the key for the cipher: Up! Enter the start sequence: 22 Enter the end sequence: 88 Enter the bit offset before you want to start encoding: 2 Enter the path of the input image: ref_gry.png Enter the path for your encoded image: gry_v.png Enter the path of the image you want to compare: ref_gry_v.png Encrypted Message using Vigenère Cipher: Vdcftl Binary output message: 00110010 00110010 01010110 01100100 01100011 01100110 01110100 01101100 00111000 00111000 Message successfully encoded and saved to: gry_v.png The images are the same.

Case_3_gry_v.png

Fig. 11.7 Case_3_gry_v.png#

Case_3_plot_diff_image.png

Fig. 11.8 Case_3_plot_diff_image.png#

Case_3_plot_gry_v.png

Fig. 11.9 Case_3_plot_gry_v.png#

Case 4 Sample Output

$ python3 tp3_team_1_teamnumber.py Enter the cipher you want to use for encryption: vigenere Enter the plaintext you want to encrypt: Math 101 Enter the key for the cipher: is fun Enter the start sequence: 31 Enter the end sequence: && Enter the bit offset before you want to start encoding: 15 Enter the path of the input image: ref_col.png Enter the path for your encoded image: col_v.png Enter the path of the image you want to compare: ref_col_v.png Encrypted Message using Vigenère Cipher: Usmm 489 Binary output message: 00110011 00110001 01010101 01110011 01101101 01101101 00100000 00110100 00111000 00111001 00100110 00100110 Message successfully encoded and saved to: col_v.png The images are the same.

Case_4_col_v.png

Fig. 11.10 Case_4_col_v.png#

Case_4_plot_col_v.png

Fig. 11.11 Case_4_plot_col_v.png#

Case_4_plot_diff_image.png

Fig. 11.12 Case_4_plot_diff_image.png#

Case 5 Sample Output

$ python3 tp3_team_1_teamnumber.py Enter the cipher you want to use for encryption: caesar Enter the plaintext you want to encrypt: Red Rose Enter the key for the cipher: 159 Enter the start sequence: 5 Enter the end sequence: 1 Enter the bit offset before you want to start encoding: 11 Enter the path of the input image: ref_gry.png Enter the path for your encoded image: gry_c.png Enter the path of the image you want to compare: ref_gry_c.png Encrypted Message using Caesar Cipher: Uhg Urvh Binary output message: 00110101 01010101 01101000 01100111 00100000 01010101 01110010 01110110 01101000 00110001 Message successfully encoded and saved to: gry_c.png The images are the same.

Case_5_gry_c.png

Fig. 11.13 Case_5_gry_c.png#

Case_5_plot_diff_image.png

Fig. 11.14 Case_5_plot_diff_image.png#

Case_5_plot_gry_c.png

Fig. 11.15 Case_5_plot_gry_c.png#

Case 6 Sample Output

$ python3 tp3_team_1_teamnumber.py Enter the cipher you want to use for encryption: caesar Enter the plaintext you want to encrypt: Love ENGR Enter the key for the cipher: 777 Enter the start sequence: 40 Enter the end sequence: 04 Enter the bit offset before you want to start encoding: 9 Enter the path of the input image: ref_col.png Enter the path for your encoded image: col_c.png Enter the path of the image you want to compare: ref_col_c.png Encrypted Message using Caesar Cipher: Ilsb BKDO Binary output message: 00110100 00110000 01001001 01101100 01110011 01100010 00100000 01000010 01001011 01000100 01001111 00110000 00110100 Message successfully encoded and saved to: col_c.png The images are the same.

Case_6_col_c.png

Fig. 11.16 Case_6_col_c.png#

Case_6_plot_col_c.png

Fig. 11.17 Case_6_plot_col_c.png#

Case_6_plot_diff_image.png

Fig. 11.18 Case_6_plot_diff_image.png#

Case 7 Sample Output

$ python3 tp3_team_1_teamnumber.py Enter the cipher you want to use for encryption: caesar Enter the plaintext you want to encrypt: Love ENGR Enter the key for the cipher: 778 Enter the start sequence: 40 Enter the end sequence: 04 Enter the bit offset before you want to start encoding: 9 Enter the path of the input image: ref_col.png Enter the path for your encoded image: col_c.png Enter the path of the image you want to compare: ref_col_c.png Encrypted Message using Caesar Cipher: Jmtc CLEP Binary output message: 00110100 00110000 01001010 01101101 01110100 01100011 00100000 01000011 01001100 01000101 01010000 00110000 00110100 Message successfully encoded and saved to: col_c.png The images are different.

Case_7_col_c.png

Fig. 11.19 Case_7_col_c.png#

Case_7_plot_col_c.png

Fig. 11.20 Case_7_plot_col_c.png#

Case_7_plot_diff_image.png

Fig. 11.21 Case_7_plot_diff_image.png#

Case 8 Sample Output

$ python3 tp3_team_1_teamnumber.py Enter the cipher you want to use for encryption: caesar Enter the plaintext you want to encrypt: Love ENGR Enter the key for the cipher: 777 Enter the start sequence: 40 Enter the end sequence: 04 Enter the bit offset before you want to start encoding: 25 Enter the path of the input image: ref_col.png Enter the path for your encoded image: col_c.png Enter the path of the image you want to compare: ref_col_c.png Given message is too long to be encoded in the image. Encrypted Message using Caesar Cipher: Ilsb BKDO Binary output message: 00110100 00110000 01001001 01101100 01110011 01100010 00100000 01000010 01001011 01000100 01001111 00110000 00110100