| 7 | import java.util.Base64; |
| 8 | |
| 9 | public class AESApplet extends Applet implements ActionListener { |
| 10 | |
| 11 | private TextField inputField; |
| 12 | private TextField keyField; |
| 13 | private Button encryptButton; |
| 14 | private Button decryptButton; |
| 15 | private TextArea outputArea; |
| 16 | |
| 17 | private static final String ALGORITHM = "AES"; |
| 18 | private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding"; |
| 19 | |
| 20 | public void init() { |
| 21 | setLayout(new BorderLayout()); |
| 22 | |
| 23 | Panel inputPanel = new Panel(new GridLayout(3, 2)); |
| 24 | inputPanel.add(new Label("Input:")); |
| 25 | inputField = new TextField(); |
| 26 | inputPanel.add(inputField); |
| 27 | inputPanel.add(new Label("Key:")); |
| 28 | keyField = new TextField(); |
| 29 | inputPanel.add(keyField); |
| 30 | encryptButton = new Button("Encrypt"); |
| 31 | encryptButton.addActionListener(this); |
| 32 | inputPanel.add(encryptButton); |
| 33 | decryptButton = new Button("Decrypt"); |
| 34 | decryptButton.addActionListener(this); |
| 35 | inputPanel.add(decryptButton); |
| 36 | add(inputPanel, BorderLayout.NORTH); |
| 37 | |
| 38 | outputArea = new TextArea(); |
| 39 | outputArea.setEditable(false); |
| 40 | add(outputArea, BorderLayout.CENTER); |
| 41 | |
| 42 | setSize(400, 300); |
| 43 | } |
| 44 | |
| 45 | public void actionPerformed(ActionEvent e) { |
| 46 | String key = keyField.getText(); |
| 47 | String input = inputField.getText(); |
| 48 | |
| 49 | try { |
| 50 | if (e.getSource() == encryptButton) { |
| 51 | String encryptedText = encrypt(key, input); |
| 52 | outputArea.setText("Encrypted Text:\n" + encryptedText); |
| 53 | } else if (e.getSource() == decryptButton) { |
| 54 | String decryptedText = decrypt(key, input); |
| 55 | outputArea.setText("Decrypted Text:\n" + decryptedText); |
| 56 | } |
| 57 | } catch (Exception ex) { |
| 58 | outputArea.setText("Error: " + ex.getMessage()); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | private String encrypt(String key, String input) throws Exception { |
| 63 | Cipher cipher = Cipher.getInstance(TRANSFORMATION); |
| 64 | SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM); |
| 65 | cipher.init(Cipher.ENCRYPT_MODE, secretKey); |
| 66 | byte[] encryptedBytes = cipher.doFinal(input.getBytes()); |
nothing calls this directly
no outgoing calls
no test coverage detected