i have this code .. but i want to build a java swing gui .. whichwill have a tex
ID: 3615182 • Letter: I
Question
i have this code .. but i want to build a java swing gui .. whichwill have a text box to input the password string and a gui spaceto display the output informationi am new to java gui.. can you create this sample gui for me ..when i understand this basic gui further changes to the gui as perrequirement i will do on my own. thanks
package org.temp2.cod1;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
/**
* This program generates a AES key, retrieves its rawbytes, and
* then reinstantiates a AES key from the keybytes.
* The reinstantiated key is used to initialize a AEScipher for
* encryption and decryption.
*/
public class AES1 {
/**
* Turns array of bytes into string
*
* @param buf Array ofbytes to convert to hex string
* @return Generated hexstring
*/
public static String asHex (byte buf[]){
StringBuffer strbuf = newStringBuffer(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++){
if (((int) buf[i] & 0xff)< 0x10)
strbuf.append("0");
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
}
public static void main(String[] args)throws Exception {
String message="This is justan example";
// Get the KeyGenerator
KeyGenerator kgen =KeyGenerator.getInstance("AES");
kgen.init(128); // 192 and 256bits may not be available
// Generate the secret keyspecs.
SecretKey skey =kgen.generateKey();
byte[] raw =skey.getEncoded();
SecretKeySpec skeySpec = newSecretKeySpec(raw, "AES");
// Instantiate the cipher
Cipher cipher =Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted =
cipher.doFinal((args.length == 0 ?
"This isjust an example" : args[0]).getBytes());
System.out.println("encryptedstring: " + asHex(encrypted));
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] original =
cipher.doFinal(encrypted);
String originalString = newString(original);
System.out.println("Originalstring: " +
originalString + "" + asHex(original));
}
}
Explanation / Answer
This isn't too difficult, as you may imagine: import java.awt.event.*; import java.awt.*; import javax.swing.*; import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; import java.io.*; public class AESGUI extends JPanel { public static void main(String[] args) { JFrame frame = newJFrame("AES Encryption"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setPreferredSize(newDimension(600,300)); frame.setLocationRelativeTo(null); frame.setResizable(false); AESGUI p = new AESGUI(); frame.getContentPane().add(p); frame.pack(); frame.setVisible(true); } private JTextField in; private JTextArea out; public AESGUI() { JLabel info = newJLabel("Type any String"); in = new JTextField(20); JButton encrypt = newJButton("Encrypt"); out = newJTextArea(10,40); out.setEditable(false); encrypt.addActionListener(newencryptListener()); in.addActionListener(newencryptListener()); add(info); add(in); add(encrypt); add(out); add(newJScrollPane(out)); } private class encryptListener implementsActionListener { public voidactionPerformed(ActionEvent e) { Stringdata = in.getText(); if(data.length() == 0) { } else try { String en =encrypt(data); out.append("Encryptedstring: " + en + " "); out.append("OriginalString: " + decrypt(en) + " "); } catch(Exception ex) { } } } public String asHex(byte[] buf) { StringBuffer strbuf = newStringBuffer(buf.length * 2); int i; for (i = 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.