Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Blue Montag committed Aug 31, 2016
1 parent 2183e0d commit 59e2c0a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 52 deletions.
54 changes: 6 additions & 48 deletions igs-lsgp/src/cipher/model/AlexCipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
*/
package cipher.model;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

import org.apache.commons.codec.binary.Base64;

import seqgen.model.generators.SeqGenWithReplGraph;

import commons.generators.IRandomLatinSquareGenerator;
import commons.model.ILatinSquare;
import commons.utils.Base64Utils;
import commons.utils.FileUtils;

/**
* This is another LS-based cipher, created by IGS for Blue Montag Software.
Expand Down Expand Up @@ -49,10 +47,10 @@ public static void main(String[] args) throws Exception {


String encrypted = cipher.crypt(inputString);
System.out.println(encrypted);
System.out.println(Base64Utils.getBase64OfString(encrypted));

writeTextToFile("c:\\users\\igallego\\Dropbox\\Blue Montag Software\\2016-08-10_Desafio\\2016-08-11_cipherText.txt", cipher.getBase64OfString(encrypted));
writeTextToFile("c:\\users\\igallego\\Dropbox\\Blue Montag Software\\2016-08-10_Desafio\\2016-08-11_LS.txt", cipher.getLSAsBase64String());
FileUtils.writeTextToFile("c:\\users\\igallego\\Dropbox\\Blue Montag Software\\2016-08-10_Desafio\\2016-08-31_cipherText.txt", Base64Utils.getBase64OfString(encrypted));
FileUtils.writeTextToFile("c:\\users\\igallego\\Dropbox\\Blue Montag Software\\2016-08-10_Desafio\\2016-08-31_LS.txt", Base64Utils.getLSAsBase64String(ls));

String plain = cipher.decrypt(encrypted);

Expand All @@ -63,16 +61,6 @@ public static void main(String[] args) throws Exception {
input.close();
}

private static void writeTextToFile(String path, String text) throws Exception {
File logFile=new File(path);

BufferedWriter writer = new BufferedWriter(new FileWriter(logFile));
writer.write (text);

//Close writer
writer.close();
}

/**
* Constructs a cipher with the LS passed as parameter as the private key.
*
Expand Down Expand Up @@ -234,34 +222,4 @@ public void showPrivateKey() throws Exception {
}
}

private String getLSAsBase64String() throws Exception {
String result = "";
byte[] byteArr = new byte[65536];//para guardar el LS de 256*256
char[] charArr = new char[65536];//para guardar el LS de 256*256
int k = 0;
for (int i=0; i<ls.size(); i++) {
for(int j=0; j<ls.size(); j++) {
//result+=(Character.toString((char)(ls.getValueAt(i, j).intValue())));//this is to return a string
//byte signedByte = (byte)(ls.getValueAt(i, j).intValue());
//int unsignedByte = signedByte & (0xff);
char c = (char)(ls.getValueAt(i, j).intValue());
charArr[k] = c;
k++;
}
}

//lo codifico base64
byteArr = new String(charArr).getBytes();

Base64 base64 = new Base64();//esta clase esta en el Jar common-codec-1.6.jar
result = base64.encodeToString(byteArr);
return result;
}

private String getBase64OfString(String str) throws Exception {
byte[] byteArr = str.getBytes();
Base64 base64 = new Base64();//esta clase esta en el Jar common-codec-1.6.jar
return base64.encodeToString(byteArr);
}

}
6 changes: 4 additions & 2 deletions igs-lsgp/src/cipher/model/GibsonCipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import java.util.Scanner;

import seqgen.model.generators.SeqGenWithReplGraph;

import commons.model.ILatinSquare;
import commons.model.OrderedPair;
import commons.utils.Base64Utils;

/**
* This simple Cipher shows how a LS can be used to crypt and decrypt information.
Expand Down Expand Up @@ -56,15 +58,15 @@ public static void main(String[] args) throws Exception {

GibsonCipher cipher = new GibsonCipher(ls);
// cipher.add(64);
cipher.showPrivateKey();
// cipher.showPrivateKey();

Scanner input = new Scanner(System.in);
System.out.print("Enter some plain text: ");
String inputString = input.nextLine();


String encrypted = cipher.crypt(inputString);
System.out.println(encrypted);
System.out.println(Base64Utils.getBase64OfString(encrypted));

String plain = cipher.decrypt(encrypted);

Expand Down
4 changes: 2 additions & 2 deletions igs-lsgp/src/commons/mainexecutable/GeneratorJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static void main(String[] args) throws Exception {
System.out.println("_____________________________________________________________________________");
System.out.println("");
System.out.println("Usage: <method> <order> [write <path>]");
System.out.println("Where <method> ::= simple | product | swapping | restart | graph | jm | mckay");
System.out.println("Where <method> ::= back | product | swapping | restart | graph | jm | mckay");
System.out.println("_____________________________________________________________________________");
return;
}
Expand All @@ -85,7 +85,7 @@ public static void main(String[] args) throws Exception {
}
IRandomLatinSquareGenerator generator;

if (args[0].equalsIgnoreCase("simple")) {
if (args[0].equalsIgnoreCase("back")) {
generator = new SeqGenWithBacktracking(n);
computeTimeFor(generator, path);
return;
Expand Down
47 changes: 47 additions & 0 deletions igs-lsgp/src/commons/utils/Base64Utils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Creation date: 31/08/2016
*
*/
package commons.utils;

import org.apache.commons.codec.binary.Base64;

import commons.model.ILatinSquare;

/**
* @author igallego
*
*/
public class Base64Utils {

public static String getLSAsBase64String(ILatinSquare ls) throws Exception {
String result = "";
byte[] byteArr = new byte[65536];//para guardar el LS de 256*256
char[] charArr = new char[65536];//para guardar el LS de 256*256
int k = 0;
for (int i=0; i<ls.size(); i++) {
for(int j=0; j<ls.size(); j++) {
//result+=(Character.toString((char)(ls.getValueAt(i, j).intValue())));//this is to return a string
//byte signedByte = (byte)(ls.getValueAt(i, j).intValue());
//int unsignedByte = signedByte & (0xff);
char c = (char)(ls.getValueAt(i, j).intValue());
charArr[k] = c;
k++;
}
}

//lo codifico base64
byteArr = new String(charArr).getBytes();

Base64 base64 = new Base64();//esta clase esta en el Jar common-codec-1.6.jar
result = base64.encodeToString(byteArr);
return result;
}

public static String getBase64OfString(String str) throws Exception {
byte[] byteArr = str.getBytes();
Base64 base64 = new Base64();//esta clase esta en el Jar common-codec-1.6.jar
return base64.encodeToString(byteArr);
}

}
16 changes: 16 additions & 0 deletions igs-lsgp/src/commons/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
*/
package commons.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import commons.model.ILatinSquare;

/**
Expand All @@ -43,4 +47,16 @@ public static void writeLS(ILatinSquare ic, String path) {
}
}
}


public static void writeTextToFile(String path, String text) throws Exception {
File logFile=new File(path);

BufferedWriter writer = new BufferedWriter(new FileWriter(logFile));
writer.write (text);

//Close writer
writer.close();
}

}

0 comments on commit 59e2c0a

Please sign in to comment.