Skip to content

Commit

Permalink
Substitution and Permutation
Browse files Browse the repository at this point in the history
  • Loading branch information
chamathns committed Feb 27, 2018
1 parent 46d6432 commit f79eccb
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 83 deletions.
23 changes: 6 additions & 17 deletions src/sample/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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){
Expand Down Expand Up @@ -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();
}
Expand All @@ -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) {
//
// }
}
62 changes: 35 additions & 27 deletions src/sample/Permutation.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
97 changes: 58 additions & 39 deletions src/sample/Substitution.java
Original file line number Diff line number Diff line change
@@ -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<inputString.length() ; i++) {
char c = inputString.charAt(i);
if(i%3 == 0){
subString += ((char) (c + subVal));
}
else if(i%3 == 1){
subString += ((char) (c + subVal + 3));
}
else{
subString += ((char) (c + subVal + 5));
for (int i = 0; i < inputValue.length(); i++) {
char c = inputValue.charAt(i);
if (i % keyLength == 0) {
substitutedValue += ((char) (c + subVal));
} else if (i % keyLength == 1) {
substitutedValue += ((char) (c + subVal + keyLength % 2));
} else if (i % keyLength == 2) {
substitutedValue += ((char) (c + subVal + keyLength % 3));

} else if (i % keyLength == 3) {
substitutedValue += ((char) (c + subVal + keyLength % 5));

} else {
substitutedValue += ((char) (c + subVal + keyLength % 7));
}

}
// System.out.println("Enc sub: "+subString);
return subString;
return substitutedValue;
}

public String decryptwithSub(String inputString, String secretKey){
String decryptedString = "";

int value = 0;
public String getEncryptSubstitution(String input, String passphrase){
return Substitution.getInstance().encryptSubstitution(input, passphrase);
}

for(int j = 0; j<secretKey.length(); j++){
value += (int)secretKey.charAt(j);
}

int subVal = value%secretKey.length() + 2;
private String decryptSubstitution (String inputValue, String passphrase){
String decryptedValue = "";
int keyLength = passphrase.length();
int value = 0;

for(int i=0; i<inputString.length(); i++){
char c = inputString.charAt(i);
if(i%3 == 0){
decryptedString += ((char) (c - subVal));
for (int j = 0; j < keyLength; j++) {
value += (int) passphrase.charAt(j);
}
else if(i%3 == 1){
decryptedString += ((char) (c - subVal - 3));
}
else{
decryptedString += ((char) (c - subVal - 5));

int subVal = value % keyLength + keyLength;

for (int i = 0; i < inputValue.length(); i++) {
char c = inputValue.charAt(i);
if (i % keyLength == 0) {
decryptedValue += ((char) (c - subVal));
} else if (i % keyLength == 1) {
decryptedValue += ((char) (c - subVal - keyLength % 2));
} else if (i % keyLength == 2) {
decryptedValue += ((char) (c - subVal - keyLength % 3));
} else if (i % keyLength == 3) {
decryptedValue += ((char) (c - subVal - keyLength % 5));
} else {
decryptedValue += ((char) (c - subVal - keyLength % 7));
}
}
return decryptedValue;
}
// System.out.println("Dec sub: "+decryptedString);
return decryptedString;

public String getDecryptSubstitution(String input, String passphrase){
return Substitution.getInstance().decryptSubstitution(input, passphrase);
}
}

}

0 comments on commit f79eccb

Please sign in to comment.