Created
April 23, 2021 21:25
-
-
Save Da9el00/ee3c5c93f2ad8857ceb494bd90b2ce66 to your computer and use it in GitHub Desktop.
AES encryption using JAVA and JavaFX for GUI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.event.ActionEvent; | |
import javafx.fxml.FXML; | |
import javafx.scene.control.TextField; | |
import javax.crypto.BadPaddingException; | |
import javax.crypto.IllegalBlockSizeException; | |
import javax.crypto.NoSuchPaddingException; | |
import java.security.InvalidAlgorithmParameterException; | |
import java.security.InvalidKeyException; | |
import java.security.NoSuchAlgorithmException; | |
public class Controller { | |
Encryptor encryptor = new Encryptor(); | |
@FXML | |
private TextField keyTextField; | |
@FXML | |
private TextField inputTextField; | |
@FXML | |
private TextField outputTextField; | |
@FXML | |
void decryptButton(ActionEvent event) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException { | |
// Could be 128, 192, or 256 bits. | |
byte[] key = encryptor.stringToByteArray(keyTextField.getText()); | |
String input = inputTextField.getText(); | |
String encryptedString = encryptor.decrypt(input,key); | |
outputTextField.setText(encryptedString); | |
} | |
@FXML | |
void encryptButton(ActionEvent event) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException { | |
//Could be 128, 192, or 256 bits. | |
byte[] key = encryptor.stringToByteArray(keyTextField.getText()); | |
String input = inputTextField.getText(); | |
String encryptedString = encryptor.encrypt(input,key); | |
outputTextField.setText(encryptedString); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javax.crypto.*; | |
import javax.crypto.spec.IvParameterSpec; | |
import javax.crypto.spec.SecretKeySpec; | |
import java.security.InvalidAlgorithmParameterException; | |
import java.security.InvalidKeyException; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.Base64; | |
public class Encryptor { | |
//Advanced Encryption Standard | |
//128 bit | |
static byte[] IV = { 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; | |
public String encrypt(String input, byte[] secretKey) throws NoSuchPaddingException, NoSuchAlgorithmException, | |
InvalidAlgorithmParameterException, InvalidKeyException, | |
BadPaddingException, IllegalBlockSizeException { | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
SecretKeySpec key = new SecretKeySpec(secretKey,"AES"); | |
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV)); | |
byte[] cipherText = cipher.doFinal(input.getBytes()); | |
return Base64.getEncoder() | |
.encodeToString(cipherText); | |
} | |
public String decrypt(String cipherText, byte[] secretKey) throws NoSuchPaddingException, NoSuchAlgorithmException, | |
InvalidAlgorithmParameterException, InvalidKeyException, | |
BadPaddingException, IllegalBlockSizeException { | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
SecretKeySpec key = new SecretKeySpec(secretKey,"AES"); | |
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV)); | |
byte[] plainText = cipher.doFinal(Base64.getDecoder() | |
.decode(cipherText)); | |
return new String(plainText); | |
} | |
public byte[] stringToByteArray(String keyString){ | |
String[] keyFragments = keyString.split(" "); | |
byte[] key = new byte[16]; | |
for (int i = 0; i < keyFragments.length; i++) { | |
key[i] = Byte.parseByte(keyFragments[i]); | |
} | |
return key; | |
} | |
public static void main(String[] args) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException { | |
Encryptor encryptor = new Encryptor(); | |
//128 bit | |
byte[] encryptionKey = {65, 12, 12, 12, 12, 12, 12, 12, 12, | |
12, 12, 12, 12, 12, 12, 12 }; | |
String stringKey = "65 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12"; | |
byte[] key = encryptor.stringToByteArray(stringKey); | |
String input = "Secret"; | |
System.out.println(encryptor.encrypt(input,key)); | |
//output: VyEcl0pLeqQLemGONcik0w== | |
System.out.println(encryptor.decrypt("VyEcl0pLeqQLemGONcik0w==",key)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sample; | |
import javafx.application.Application; | |
import javafx.fxml.FXMLLoader; | |
import javafx.scene.Parent; | |
import javafx.scene.Scene; | |
import javafx.stage.Stage; | |
public class Main extends Application { | |
@Override | |
public void start(Stage primaryStage) throws Exception{ | |
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); | |
primaryStage.setTitle("Hello World"); | |
primaryStage.setScene(new Scene(root)); | |
primaryStage.show(); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<?import javafx.scene.control.Button?> | |
<?import javafx.scene.control.TextField?> | |
<?import javafx.scene.layout.AnchorPane?> | |
<?import javafx.scene.layout.VBox?> | |
<?import javafx.scene.text.Text?> | |
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" style="-fx-background-color: #EAF4D3;" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller"> | |
<children> | |
<VBox layoutX="111.0" layoutY="123.0" prefHeight="154.0" prefWidth="379.0" spacing="25.0"> | |
<children> | |
<Button mnemonicParsing="false" onAction="#encryptButton" prefHeight="33.0" prefWidth="398.0" text="Encrypt"> | |
<graphic> | |
<TextField fx:id="keyTextField" prefHeight="25.0" prefWidth="310.0" promptText="Key" text="65 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12" /> | |
</graphic> | |
</Button> | |
<Button mnemonicParsing="false" onAction="#decryptButton" prefHeight="33.0" prefWidth="394.0" text="Decrypt"> | |
<graphic> | |
<TextField fx:id="inputTextField" prefHeight="25.0" prefWidth="310.0" promptText="Input" /> | |
</graphic> | |
</Button> | |
<TextField fx:id="outputTextField" editable="false" promptText="Output" /> | |
</children> | |
</VBox> | |
<Text layoutX="110.0" layoutY="112.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Key" /> | |
<Text layoutX="110.0" layoutY="174.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Input" /> | |
<Text layoutX="110.0" layoutY="231.0" strokeType="OUTSIDE" strokeWidth="0.0" text="Output" /> | |
</children> | |
</AnchorPane> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment