package sonienkripsi; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import java.util.Base64; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import javax.swing.*; /** * * @author montox nox on Linux Mint source: * https://howtodoinjava.com/java/java-security/java-aes-encryption-example/ */ public class NoxAES_WIthKey extends javax.swing.JFrame { String KalimatAsli, RumusEnkrip, RumusDekrip; private static SecretKeySpec secretKey; private static byte[] key; public static void setKey(final String myKey) { MessageDigest sha = null; try { key = myKey.getBytes("UTF-8"); sha = MessageDigest.getInstance("SHA-1"); key = sha.digest(key); key = Arrays.copyOf(key, 16); secretKey = new SecretKeySpec(key, "AES"); } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { e.printStackTrace(); } } public static String encrypt(final String strToEncrypt, final String secret) { try { setKey(secret); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); return Base64.getEncoder() .encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8"))); } catch (Exception e) { System.out.println("Error while encrypting: " + e.toString()); } return null; } public static String decrypt(final String strToDecrypt, final String secret) { try { setKey(secret); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); cipher.init(Cipher.DECRYPT_MODE, secretKey); return new String(cipher.doFinal(Base64.getDecoder() .decode(strToDecrypt))); } catch (Exception e) { System.out.println("Error while decrypting: " + e.toString()); } return null; } /** * Creates new form NoxAES */ public NoxAES_WIthKey() { initComponents(); BtDekrip_.setVisible(false); jLabel4.setVisible(false); } private void BtEnkrip_ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: KalimatAsli = TextASLI_.getText(); final String secretKey = TextKunci_.getText(); RumusEnkrip = NoxAES_WIthKey.encrypt(KalimatAsli, secretKey); if (TextASLI_.getText().equals("") || TextKunci_.getText().equals("")) { JOptionPane.showMessageDialog(null, "Maaf...!!!\nTeks Kalimat Asli atau Kata Kunci Rahasia\nbelum di isi..."); TextASLI_.requestFocus(); } else { TextAES_.setText(RumusEnkrip); } } private void BtDekrip_ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: final String secretKey = TextKunci_.getText(); RumusDekrip = NoxAES_WIthKey.decrypt(RumusEnkrip, secretKey); } private void BtReset_ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: TextASLI_.setText(""); TextKunci_.setText(""); TextAES_.setText(""); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(NoxAES_WIthKey.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(NoxAES_WIthKey.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(NoxAES_WIthKey.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(NoxAES_WIthKey.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NoxAES_WIthKey().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton BtDekrip_; private javax.swing.JButton BtEnkrip_; private javax.swing.JButton BtReset_; private javax.swing.JTextArea TextAES_; private javax.swing.JTextArea TextASLI_; private javax.swing.JTextField TextKunci_; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane4; private javax.swing.JTextArea jTextArea1; // End of variables declaration }

Tidak ada komentar:
Posting Komentar