-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
99 additions
and
83 deletions.
There are no files selected for viewing
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
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
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); | ||
} | ||
} |
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
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); | ||
} | ||
} | ||
|
||
} |