(Simple Encryption Application) This application uses a simple technique to encrypt a number. Encryption is the process of modifying data so that only those intended to receive the data can undo the changes and view the original data. The user inputs in a JTextField the number to be encrypted—this is often called the “plain text” value. The application then multiplies the number by 7 and adds 5 to encrypt the original number—this new value is Tutorial 5 Enhancing the Inventory Application 75 often called the “cypher text” value. The application displays the encrypted number in an uneditable JTextField as shown in Fig. 5.32. A user who receives the encr
a) Copying the template to your working directory. Copy the C:Examples Tutorial05ExercisesSimpleEncryption directory to your C:SimplyJava directory.
b) Opening the template file. Open the SimpleEncryption.java file in your text editor. c) Coding the encryptJButtonActionPerformed event handler. Scroll to the event
handler encryptJButtonActionPerformed (lines 98–101). Insert a statement that
gets the number in the enterJTextField, converts the number to an int and assigns the value to int variable plainText. Next, insert a statement that multiplies the number stored in variable plainText by 7 and adds 5, then stores the result in int variable cypherText. Finally, insert a statement that displays the value of cypher- Text in encryptedJTextField.
d) Clearing the result when a new value is input by the user. Locate the event handler enterJTextFieldKeyPressed (it appears immediately after the event handler encryptJButtonActionPerformed) in the template so
```
1 // SimpleEncryption.java
2 // Application that uses a simple formula to encrypt numbers.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class SimpleEncryption extends JFrame
8 {
9 // JLabel and JTextField for entering number to encrypt
10 private JLabel enterJLabel;
11 private JTextField enterJTextField;
12
13 // JLabel and JTextField for displaying encrypted number
14 private JLabel encryptedJLabel;
15 private JTextField encryptedJTextField;
16
17 // JButton to initiate encryption
18 private JButton encryptJButton;
19
20 // no-argument constructor
21 public SimpleEncryption()
22 {
23 createUserInterface();
24 }
25
26 // create and position GUI components; register event handlers
27 private void createUserInterface()
28 {
29 // get content pane for attaching GUI components
30 Container contentPane = getContentPane();
31
32 // enable explicit positioning of GUI components
33 contentPane.setLayout( null );
34
35 // set up enterJLabel
36 enterJLabel = new JLabel();
37 enterJLabel.setText( "Enter number to encrypt:" );
38 enterJLabel.setBounds( 20, 20, 140, 20 );
39 contentPane.add( enterJLabel );
40
41 // set up enterJTextField
42 enterJTextField = new JTextField();
43 enterJTextField.setBounds( 180, 20, 50, 20 );
44 contentPane.add( enterJTextField );
45 enterJTextField.addKeyListener(
46
47 new KeyAdapter() // anonymous inner class
48 {
49 // called when key pressed in enterJTextField
50 public void keyPressed( KeyEvent event )
51 {
52 enterJTextFieldKeyPressed( event );
53 }
54
55 } // end anonymous inner class
56
57 ); // end call to addKeyListener
58
59 // set up encryptedJLabel
60 encryptedJLabel = new JLabel();
61 encryptedJLabel.setText( "Encrypted number:" );
62 encryptedJLabel.setBounds( 20, 50, 140, 20 );
63 contentPane.add( encryptedJLabel );
64
65 // set up encryptedJTextField
66 encryptedJTextField = new JTextField();
67 encryptedJTextField.setEditable( false );
68 encryptedJTextField.setBounds( 180, 50, 50, 20);
69 contentPane.add( encryptedJTextField );
70
71 // set up encryptJButton
72 encryptJButton = new JButton ();
73 encryptJButton.setText( "Encrypt" );
74 encryptJButton.setBounds( 250, 20, 80, 20 );
75 contentPane.add( encryptJButton );
76 encryptJButton.addActionListener(
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 } // end method createUserInterface
96
97 // encrypt number input by user
98 private void encryptJButtonActionPerformed ( ActionEvent event )
99 {
100 // read input value from enterJTextField
101 int plainText = Integer.parseInt( enterJTextField.getText() );
102
103 // to encrypt, multiply plainText by 7 and add 5
104 int cypherText = plainText * 7 + 5;
105
106 // display cypherText
107 encryptedJTextField.setText( String.valueOf( cypherText ) );
108
109 } // end method encryptJButtonActionPerformed
110
111 // clear encryptedJTextField because the value is now invalid
112 private void enterJTextFieldKeyPressed( KeyEvent event )
113 {
114 encryptedJTextField.setText( "" ); // clear encryptedJTextField
115
116 } // end method enterJTextFieldKeyPressed
117
118 //
119 public static void main( String args[] )
120 {
121 SimpleEncryption application = new SimpleEncryption();
122 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
123
124 } // end method main
125
126 } // end class SimpleEncryption
```
You might also like to view...
The named range from a selection for the VLOOKUP function has to use the top row of the table array
Indicate whether the statement is true or false
Images created using ____, a Mac OS X program for editing and displaying images, are created in Portable Network Graphics (PNG) format, which was developed for image use on the Internet.
A. Slide Sorter B. Format Picture C. Clip Art Gallery D. Preview
Briefly describe how to replace one word with another word that has the same meaning.
What will be an ideal response?
Which HDLC field is equivalent to the preamble in Ethernet?
A) Flag B) Address C) Control D) FCS