Skip to content

Commit

Permalink
fixed final bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
AnirudhhRamesh committed Nov 10, 2020
1 parent 8577439 commit 52d2999
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
35 changes: 22 additions & 13 deletions Crypto-CS107-2020/src/crypto/Decrypt.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,9 @@ public class Decrypt {
* @return the decoded string or the original encoded message if type is not in the list above.
*/
public static String breakCipher(String cipher, int type) {
//The method should be such that if you call it with cipher and type, it will decrypt the message

byte[] plainText = stringToBytes(cipher);
String output = null;

byte[] keywordInverse = new byte[vigenereWithFrequencies(plainText).length]; //Calculates the inverse keyword for Vigenere

for (int i = 0; i < (vigenereWithFrequencies(plainText).length); ++i) {
keywordInverse[i] = (byte) (-(vigenereWithFrequencies(plainText)[i]));
}
String output = "";

while ((type < 0) || (type > 2))
System.out.println("False input. Please enter a type that is within the range of 0-2: 0 = Caesar; 1 = Vigenere; 2 = XOR");
Expand All @@ -42,7 +35,7 @@ public static String breakCipher(String cipher, int type) {
output = bytesToString(Encrypt.caesar(plainText, (byte) (-(caesarWithFrequencies(plainText)))));

else if (type == Encrypt.VIGENERE) //VIGENERE = 1
output = bytesToString(Encrypt.vigenere(plainText, keywordInverse));
output = bytesToString(vigenereWithFrequencies(plainText));

else if (type == Encrypt.XOR) //XOR = 2
output = arrayToString(xorBruteForce(plainText));
Expand Down Expand Up @@ -220,20 +213,34 @@ public static byte[][] xorBruteForce(byte[] cipher) {
* @return the byte encoding of the clear text
*/
public static byte[] vigenereWithFrequencies(byte[] cipher) {
assert (cipher.length >= 6);
/* Method used:
* 1. Create an iterator from 0 to 255 (inclusive => 256 values) /
* 2. Multiply ENGLISHFREQUENCIES with A*0 (It. 0), A*1 (It. 1), ..., A*255 (It. 255) /
* 3. Create a wrap-around so if the charFreqIndex goes above 255, it wraps back to 0 /
* 4. Find the index of the biggest scalar product out of all scalar products => Error here: always returns 255
* Distance between charFrequencies[i] and 97 is the key
*/

if (cipher.length <= 1) {
//Fixes error where a cipher with length 1 causes error:
System.out.println("The inputted text is too small, please use a larger sized cipher");
byte[] empty = {};
return empty;
}

List<Byte> cleanCipher = removeSpaces(cipher);
int keyLength = vigenereFindKeyLength(cleanCipher);

byte[] vigenereKey = Decrypt.vigenereFindKey(cleanCipher, keyLength);

return vigenereKey;
byte[] vigenereKey = Decrypt.vigenereFindKey(cleanCipher, vigenereFindKeyLength(cleanCipher)); //Returns vigenere-encoding key
byte[] keywordInverse = new byte[vigenereKey.length]; //Calculates the inverse keyword for Vigenere

for (int i = 0; i < vigenereKey.length; ++i) {
keywordInverse[i] = (byte)(-vigenereKey[i]);
}

byte[] decryptedCipher = Encrypt.vigenere(cipher, keywordInverse);

return decryptedCipher;
}


Expand Down Expand Up @@ -296,6 +303,7 @@ public static int vigenereFindKeyLength(List<Byte> cipher) {
* @return the inverse key to decode the Vigenere cipher text
*/
public static byte[] vigenereFindKey(List<Byte> cipher, int keyLength) {
assert (cipher.size() >= 6);
/* Method used:
* Recover the key values:
* -> Divide the cipher by keyLength, into keyLength strings
Expand Down Expand Up @@ -393,6 +401,7 @@ public static int[] calculateCoincidences (List<Byte> cipher) {
* @return the Integer dynamic array containing the indices of all the localMaximas (in ascending index order)
*/
public static List<Integer> calculateMaximas(List<Byte> cipher, int[] coincidences){

int maxIndex = (int)Math.ceil(cipher.size()/2); //If cipher.size/2 = 7.5 => Math.ciel returns 8
List<Integer> localMaximas = new ArrayList<Integer>(); //stores the indices of local Maximas

Expand Down
14 changes: 10 additions & 4 deletions Crypto-CS107-2020/src/crypto/Encrypt.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Encrypt {
*/
public static String encrypt(String message, String key, int type) {

String output = null;
String output = "";

byte[] plainText = Helper.stringToBytes(message);
assert (plainText != null);
Expand All @@ -48,7 +48,12 @@ public static String encrypt(String message, String key, int type) {
output = bytesToString(xor(plainText, keyByte));
// By default, space encoding is OFF for XOR
} else if (type == ONETIME) { //ONETIME = 3
output = bytesToString(oneTimePad(plainText, keyBytes));
if (oneTimePad(plainText, keyBytes) != plainText) {
output = bytesToString(oneTimePad(plainText, keyBytes));
}
else {
output = "";
}
// By default, space encoding is ON for Onetimepad
} else if (type == CBC) { //CBC = 4
output = bytesToString(cbc(plainText, keyBytes));
Expand Down Expand Up @@ -202,8 +207,9 @@ public static byte[] oneTimePad(byte[] plainText, byte[] pad) {

//Verify the pad.length >= plainText.length, else return null
if (pad.length < plainTextCopy.length) {
System.out.println("Error: Failed OTP encoding. Pad length smaller than string length.");
return null;
System.out.println("Error: Failed OTP encoding. Pad length smaller than string length. Retry with a padlength >= text length.");
byte[] empty = {};
return empty; //Returns an empty string
}

for (int i = 0; i < plainTextCopy.length; ++i) {
Expand Down
Binary file modified Crypto-CS107-2020/src/crypto/Main.java
Binary file not shown.

0 comments on commit 52d2999

Please sign in to comment.