Skip to content

Instantly share code, notes, and snippets.

@Da9el00
Created April 27, 2021 19:54
Show Gist options
  • Save Da9el00/68f157ac07456aa3c051c814e24c44f3 to your computer and use it in GitHub Desktop.
Save Da9el00/68f157ac07456aa3c051c814e24c44f3 to your computer and use it in GitHub Desktop.
AES and TripleDES encryption using JAVA
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;
public class Controller {
EncryptorAES encryptorAES = new EncryptorAES();
EncryptorTripleDES encryptorTripleDES = new EncryptorTripleDES();
@FXML
private TextField keyTextField;
@FXML
private TextField inputTextField;
@FXML
private TextField outputTextField;
@FXML
private RadioButton aseRadio;
@FXML
private ToggleGroup encryptionDecryptionType;
@FXML
private RadioButton tripleDSERadio;
public Controller() throws InvalidKeySpecException, InvalidKeyException, NoSuchAlgorithmException {
}
@FXML
void decryptButton(ActionEvent event) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException {
String input = inputTextField.getText();
String key = keyTextField.getText();
String encryptedString = null;
if(encryptionDecryptionType.getSelectedToggle() == aseRadio){
encryptedString = encryptorAES.decrypt(input,key);
} else if(encryptionDecryptionType.getSelectedToggle() == tripleDSERadio){
encryptedString = encryptorTripleDES.decrypt(input,key);
}
outputTextField.setText(encryptedString);
}
@FXML
void encryptButton(ActionEvent event) throws NoSuchPaddingException, InvalidKeyException, NoSuchAlgorithmException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, UnsupportedEncodingException {
String key = keyTextField.getText();
String input = inputTextField.getText();
String encryptedString = null;
if(encryptionDecryptionType.getSelectedToggle() == aseRadio){
encryptedString = encryptorAES.encrypt(input,key);
} else if(encryptionDecryptionType.getSelectedToggle() == tripleDSERadio){
encryptedString = encryptorTripleDES.encrypt(input,key);
}
outputTextField.setText(encryptedString);
}
}
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 EncryptorAES {
//Advanced Encryption Standard
//128 bit
private final byte[] IV = { 0x01, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
public String encrypt(String input, String secretKey) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec key = new SecretKeySpec(stringToByteArray(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, String secretKey) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec key = new SecretKeySpec(stringToByteArray(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 {
EncryptorAES encryptorAES = new EncryptorAES();
//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";
String input = "Secret";
System.out.println(encryptorAES.encrypt(input,stringKey));
//output: VyEcl0pLeqQLemGONcik0w==
System.out.println(encryptorAES.decrypt("VyEcl0pLeqQLemGONcik0w==",stringKey));
}
}
package sample;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;
public class EncryptorTripleDES {
private final String transformation = "DESede/CBC/PKCS5Padding";
private String algorithm = "DESede";
public String encrypt(String input, String keyString) throws NoSuchAlgorithmException, UnsupportedEncodingException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
MessageDigest md = MessageDigest.getInstance("md5");
byte[] digestOfPassword = md.digest(keyString.getBytes());
byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8;) {
keyBytes[k++] = keyBytes[j++];
}
SecretKey key = new SecretKeySpec(keyBytes, "DESede");
IvParameterSpec iv = new IvParameterSpec(new byte[8]);
Cipher cipher = Cipher.getInstance(transformation);
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
byte[] plainTextBytes = input.getBytes();
byte[] cipherText = cipher.doFinal(plainTextBytes);
String outputString = "";
for (byte a:cipherText) {
outputString = outputString + a + " ";
}
return String.valueOf(outputString);
}
public String decrypt(String ciphertext, String keyString)
throws InvalidKeyException, NoSuchPaddingException,
InvalidAlgorithmParameterException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException {
byte [] cipherTextByteArray = stringToByteArray(ciphertext);
MessageDigest md = MessageDigest.getInstance("md5");
byte[] digestOfPassword = md.digest(keyString.getBytes());
byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
for (int j = 0, k = 16; j < 8;) {
keyBytes[k++] = keyBytes[j++];
}
SecretKey secretKey = new SecretKeySpec(keyBytes, algorithm);
IvParameterSpec iv = new IvParameterSpec(new byte[8]);
Cipher decryptor = Cipher.getInstance(transformation);
decryptor.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] decrypted = decryptor.doFinal(cipherTextByteArray);
return new String(decrypted);
}
private byte[] stringToByteArray(String string){
String[] stringFragments = string.split(" ");
byte[] byteArray = new byte[8];
for (int i = 0; i < stringFragments.length; i++) {
byteArray[i] = Byte.parseByte(stringFragments[i]);
}
return byteArray;
}
public static void main(String[] args) throws Exception {
//Test for TripleDES
String key = "TestKey";
String testString = "Secret";
EncryptorTripleDES encryptorTripleDES = new EncryptorTripleDES();
String encrypted = encryptorTripleDES.encrypt(testString, key);
System.out.println(encrypted);
String test = "[12, -37, -92, 118, 24, -49, -44, -75]";
String decrypted = encryptorTripleDES.decrypt("126 -86 29 -79 64 -86 31 -89", key);
System.out.println(decrypted);
}
}
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);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?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" />
<HBox alignment="CENTER" layoutX="173.0" layoutY="39.0" prefHeight="25.0" prefWidth="255.0" spacing="25.0">
<children>
<RadioButton fx:id="aseRadio" mnemonicParsing="false" selected="true" text="ASE">
<toggleGroup>
<ToggleGroup fx:id="encryptionDecryptionType" />
</toggleGroup>
</RadioButton>
<RadioButton fx:id="tripleDSERadio" mnemonicParsing="false" text="TripleDES" toggleGroup="$encryptionDecryptionType" />
</children>
</HBox>
<Text layoutX="211.0" layoutY="84.0" strokeType="OUTSIDE" strokeWidth="0.0" text="ASE key: 16 hexadecimal numbers" />
<Text layoutX="234.0" layoutY="104.0" strokeType="OUTSIDE" strokeWidth="0.0" text="TripleDES key: any String" />
</children>
</AnchorPane>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment