1
2 package samplecode;
3
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import javax.crypto.*;
8
9
10
11 /**
12 * <p>Title: Font Names </p>
13 * <p>Description: This program shows how Encrypt a message</p>
14 * <p>Company: Developer's super Page.com</p>
15 */
16
17
18 public class SampleFrame extends JFrame {
19 JPanel contentPane;
20 BorderLayout borderLayout1 = new BorderLayout();
21 JPanel jpnlMain = new JPanel();
22 JButton jButton1 = new JButton();
23 DefaultListModel model = new DefaultListModel();
24 JLabel jLabel1 = new JLabel();
25 JTextField jTextField1 = new JTextField();
26 JTextField jTextField2 = new JTextField();
27 //Construct the frame
28 public SampleFrame() {
29 enableEvents(AWTEvent.WINDOW_EVENT_MASK);
30 try {
31 jbInit();
32 }
33 catch(Exception e) {
34 e.printStackTrace();
35 }
36 }
37
38 //Component initialization
39 private void jbInit() throws Exception {
40 contentPane = (JPanel) this.getContentPane();
41 contentPane.setLayout(borderLayout1);
42 this.setSize(new Dimension(400, 300));
43 this.setTitle("Encrypt Message");
44 this.addHierarchyBoundsListener(new
45 SampleFrame_this_hierarchyBoundsAdapter(this));
46 jpnlMain.setLayout(null);
47 contentPane.setMinimumSize(new Dimension(300, 300));
48 contentPane.setPreferredSize(new Dimension(300, 256));
49 jpnlMain.setDebugGraphicsOptions(0);
50 jpnlMain.setMinimumSize(new Dimension(400, 256));
51 jpnlMain.setPreferredSize(new Dimension(400, 256));
52 jButton1.setBounds(new Rectangle(78, 63, 88, 36));
53 jButton1.setText("Encrypt");
54 jButton1.addActionListener(new SampleFrame_jButton1_actionAdapter(this));
55
56 jTextField1.setText("jTextField1");
57 jTextField1.setBounds(new Rectangle(40, 25, 201, 26));
58 jTextField2.setBounds(new Rectangle(42, 113, 201, 26));
59 jTextField2.setText("jTextField1");
60 contentPane.add(jpnlMain, BorderLayout.CENTER);
61 jpnlMain.add(jTextField1, null);
62 jpnlMain.add(jTextField2, null);
63 jpnlMain.add(jButton1, null);
64
65
66 }
67
68 //Overridden so we can exit when window is closed
69 protected void processWindowEvent(WindowEvent e) {
70 super.processWindowEvent(e);
71 if (e.getID() == WindowEvent.WINDOW_CLOSING) {
72 System.exit(0);
73 }
74 }
75
76
77 void jButton1_actionPerformed(ActionEvent e) {
78 //this function encrypt a message
79 try {
80 Cipher ecipher = Cipher.getInstance("Blowfish");
81
82 //Generates a Key
83 SecretKey key = KeyGenerator.getInstance("Blowfish").generateKey();
84
85 ecipher.init(Cipher.ENCRYPT_MODE, key);
86 // Encode the string into bytes using utf-8
87 byte[] utf8 = jTextField1.getText().getBytes("UTF8");
88
89 byte[] enc = ecipher.doFinal(utf8);
90
91 // Encode bytes to base64 to get a string
92 jTextField2.setText( new sun.misc.BASE64Encoder().encode(enc));
93 } catch (Exception e1) {
94 }
95
96 }
97
98 }
99
100 class SampleFrame_this_hierarchyBoundsAdapter extends
101 java.awt.event.HierarchyBoundsAdapter {
102 SampleFrame adaptee;
103
104 SampleFrame_this_hierarchyBoundsAdapter(SampleFrame adaptee) {
105 this.adaptee = adaptee;
106 }
107
108 }
109
110 class SampleFrame_jButton1_actionAdapter implements java.awt.event.ActionListener {
111 SampleFrame adaptee;
112
113 SampleFrame_jButton1_actionAdapter(SampleFrame adaptee) {
114 this.adaptee = adaptee;
115 }
116 public void actionPerformed(ActionEvent e) {
117 adaptee.jButton1_actionPerformed(e);
118 }
119 }
120
|