CPT105 Introduction to Programming in Java 2021-2022 S1
Overview
Part A
In Part A, you are required to write a GUI program to simulate data encapsulation when we transfer data via the Internet. You should follow the description to produce the correct output. It is further divided into two parts: Part A.1 and A.2.
CW3 Part A will contribute to 35% of the total marks.
Part A.1 DataPacket (12 marks)
We usually transfer encrypted data packets via the Internet to protect the privacy of original data. Usually, an encrypted data packet is composed of several components: the frame header, frame tail, length of data and encrypted data. Table 1 below shows the detailed packet structure used in the question.
Table 1. Packet structure
Header Data length Encrypted data segment Tail
2 Bytes 1 Byte N Bytes 2 Bytes
In Table 1, we use 2 hexadecimal constants of “AAAA” and “BBBB” to represent the header and tail of a data packet (Header and Tail columns), respectively, one byte to represent the length of the encrypted data. We should encrypt the original data and encapsulate the encrypted data into the data segment and convert the data packet to a hexadecimal string before we send the data via the Internet. The procedure of the data encapsulation is as follows:
(1) Encrypt the data string into an encrypted string.
(2) Calculate the length of the encrypted string.
(3) Convert encrypted string into a hexadecimal value.
(4) Combine the header, data length, encrypted data and tail into a data packet.
For example, before we send a string “123” to a remote user via the Internet. The string should be encrypted and encapsulated into a data packet of a hexadecimal string “AAAA0c44464567552f507a6e4f513dBBBB”. “AAAA” and “BBBB” are the header and tail of the data packet, “0c” represents the length of encrypted data (12 bytes), “44464567552f507a6e4f513d” is the hexadecimal string of each character in the encrypted data “DFEgU/PznOQ=”. We defined a class “Encryption” to help you encrypt or decrypt strings as follows:
2 Please follow the steps below to complete the task:
(1) Create an empty java project. The project name should be “Resit_1”. (2) Create a class “Encryption” in your project.
- (a) Copy the code from the figure above into your Encryption class;
- (b) Change the encryption key to your student id.
(3) Create
- (a) Defineamethod“strToHex(Stringstr)”toconvertacommonstringinto
a hexadecimal string. For example, convert a string “123A” to
“31323341”.
- (b) Define a method “convertToDataPacket(String data)”, which can
encrypt a string into a new encrypted string and encapsulate it into a data packet and return a hexadecimal string, as Table 1 shows. For example, encrypt a string “123” into a data packet string “AAAA0c 44464567552f507a6e4f513dBBBB”.
- (c) Define a method “getDataFromDataPacket(String hexData)”, which can retrieve the original data from a hexadecimal string of a data packet. The method should check the header and tail of the packet and the length of the data. For example, retrieve the original data string “123” from the data packet “AAAA0c44464567552f507a6e4f5 13dBBBB”
a class “DataPacket”.
(4) Create
(a) Create a frame window as the following;
a class “Q1” and achieve the following functions in its main method.
(b) Create a DataPacket object in the main method;
(c) When users click “Data to packet” button, the string in the raw data
text field can be encrypted and showed in the encrypted data text field. The encrypted data can be encapsulated into a data packet string, and the data packet is shown in the data packet text field;
(d) When users click “Packet to data” button, it can retrieve the encrypted and original data strings from the data packet field and show them in the raw data and encrypted data text fields, respectively.
Part A.2 MultiDataPacket (23 marks)
Sometimes, we need to transfer several data within one data packet to improve data transmission efficiency. For example, if we send three strings of “ABC”, “EFG” and “HIJ” in one data packet, the hexadecimal value of the data packet is “AAAA0c685279554936796637696b3dBBBBAAAA0c61427a616c76474133366b3dBB BBAAAA0c5861346a4970775755756b3dBBBB”. The sub string of “AAAA0c68527955 4936796637696b3dBBBB” is the hexadecimal data of the first encrypted string “ABC”. The sub strings of “AAAA0c61427a616c76474133366b3dBBBB” and “AAAA0c5861346a4970775755756b3dBBBB” are the hexadecimal data for encrypted strings of “EFG” and “HIJ”, respectively. In addition, you should check the validation of the data packet before we retrieve data from it. If the data packet is not valid, you should throw an exception. Please follow the steps below to complete the task.
(1) Create an exception class “MyException”, which is inherited from “Exception”.
- (a) Create a constructor “MyException (String message)” to create an
exception object with a string of the exception message.
- (b) Override the method “toString()” to return the message string.
(2) Create a subclass “MultiDataPacket” based on the base class “DataPacket”.
- (a) Create an overloading method “String[] strToHex(String[] str)”, which can convert an array of common strings to an array of hexadecimal
strings.
- (b) Create an overloading method “String convertToHexDataPacket
(String[] data)”, which can convert an array of common strings to a MultiDataPacket with a single hexadecimal string.
4
(c) Create a new method “String[] getMultiDataFromHexDataPacket (String hexData)” to retrieve and return an array of strings from the MultiDataPacket string “hexData”. The method should validate the header, tail and length for each data packet in the string “hexData” and throw a “MyException” exception if any validation is failed.
(3) Create a class “ImgePanel”, which is inherited from class “JPanel”.
a. Define a public constructor to create an ImagePanel object with an
image file name.
b. Override the method “paintComponent (Graphics g)” to set a
background image for the ImagePanel object.
(4) Create a class “Q2” and achieve the following functions in its main method.
- (a) Create a frame window as below;
You should list your student Id on the top of the window, set a background image for the frame window, and set labels’ font size to 15 and font color to red. You can use any image you like.
- (b) Create a MultiDataPacket object in the main method;
- (c) CreateanImagePanelobjectwithabackgroundimageandaddittothe
Frame window as the main container of other components.
- (d) Users can input strings into the first 3 text fields ( raw data 1~raw data 3). When users click the “Convert to multi packet” button, strings in the 3 text fields can be encrypted and encapsulated into a hexadecimal
string and shown in the “multi data packet” text field.
- (e) When “Retrive data” button is clicked, users can retrieve all the separate data from the “multi data packet” text field and show them in
the first 3 text fields (Raw data 1~Raw data 3), respectively.
- (f) You should use a message dialogue to show any exceptions raised in
your program. Below is an example for your reference.