/*
OTP4U 0.9.2 - One Time Pad for you
Copyright (C) 2003 Pierre Blanc
Pierre Blanc: blanc_teutoburgo@yahoo.it
http://www.teutoburgo.tk
http://it.geocities.com/teutoburgo
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
or go to http://www.gnu.org/copyleft/gpl.html
*/
package tk.teutoburgo.otp4u.application;
import tk.teutoburgo.otp4u.crypt.*;
import tk.teutoburgo.otp4u.util.*;
import tk.teutoburgo.otp4u.constants.*;
import tk.teutoburgo.otp4u.io.*;
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
/**
* <p>Title: OTP4U</p>
* <p>Description: The main class for the command-line application
* OTP4U (One Time Pad for you)</p>
* <p>Copyright: Copyright (c) Pierre Blanc 2003</p>
* <p>Company: Teutoburgo</p>
* @author Pierre Blanc
* revisions:
* 05/29/03 - Karl Dinwiddie - some minor code clean-up so it can be run
* "out of the box" more easily.
* @version 0.8.2
*/
public class OTP4U {
public static String OTP4U_HOME
= "";
public static final String SEPARATOR
=Constants.
SEPARATOR,MIXED
="mixed.key",
PUBLIC_KEY_FILE=Constants.PUBLIC_KEY_FILE, CFG_FILE=Constants.CFG_FILE,
RANDOM_KEY_FILE=Constants.RANDOM_KEY_FILE, CIPHER=Constants.CIPHER,
KEY_SMALL = Constants.KEY_SMALL , PLAIN=Constants.PLAIN,
RANDOM_KEY=Constants.RANDOM_KEY , PUBLIC_KEY=Constants.PUBLIC_KEY,
ENTROPY=Constants.ENTROPY , KEY4KEY=Constants.KEY4KEY,
KEY4KEY_FILE=Constants.KEY4KEY_FILE,
PSEUDO_RANDOM=Constants.PSEUDO_RANDOM,
badParameter=Constants.BAD_PARAMETER, CONFIG = Constants.CONFIG;
private int defaultLength=3000;
public static final boolean GUI_TRUE = true;
public static final boolean GUI_FALSE = false;
KeyManager keyManager;
Xor xor;
Io io;
Cipher cipher;
public static MessagesPrinter mp;
// MessagesPrinter mp;
// Random rnd = new Random();
RandomGenerator rg;
private long DEFAULT_DEVIATION=100;
private String defaultFile
="file", stringLog
="";
private boolean isGUI=false;
public static File configFile
;
/**
* OTP4U constructor comment.
*/
public OTP4U() {
}
public static void setConfigFile
(File newConfigFile
) {
configFile = newConfigFile;
}
public OTP4U(boolean isGUI) {
super();
this.isGUI=isGUI;
OTP4U_HOME
=new File((new File("")).
getAbsolutePath())+SEPARATOR
;
/* try {
fis = new FileInputStream(new File(OTP4U_HOME+CONFIG+CFG_FILE));
// otp4uInit(CFG_FILE);
} catch (FileNotFoundException fnfe){
OTP4U_HOME=new File((new File("")).getAbsolutePath())+
SEPARATOR;
// fis2 = new FileInputStream(new File(OTP4U_HOME+CONFIG+CFG_FILE));
}*/
try{
otp4uInit(CFG_FILE);
/* File file = new File("c:/otp4u_home.cfg");
try {
fis2 = new FileInputStream(file);
try{
fis2 = new FileInputStream(new File("c:/otp4u_home.cfg"));
} catch (Exception e){
System.out.println("Error! Can't find c:/otp4u_home.cfg");
System.out.println("Under Windows copy it from patch4win/otp4u_home.cfg");
System.exit(-1);
}
try{
prop.load(fis2);
OTP4U_HOME=prop.getProperty("OTP4U_HOME");
otp4uInit(CFG_FILE);*/
ie.printStackTrace();
cnfe.printStackTrace();
e.printStackTrace();
mp.sPrintLog("Using OTP4U_HOME="+OTP4U_HOME);
mp.sPrintLog("Set the OTP4U_HOME");
ioe.printStackTrace();
e.printStackTrace();
}
/* catch (IOException ioe) {
ioe.printStackTrace();
System.exit(-1);
// } catch (NumberFormatException e){
} catch (Exception e){
e.printStackTrace();
}*/
}
/**
* Used by the contructor and by a GUI method to load the properties
* in the configuration file
* @param fileName the name of the configuration file
*/
xor= new Xor();
// System.out.println("\n---- honme "+OTP4U_HOME);
io = new Io(OTP4U_HOME, isGUI);
cipher = new Cipher(OTP4U_HOME, isGUI);
rg = new RandomGenerator(OTP4U_HOME, isGUI);
mp = new MessagesPrinter(isGUI);
prop.load(fis);
temp = prop.getProperty("keymanager");
Class[] parameters = {OTP4U_HOME.getClass(), b.getClass()};
temp).getConstructor(parameters);
Object[] initArgs
={OTP4U_HOME, b
};
keyManager = (KeyManager)constr.newInstance(initArgs);
temp = prop.getProperty("defaultfile","file");
defaultFile=temp;
temp = prop.getProperty("randomFileDefaultLength","3");
defaultLength
=Integer.
parseInt(temp
)*1024;
mp.sPrintLog("Read configuration from file "+
OTP4U_HOME+CONFIG+fileName);
}
/**
* Used to generate a PSEUDO-random file in the entropySource directory
* or in the key4key directory.
* Use it to test the functionalities of OTP4U.
* @param pathName the path name of the pseudo-random file
* @param fileName the file name
* @param length the file length
*/
public void generateEntropyFile(){
rg.generateRandomFile(OTP4U_HOME+ENTROPY, PSEUDO_RANDOM, defaultLength);
}
public void generateK4K0File(){
rg.generateRandomFile(OTP4U_HOME + RANDOM_KEY + KEY4KEY, KEY4KEY_FILE,
defaultLength/2);
}
/**
* Performs decryption if the files of ciphertext and randomKey are found <br>
* Writes the plaintext array in its directory
* @param pathName The pathName of the ciphertext
*/
public void doDecryption
(String pathName
) {
if(pathName.equals(""))
pathName=defaultFile;
cipher.decipher(pathName);
}
/**
* Performs encryption if the files of plaintext and randomKey are found <br>
* Writes the ciphertext array ciphertext in its directory
* @param pathName The pathname of the plaintext
*/
public void doEncryption
(String pathName
) {
if(pathName.equals(""))
pathName=defaultFile;
cipher.encipher(pathName);
}
public String getDefaultFile
(){
return defaultFile;
}
public int getDefaultLength(){
return defaultLength;
}
public int getRandomKeySize(){
try{
byte[] randomKey = io.readFile(OTP4U_HOME +RANDOM_KEY +
RANDOM_KEY_FILE);
return randomKey.length;
e.printStackTrace();
return 0;
}
}
byte[][] arrays=new byte[2][];
byte[] fileToXOR=null;
fileToXOR = io.readFile(pathName);
byte[] randomKeyBytes = io.readFile(OTP4U_HOME + RANDOM_KEY +
RANDOM_KEY_FILE);
arrays[0]=fileToXOR;
arrays[1]=randomKeyBytes;
return arrays;
}
byte[] publicKey=io.readFile(OTP4U_HOME + PUBLIC_KEY +
PUBLIC_KEY_FILE);
byte[] ciphertext = io.readFile(OTP4U_HOME + CIPHER + pathName);
byte[] mixed=mixer.mix(publicKey, ciphertext);
io.writeFile(OTP4U_HOME+ PUBLIC_KEY + MIXED, mixed);
}
byte[] publicKey=io.readFile(OTP4U_HOME + PUBLIC_KEY +
PUBLIC_KEY_FILE);
byte[] ciphertext = io.readFile(pathName);
byte[] mixed=mixer.mix(publicKey, ciphertext);
io.writeFile(OTP4U_HOME+ PUBLIC_KEY + MIXED, mixed);
}
byte[] mixed=io.readFile(OTP4U_HOME + PUBLIC_KEY + MIXED),
K4KN=io.readFile(OTP4U_HOME + RANDOM_KEY + KEY4KEY + KEY4KEY_FILE);
int l=K4KN.length;
byte[] publicKey=mixer.getPublicKey(mixed, l);
byte[] ciphertext=mixer.getCiphertext(mixed, l);
io.writeFile(OTP4U_HOME+ PUBLIC_KEY + PUBLIC_KEY_FILE, publicKey);
io.writeFile(OTP4U_HOME+ CIPHER + "ciphertext.dat", ciphertext);
}
public static void main
(String args
[]) {
OTP4U otp4u = new OTP4U(false);
// mp = new MessagesPrinter();
// MessagesPrinter mp = new MessagesPrinter();
mp.printCopyright();
int argsLen = args.length;
String fileName
= otp4u.
getDefaultFile();
int length=otp4u.getDefaultLength();
if (argsLen == 0) {
mp.printHelp();
}
else if (argsLen == 1) {
if (args[0].equalsIgnoreCase("e"))
otp4u.doEncryption(fileName);
else if (args[0].equalsIgnoreCase("d"))
otp4u.doDecryption(fileName);
else if (args[0].equalsIgnoreCase("h")
|| args[0].equalsIgnoreCase("help")
|| args[0].equalsIgnoreCase("-h")
|| args[0].equalsIgnoreCase("--help"))
mp.printHelp();
else if (args[0].equalsIgnoreCase("g"))
otp4u.generateEntropyFile();
else if (args[0].equalsIgnoreCase("gk4k0")){
otp4u.generateK4K0File();
} else if (args[0].equalsIgnoreCase("w")){
mp.printNoWarranty();
} else if (args[0].equalsIgnoreCase("p")){
otp4u.writePublicKey();
} else if (args[0].equalsIgnoreCase("r")){
otp4u.writeRandomKey();
} else if (args[0].equalsIgnoreCase("m")){
try{
otp4u.mix(fileName);
ioe.printStackTrace();
}
} else if (args[0].equalsIgnoreCase("s")){
try{
otp4u.separate();
ioe.printStackTrace();
}
} else if (args[0].equalsIgnoreCase("test")){
try{
/* Io io = new Io();
byte[] x = io.readFile(OTP4U_HOME + RANDOM_KEY + KEY4KEY +
KEY4KEY_FILE);
byte[] y = io.readFile(OTP4U_HOME + RANDOM_KEY + KEY4KEY +
"test.rnd");
io.writeFile(OTP4U_HOME + RANDOM_KEY + KEY4KEY+"autoKey.rnd",
otp4u.autoKey(x, y));*/
e.printStackTrace();
}
/* } else if (args[0].equalsIgnoreCase("t")){//testPermutation
byte[] pr={1,2,3,4,5,6,7,8,9,10,11};
otp4u.getKey4Message(pr,pr);*/
} else
System.
out.
println(badParameter
);
} else if (argsLen == 2)
if (args[0].equalsIgnoreCase("e"))
otp4u.doEncryption(args[1]);
else if (args[0].equalsIgnoreCase("d"))
otp4u.doDecryption(args[1]);
else if (args[0].equalsIgnoreCase("g")){
try{
length
=Integer.
parseInt(args
[1
])*1024;
nfe.printStackTrace();
}
otp4u.generateEntropyFile();
}
else if (args[0].equalsIgnoreCase("gk4k0")){
try{
length
=Integer.
parseInt(args
[1
])*1024;
nfe.printStackTrace();
}
otp4u.generateK4K0File();
}
else
System.
out.
println(badParameter
);
else
System.
out.
println("Wrong number of parameters "+argsLen
);
}
/**
* Used to write the "public" key
*/
public void writePublicKey(){
try{
keyManager.writePublicKey();
e.printStackTrace();
}
}
/**
* Used to write the random key (composed of K4Kn and K4Mn)
*/
public void writeRandomKey(){
try{
keyManager.writeRandomKey();
ioe.printStackTrace();
mp.sCatchException();
}
}
}