A company that transmits data over the telephone is con- cerned that its phones could be tapped. All its data is transmitted as four-digit int values. The company has asked you to write an application that encrypts its data so that the data may be transmitted more securely. Encryption is the process of transforming data for security reasons. Your application should read a four-digit integer input by the user in a JTextField and encrypt the information as described in the steps of this exercise when the user clicks the Encrypt JButton (Fig. 6.35).
```
a) Copying the template to your working directory. Copy the C:Examples Tutorial06ExercisesEncryption directory to your C:SimplyJava directory.
b) Opening the template file. Open the Encryption.java file in your text editor.
c) Coding the Encrypt JButton’s ActionPerformed event handler. Add the code for Steps d through f to event handler encryptJButtonActionPerformed (lines 138–141).
d) Obtaining the user input. In line 140, insert a statement that obtains the user input from numberJTextField, converts it to an int and assigns the value to int variable number.
e) Extracting the digits from the user input. Use the programming techniques you used to solve Exercise 5.17 to insert statements that extract the digits from int variable number. Store the digits of number in the int variables digit1, digit2, digit3 and digit4, respectively.
f) Encrypt each digit and display the encrypted results. Replace each digit by perform- ing the calculation (the sum of that dig
```
1 // Encryption.java
2 // Encrypts data given by user.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class Encryption extends JFrame
8 {
9 // JLabel and JTextField for inputting number
10 private JLabel numberJLabel;
11 private JTextField numberJTextField;
12
13 // JLabel and JTextFields for displaying encrypted results
14 private JLabel encryptedResultsJLabel;
15 private JTextField encryptedDigit1JTextField;
16 private JTextField encryptedDigit2JTextField;
17 private JTextField encryptedDigit3JTextField;
18 private JTextField encryptedDigit4JTextField;
19
20 // JButton to initiate encryption
21 private JButton encryptJButton;
22
23 // no-argument constructor
24 public Encryption()
25 {
26 createUserInterface();
27 }
28
29 // create and position GUI components; register event handlers
30 private void createUserInterface()
31 {
32 // get content pane for attaching GUI components
33 Container contentPane = getContentPane();
34
35 // enable explicit positioning of GUI components
36 contentPane.setLayout( null );
37
38 // set up numberJLabel
39 numberJLabel = new JLabel();
40 numberJLabel.setBounds( 10, 10, 140, 25 );
41 numberJLabel.setText( "Enter 4-digit integer:" );
42 contentPane.add( numberJLabel );
43
44 // set up numberJTextField
45 numberJTextField = new JTextField();
46 numberJTextField.setBounds( 145, 10, 95, 25 );
47 contentPane.add( numberJTextField );
48 numberJTextField.addKeyListener(
49
50 new KeyAdapter() // anonymous inner class
51 {
52 // event handler called when key pressed
53 // in numberJTextField
54 public void keyPressed( KeyEvent event )
55 {
56 numberJTextFieldKeyPressed( event );
57 }
58
59 } // end anonymous inner class
60
61 ); // end call to addKeyListener
62
63 // set up encryptedResultsJLabel
64 encryptedResultsJLabel = new JLabel();
65 encryptedResultsJLabel.setBounds( 10, 45, 140, 25 );
66 encryptedResultsJLabel.setText( "Encrypted digits:" );
67 contentPane.add( encryptedResultsJLabel );
68
69 // set up encryptedDigit1JTextField
70 encryptedDigit1JTextField = new JTextField();
71 encryptedDigit1JTextField.setBounds( 145, 45, 20, 25 );
72 encryptedDigit1JTextField.setHorizontalAlignment(
73 JTextField.CENTER );
74 encryptedDigit1JTextField.setEditable( false );
75 contentPane.add( encryptedDigit1JTextField );
76
77 // set up encryptedDigit2JTextField
78 encryptedDigit2JTextField = new JTextField();
79 encryptedDigit2JTextField.setBounds( 170, 45, 20, 25 );
80 encryptedDigit2JTextField.setHorizontalAlignment(
81 JTextField.CENTER );
82 encryptedDigit2JTextField.setEditable( false );
83 contentPane.add( encryptedDigit2JTextField );
84
85 // set up encryptedDigit3JTextField
86 encryptedDigit3JTextField = new JTextField();
87 encryptedDigit3JTextField.setBounds( 195, 45, 20, 25 );
88 encryptedDigit3JTextField.setHorizontalAlignment(
89 JTextField.CENTER );
90 encryptedDigit3JTextField.setEditable( false );
91 contentPane.add( encryptedDigit3JTextField );
92
93 // set up encryptedDigit4JTextField
94 encryptedDigit4JTextField = new JTextField();
95 encryptedDigit4JTextField.setBounds( 220, 45, 20, 25 );
96 encryptedDigit4JTextField.setHorizontalAlignment(
97 JTextField.CENTER );
98 encryptedDigit4JTextField.setEditable( false );
99 contentPane.add( encryptedDigit4JTextField );
100
101 // set up encryptJButton and register its event handler
102 encryptJButton = new JButton();
103 encryptJButton.setText( "Encrypt" );
104 encryptJButton.setBounds( 250, 10, 100, 25 );
105 contentPane.add( encryptJButton );
106 encryptJButton.addActionListener(
107
108 new ActionListener() // anonymous inner class
109 {
110 // event handler called when encryptJButton is pressed
111 public void actionPerformed ( ActionEvent event )
112 {
113 encryptJButtonActionPerformed( event );
114 }
115
116 } // end anonymous inner class
117
118 ); // end call to addActionListener
119
120 // set properties of application’s window
121 setTitle( "Encryption" ); // set title bar text
122 setSize( 370, 110 ); // set window size
123 setVisible( true ); // display window
124
125 } // end method createUserInterface
126
127 // method called when key pressed in numberJTextField
128 private void numberJTextFieldKeyPressed( KeyEvent event )
129 {
130 // clear JTextFields for encrypted digits when new number input
131 encryptedDigit1JTextField.setText( "" );
132 encryptedDigit2JTextField.setText( "" );
133 encryptedDigit3JTextField.setText( "" );
134 encryptedDigit4JTextField.setText( "" );
135
136 } // end method numberJTextFieldKeyPressed
137
138 // method called when user clicks encryptJButton
139 private void encryptJButtonActionPerformed( ActionEvent event )
140 {
111 public void actionPerformed ( ActionEvent event )
112 {
113 encryptJButtonActionPerformed( event );
114 }
115
116 } // end anonymous inner class
117
118 ); // end call to addActionListener
119
120 // set properties of application’s window
121 setTitle( "Encryption" ); // set title bar text
122 setSize( 370, 110 ); // set window size
123 setVisible( true ); // display window
124
125 } // end method createUserInterface
126
127 // method called when key pressed in numberJTextField
128 private void numberJTextFieldKeyPressed( KeyEvent event )
129 {
130 // clear JTextFields for encrypted digits when new number input
131 encryptedDigit1JTextField.setText( "" );
132 encryptedDigit2JTextField.setText( "" );
133 encryptedDigit3JTextField.setText( "" );
134 encryptedDigit4JTextField.setText( "" );
135
136 } // end method numberJTextFieldKeyPressed
137
138 // method called when user clicks encryptJButton
139 private void encryptJButtonActionPerformed( ActionEvent event )
140 {
141 // retrieve number from numberJTextField
142 int number = Integer.parseInt( numberJTextField.getText() );
143
144 // extract individual digits
145 int digit4 = number % 10; // pick off last digit
146 number /= 10; // eliminate last digit
147
148 int digit3 = number % 10; // pick off last digit
149 number /= 10; // eliminate last digit
150
151 int digit2 = number % 10; // pick off last digit
152 number /= 10; // eliminate last digit
153
154 int digit1 = number; // pick off remaining digit 155
156 // encrypt each digit and display it
157 encryptedDigit1JTextField.setText(
158 String.valueOf( ( digit3 + 7 ) % 10 ) );
159 encryptedDigit2JTextField.setText(
160 String.valueOf( ( digit4 + 7 ) % 10 ) );
161 encryptedDigit3JTextField.setText(
162 String.valueOf( ( digit1 + 7 ) % 10 ) );
163 encryptedDigit4JTextField.setText(
164 String.valueOf( ( digit2 + 7 ) % 10 ) );
165
165
// retrieve number from numberJTextField
int number = Integer.parseInt( numberJTextField.getText() );
// extract individual digits
int digit4 = number % 10; // pick off last digit number /= 10; // eliminate last digit
int digit3 = number % 10; // pick off last digit number /= 10; // eliminate last digit
int digit2 = number % 10; // pick off last digit number /= 10; // eliminate last digit
int digit1 = number; // pick off remaining digit
166 } // end method encryptJButtonActionPerformed
167
168 // main method
169 public static void main( String args[] )
170 {
171 Encryption application = new Encryption();
172 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
173
174 } // end method main
175
176 } // end class Encryption
```
You might also like to view...
The capabilities of inputting and outputting strings in memory are known as:
a. String i/o manipulation. b. String stream processing. c. Character handling. d. Dynamic string operations
Different Web site design principles apply to the protected section than to the regular, open section of the site.
Answer the following statement true (T) or false (F)
Use the SQL _____________ keyword to perform regular expression matching.
a. LIKE b. REGEXP c. POSIX d. IS NULL
Embedding fonts in a presentation enables the font to be displayed on another computer regardless of whether or not the font is installed on that computer
Indicate whether the statement is true or false