Open Cooltk63 opened 5 months ago
import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Base64;
public class AESEncryptionDecryptionApp {
private static final String ALGORITHM = "AES"; private static final String SECRET_KEY_STRING = "Tushar##248529"; public static SecretKey getSecretKeyFromString(String key) { // Pad the key to 16 bytes (128 bits) byte[] keyBytes = new byte[16]; byte[] keyBytesOriginal = key.getBytes(); // Copy the original key bytes into the padded key bytes System.arraycopy(keyBytesOriginal, 0, keyBytes, 0, Math.min(keyBytesOriginal.length, keyBytes.length)); return new SecretKeySpec(keyBytes, ALGORITHM); } public static String encrypt(String text) throws Exception { SecretKey secretKey = getSecretKeyFromString(SECRET_KEY_STRING); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encryptedBytes = cipher.doFinal(text.getBytes()); return Base64.getEncoder().encodeToString(encryptedBytes); } public static String decrypt(String encryptedText) throws Exception { SecretKey secretKey = getSecretKeyFromString(SECRET_KEY_STRING); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText)); return new String(decryptedBytes); } public static void main(String[] args) { JFrame frame = new JFrame("AES Encryption/Decryption"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 300); frame.setLayout(new GridLayout(6, 1)); JTextField textField = new JTextField(); JTextArea resultArea = new JTextArea(); JRadioButton encryptButton = new JRadioButton("Encrypt"); JRadioButton decryptButton = new JRadioButton("Decrypt"); ButtonGroup group = new ButtonGroup(); group.add(encryptButton); group.add(decryptButton); JButton executeButton = new JButton("Execute"); frame.add(new JLabel("Enter Text:")); frame.add(textField); frame.add(encryptButton); frame.add(decryptButton); frame.add(executeButton); frame.add(new JLabel("Result:")); frame.add(new JScrollPane(resultArea)); executeButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String text = textField.getText(); try { if (encryptButton.isSelected()) { String encryptedText = encrypt(text); resultArea.setText(encryptedText); } else if (decryptButton.isSelected()) { String decryptedText = decrypt(text); resultArea.setText(decryptedText); } } catch (Exception ex) { ex.printStackTrace(); resultArea.setText("Error: " + ex.getMessage()); } } }); frame.setVisible(true); }
}
Commit the issue
import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Base64;
public class AESEncryptionDecryptionApp {
}