From f79eccb372ee49a81bd1bb217f4fc5aa8b20c8ee Mon Sep 17 00:00:00 2001 From: chamatx Date: Tue, 27 Feb 2018 18:09:28 +0530 Subject: [PATCH] Substitution and Permutation --- src/sample/Controller.java | 23 +++------ src/sample/Permutation.java | 62 +++++++++++++---------- src/sample/Substitution.java | 97 +++++++++++++++++++++--------------- 3 files changed, 99 insertions(+), 83 deletions(-) diff --git a/src/sample/Controller.java b/src/sample/Controller.java index 88989ef..bab5448 100644 --- a/src/sample/Controller.java +++ b/src/sample/Controller.java @@ -6,10 +6,8 @@ import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; -import javafx.scene.control.Alert; import javafx.scene.input.MouseEvent; import javafx.scene.layout.AnchorPane; -import javafx.stage.DirectoryChooser; import javafx.stage.FileChooser; import java.io.File; @@ -33,8 +31,8 @@ public class Controller implements Initializable{ public static String filePath; public File selectedFile; private String encryptedValue,decryptedValue; - Substitution substitution = new Substitution(); - Permutation permutation = new Permutation(); +// Substitution substitution = new Substitution(); +// Permutation permutation = new Permutation(); @FXML public void handlePane(MouseEvent event){ @@ -86,10 +84,10 @@ public void handleEncrypt(ActionEvent event) throws IOException { e.printStackTrace(); } String secretKey = secretKeyEncrypt.getText().trim(); - encryptedValue = permutation.encryptWithPerm(substitution.encryptWithSub(everything,secretKey),secretKey); + encryptedValue = Permutation.getInstance().getEncryptPermutation(Substitution.getInstance().getEncryptSubstitution(everything,secretKey),secretKey); try { - FileController.fileWriter("C:\\Users\\USER\\Desktop\\savedText.txt",encryptedValue); + FileController.fileWriter("C:\\data\\savedText.txt",encryptedValue); } catch (IOException e) { e.printStackTrace(); } @@ -103,30 +101,21 @@ public void handleDecrypt(ActionEvent event) throws IOException { } String secretKey = secretKeyDecrypt.getText().trim(); System.out.println(secretKey); - decryptedValue = substitution.decryptwithSub(permutation.decryptWithPerm(everything,secretKey),secretKey); + decryptedValue = Substitution.getInstance().getDecryptSubstitution(Permutation.getInstance().getDecryptPermutation(everything,secretKey),secretKey); System.out.println(decryptedValue); try { - FileController.fileWriter("C:\\Users\\USER\\Desktop\\savedText.txt",decryptedValue); + FileController.fileWriter("C:\\data\\savedText.txt",decryptedValue); } catch (IOException e) { e.printStackTrace(); } } - - - - @Override public void initialize(URL location, ResourceBundle resources) { } - -// @Override -// public void initialize(URL location, ResourceBundle resources) { -// -// } } diff --git a/src/sample/Permutation.java b/src/sample/Permutation.java index 2483038..789a92a 100644 --- a/src/sample/Permutation.java +++ b/src/sample/Permutation.java @@ -1,54 +1,62 @@ package sample; public class Permutation { + private static Permutation instance = new Permutation(); - public String encryptWithPerm(String inputString, String secretKey){ + public static Permutation getInstance() { + return instance; + } + + private String encryptPermutation(String inputValue, String passphrase){ int deficit; - int n = secretKey.length(); - deficit = inputString.length()%n; - System.out.println("Deficit: "+deficit); + int keyLength = passphrase.length(); + deficit = inputValue.length()%keyLength; if(deficit != 0) { - deficit = n-deficit; - for(int a = deficit; a != 0 ; a--) { - inputString += "*"; + deficit = keyLength-deficit; + for(int i = deficit; i != 0 ; i--) { + inputValue += "*"; } } - System.out.println(inputString); - StringBuffer permutedString = new StringBuffer(); - int width = inputString.length()/n; - for(int i = 0 ; i < n ; i++) { + System.out.println(inputValue); + StringBuffer permutedValue = new StringBuffer(); + int width = inputValue.length()/keyLength; + for(int i = 0 ; i < keyLength ; i++) { for(int j = 0 ; j < width ; j++) { - char c = inputString.charAt(i+(j*n)); -// System.out.print(c); - permutedString.append(c); + char c = inputValue.charAt(i+(j*keyLength)); + permutedValue.append(c); } -// System.out.println(); } - System.out.println(permutedString); - return permutedString.toString(); + System.out.println(permutedValue); + return permutedValue.toString(); + } + + public String getEncryptPermutation(String input, String passphrase){ + return Permutation.getInstance().encryptPermutation(input, passphrase); } - public String decryptWithPerm(String inputString, String secretKey){ - int n = secretKey.length(); - int width = inputString.length()/n; - if(inputString.length()%n != 0){ + + private String decryptPermutation(String inputValue, String passphrase){ + int n = passphrase.length(); + int width = inputValue.length()/n; + if(inputValue.length()%n != 0){ return "Invalid secret key"; } else{ - StringBuffer reorganisedString = new StringBuffer(); + StringBuffer reorganisedValue = new StringBuffer(); for(int i = 0 ; i < width ; i++) { for(int j = 0 ; j< n ; j++) { - char c = inputString.charAt(i+(j*width)); -// System.out.print(c); - reorganisedString.append(c); + char c = inputValue.charAt(i+(j*width)); + reorganisedValue.append(c); } -// System.out.println(); } - return reorganisedString.toString(); + return reorganisedValue.toString(); } } + public String getDecryptPermutation(String input, String passphrase){ + return Permutation.getInstance().decryptPermutation(input, passphrase); + } } \ No newline at end of file diff --git a/src/sample/Substitution.java b/src/sample/Substitution.java index c91db36..6f00f39 100644 --- a/src/sample/Substitution.java +++ b/src/sample/Substitution.java @@ -1,60 +1,79 @@ package sample; -import java.math.BigInteger; +public class Substitution { + private static Substitution instance = new Substitution(); -public class Substitution -{ - public String encryptWithSub(String inputString, String secretKey){ - String subString = ""; + public static Substitution getInstance() { + return instance; + } + + private String encryptSubstitution(String inputValue, String passphrase) { + String substitutedValue = ""; int value = 0; + int keyLength = passphrase.length(); - for(int j = 0; j < secretKey.length(); j++){ - value += (int)secretKey.charAt(j); + for (int j = 0; j < keyLength; j++) { + value += (int) passphrase.charAt(j); } - int subVal = value % secretKey.length() + 2; + int subVal = value % keyLength + keyLength; - for(int i=0 ; i