Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
OTP4U (One Time Pad For You) - OTP4U.java

OTP4U.java

Caricato da: Teutoburgo
Scarica il programma completo

  1. /*
  2.     OTP4U 0.9.2 - One Time Pad for you
  3.     Copyright (C) 2003 Pierre Blanc
  4.     Pierre Blanc: blanc_teutoburgo@yahoo.it
  5.     http://www.teutoburgo.tk
  6.     http://it.geocities.com/teutoburgo
  7.  
  8.     This program is free software; you can redistribute it and/or modify
  9.     it under the terms of the GNU General Public License as published by
  10.     the Free Software Foundation; either version 2 of the License, or
  11.     (at your option) any later version.
  12.  
  13.     This program is distributed in the hope that it will be useful,
  14.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.     GNU General Public License for more details.
  17.  
  18.     You should have received a copy of the GNU General Public License
  19.     along with this program; if not, write to the Free Software
  20.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.     or go to      http://www.gnu.org/copyleft/gpl.html
  22. */
  23.  
  24. package tk.teutoburgo.otp4u.application;
  25.  
  26. import tk.teutoburgo.otp4u.crypt.*;
  27. import tk.teutoburgo.otp4u.util.*;
  28. import tk.teutoburgo.otp4u.constants.*;
  29. import tk.teutoburgo.otp4u.io.*;
  30. import java.io.*;
  31. import java.util.*;
  32. import java.lang.reflect.*;
  33.  
  34. /**
  35.  * <p>Title: OTP4U</p>
  36.  * <p>Description: The main class for the command-line application
  37.  * OTP4U (One Time Pad for you)</p>
  38.  * <p>Copyright: Copyright (c) Pierre Blanc 2003</p>
  39.  * <p>Company: Teutoburgo</p>
  40.  * @author Pierre Blanc
  41.  * revisions:
  42.  *  05/29/03 - Karl Dinwiddie - some minor code clean-up so it can be  run
  43.  *                              "out of the box" more easily.
  44.  * @version 0.8.2
  45.  */
  46.  
  47.  
  48. public class OTP4U {
  49.     public static String OTP4U_HOME = "";
  50.     public static final String SEPARATOR=Constants.SEPARATOR,MIXED="mixed.key",
  51.         PUBLIC_KEY_FILE=Constants.PUBLIC_KEY_FILE, CFG_FILE=Constants.CFG_FILE,
  52.         RANDOM_KEY_FILE=Constants.RANDOM_KEY_FILE, CIPHER=Constants.CIPHER,
  53.         KEY_SMALL = Constants.KEY_SMALL , PLAIN=Constants.PLAIN,
  54.         RANDOM_KEY=Constants.RANDOM_KEY , PUBLIC_KEY=Constants.PUBLIC_KEY,
  55.         ENTROPY=Constants.ENTROPY , KEY4KEY=Constants.KEY4KEY,
  56.         KEY4KEY_FILE=Constants.KEY4KEY_FILE,
  57.         PSEUDO_RANDOM=Constants.PSEUDO_RANDOM,
  58.         badParameter=Constants.BAD_PARAMETER, CONFIG = Constants.CONFIG;
  59.     private int defaultLength=3000;
  60.     public static final boolean GUI_TRUE = true;
  61.     public static final boolean GUI_FALSE = false;
  62.     KeyManager keyManager;
  63.     Xor xor;
  64.     Io io;
  65.     Cipher cipher;
  66.         public static MessagesPrinter mp;
  67.         Mixer mixer = new Mixer();
  68. //    MessagesPrinter mp;
  69. //    Random rnd = new Random();
  70.     RandomGenerator rg;
  71.     private long DEFAULT_DEVIATION=100;
  72.     private String defaultFile="file",  stringLog="";
  73.     private boolean isGUI=false;
  74.     public static File configFile;
  75.     /**
  76.      * OTP4U constructor comment.
  77.      */
  78.     public OTP4U() {
  79.     }
  80.  
  81.         public static void setConfigFile(File newConfigFile) {
  82.         configFile = newConfigFile;
  83.     }
  84.  
  85.     public OTP4U(boolean isGUI) {
  86.         super();
  87.         this.isGUI=isGUI;
  88.         OTP4U_HOME=new File((new File("")).getAbsolutePath())+SEPARATOR;
  89.         String temp = "";
  90.         Properties prop = new Properties();
  91.         FileInputStream fis=null, fis2=null;
  92. /*      try {
  93.             fis = new FileInputStream(new File(OTP4U_HOME+CONFIG+CFG_FILE));
  94.          //   otp4uInit(CFG_FILE);
  95.         } catch (FileNotFoundException fnfe){
  96.  
  97.                 OTP4U_HOME=new File((new File("")).getAbsolutePath())+
  98.             SEPARATOR;
  99.            // fis2 = new FileInputStream(new File(OTP4U_HOME+CONFIG+CFG_FILE));
  100.         }*/
  101.         try{
  102.             otp4uInit(CFG_FILE);
  103.  
  104.  
  105. /*                              File file = new File("c:/otp4u_home.cfg");
  106.                         try {
  107.                                 fis2 = new FileInputStream(file);
  108.  
  109.             try{
  110.                 fis2 = new FileInputStream(new File("c:/otp4u_home.cfg"));
  111.             } catch (Exception e){
  112.                 System.out.println("Error! Can't find c:/otp4u_home.cfg");
  113.                 System.out.println("Under Windows copy it from patch4win/otp4u_home.cfg");
  114.                 System.exit(-1);
  115.             }
  116.             try{
  117.                 prop.load(fis2);
  118.                 OTP4U_HOME=prop.getProperty("OTP4U_HOME");
  119.                 otp4uInit(CFG_FILE);*/
  120.             }catch(IllegalArgumentException ie){
  121.                 ie.printStackTrace();
  122.             }catch(ClassNotFoundException cnfe){
  123.                 cnfe.printStackTrace();
  124.  
  125.             } catch (FileNotFoundException e){
  126.                 e.printStackTrace();
  127.                 mp.sPrintLog("Using OTP4U_HOME="+OTP4U_HOME);
  128.                 mp.sPrintLog("Set the OTP4U_HOME");
  129.                 System.exit(-1);
  130.             } catch(IOException ioe){
  131.                 ioe.printStackTrace();
  132.                 System.exit(-1);
  133.             } catch (Exception e){
  134.                 e.printStackTrace();
  135.             }
  136. /*         catch (IOException ioe) {
  137.             ioe.printStackTrace();
  138.             System.exit(-1);
  139.             //  } catch (NumberFormatException e){
  140.  
  141.         } catch (Exception e){
  142.             e.printStackTrace();
  143.         }*/
  144.     }
  145.     /**
  146.      *  Used by the contructor and by a GUI method to load the properties
  147.      *  in the configuration file
  148.      *  @param fileName the name of the configuration file
  149.      */
  150.     public void otp4uInit(String fileName) throws FileNotFoundException,
  151.  
  152.         String temp = "";
  153.         Properties prop = new Properties();
  154.         FileInputStream fis=null, fis2=null;
  155.         xor= new Xor();
  156.         // System.out.println("\n---- honme "+OTP4U_HOME);
  157.         io = new Io(OTP4U_HOME, isGUI);
  158.         cipher = new Cipher(OTP4U_HOME, isGUI);
  159.         rg = new RandomGenerator(OTP4U_HOME, isGUI);
  160.         mp = new MessagesPrinter(isGUI);
  161.         fis = new FileInputStream(new File(OTP4U_HOME+CONFIG+fileName));
  162.         prop.load(fis);
  163.         temp = prop.getProperty("keymanager");
  164.         Boolean b=new Boolean(isGUI);
  165.         Class[] parameters = {OTP4U_HOME.getClass(), b.getClass()};
  166.         Constructor constr=Class.forName(
  167.           temp).getConstructor(parameters);
  168.         Object[] initArgs={OTP4U_HOME, b};
  169.         keyManager = (KeyManager)constr.newInstance(initArgs);
  170.         temp = prop.getProperty("defaultfile","file");
  171.         defaultFile=temp;
  172.         temp = prop.getProperty("randomFileDefaultLength","3");
  173.         defaultLength=Integer.parseInt(temp)*1024;
  174.         mp.sPrintLog("Read configuration from file "+
  175.                      OTP4U_HOME+CONFIG+fileName);
  176.     }
  177.  
  178.     /**
  179.      * Used to generate a PSEUDO-random file in the entropySource directory
  180.      * or in the key4key directory.
  181.      * Use it to test the functionalities of OTP4U.
  182.      * @param pathName the path name of the pseudo-random file
  183.      * @param fileName the file name
  184.      * @param length the file length
  185.      */
  186.     public void generateEntropyFile(){
  187.  
  188.     rg.generateRandomFile(OTP4U_HOME+ENTROPY, PSEUDO_RANDOM, defaultLength);
  189.     }
  190.  
  191.     public void generateK4K0File(){
  192.  
  193.       rg.generateRandomFile(OTP4U_HOME + RANDOM_KEY + KEY4KEY, KEY4KEY_FILE,
  194.               defaultLength/2);
  195.     }
  196.  
  197.     /**
  198.      * Performs decryption if the files of ciphertext and randomKey are found <br>
  199.      * Writes the plaintext array in its directory
  200.      * @param pathName The pathName of the ciphertext
  201.      */
  202.     public void doDecryption(String pathName) {
  203.         if(pathName.equals(""))
  204.           pathName=defaultFile;
  205.         cipher.decipher(pathName);
  206.     }
  207.  
  208.     /**
  209.      * Performs encryption if the files of plaintext and randomKey are found <br>
  210.      * Writes the ciphertext array ciphertext in its directory
  211.      * @param pathName The pathname of the plaintext
  212.      */
  213.     public void doEncryption(String pathName) {
  214.         if(pathName.equals(""))
  215.           pathName=defaultFile;
  216.         cipher.encipher(pathName);
  217.     }
  218.  
  219.     public String getDefaultFile(){
  220.         return defaultFile;
  221.     }
  222.  
  223.     public int getDefaultLength(){
  224.         return defaultLength;
  225.     }
  226.  
  227.     public int getRandomKeySize(){
  228.         try{
  229.             byte[] randomKey = io.readFile(OTP4U_HOME +RANDOM_KEY +
  230.                                            RANDOM_KEY_FILE);
  231.             return randomKey.length;
  232.         }catch (Exception e){
  233.             e.printStackTrace();
  234.             return 0;
  235.         }
  236.     }
  237.  
  238.     public byte[][] initArrays(String pathName) throws IOException {
  239.         byte[][] arrays=new byte[2][];
  240.         byte[] fileToXOR=null;
  241.  
  242.         fileToXOR = io.readFile(pathName);
  243.         byte[] randomKeyBytes = io.readFile(OTP4U_HOME + RANDOM_KEY +
  244.                                             RANDOM_KEY_FILE);
  245.         arrays[0]=fileToXOR;
  246.         arrays[1]=randomKeyBytes;
  247.  
  248.         return arrays;
  249.     }
  250.  
  251.         public void mix(String pathName) throws IOException{
  252.                 byte[] publicKey=io.readFile(OTP4U_HOME + PUBLIC_KEY +
  253.                         PUBLIC_KEY_FILE);
  254.                 byte[] ciphertext = io.readFile(OTP4U_HOME + CIPHER + pathName);
  255.                 byte[] mixed=mixer.mix(publicKey, ciphertext);
  256.                 io.writeFile(OTP4U_HOME+ PUBLIC_KEY + MIXED, mixed);
  257.         }
  258.  
  259.         public void mixGUI(String pathName) throws IOException{
  260.                 byte[] publicKey=io.readFile(OTP4U_HOME + PUBLIC_KEY +
  261.                         PUBLIC_KEY_FILE);
  262.                 byte[] ciphertext = io.readFile(pathName);
  263.                 byte[] mixed=mixer.mix(publicKey, ciphertext);
  264.                 io.writeFile(OTP4U_HOME+ PUBLIC_KEY + MIXED, mixed);
  265.         }
  266.  
  267.         public void separate() throws IOException{
  268.                 byte[] mixed=io.readFile(OTP4U_HOME + PUBLIC_KEY + MIXED),
  269.                         K4KN=io.readFile(OTP4U_HOME + RANDOM_KEY + KEY4KEY + KEY4KEY_FILE);
  270.                 int l=K4KN.length;
  271.                 byte[] publicKey=mixer.getPublicKey(mixed, l);
  272.                 byte[] ciphertext=mixer.getCiphertext(mixed, l);
  273.                 io.writeFile(OTP4U_HOME+ PUBLIC_KEY + PUBLIC_KEY_FILE, publicKey);
  274.                 io.writeFile(OTP4U_HOME+ CIPHER + "ciphertext.dat", ciphertext);
  275.         }
  276.  
  277.     public static void main(String args[]) {
  278.         OTP4U otp4u = new OTP4U(false);
  279. //      mp = new MessagesPrinter();
  280. //      MessagesPrinter mp = new MessagesPrinter();
  281.         mp.printCopyright();
  282.         int argsLen = args.length;
  283.         String fileName = otp4u.getDefaultFile();
  284.         int length=otp4u.getDefaultLength();
  285.  
  286.         if (argsLen == 0) {
  287.             mp.printHelp();
  288.         }
  289.         else if (argsLen == 1) {
  290.             if (args[0].equalsIgnoreCase("e"))
  291.                 otp4u.doEncryption(fileName);
  292.             else if (args[0].equalsIgnoreCase("d"))
  293.                 otp4u.doDecryption(fileName);
  294.             else if (args[0].equalsIgnoreCase("h")
  295.                      || args[0].equalsIgnoreCase("help")
  296.                      || args[0].equalsIgnoreCase("-h")
  297.                      || args[0].equalsIgnoreCase("--help"))
  298.                 mp.printHelp();
  299.             else if (args[0].equalsIgnoreCase("g"))
  300.                 otp4u.generateEntropyFile();
  301.             else if (args[0].equalsIgnoreCase("gk4k0")){
  302.                 otp4u.generateK4K0File();
  303.             } else if (args[0].equalsIgnoreCase("w")){
  304.                 mp.printNoWarranty();
  305.             } else if (args[0].equalsIgnoreCase("p")){
  306.                 otp4u.writePublicKey();
  307.             } else if (args[0].equalsIgnoreCase("r")){
  308.                 otp4u.writeRandomKey();
  309.                         } else if (args[0].equalsIgnoreCase("m")){
  310.                                 try{
  311.                                         otp4u.mix(fileName);
  312.                                 } catch(IOException ioe){
  313.                                         ioe.printStackTrace();
  314.                                 }
  315.                         } else if (args[0].equalsIgnoreCase("s")){
  316.                                 try{
  317.                                         otp4u.separate();
  318.                                 } catch(IOException ioe){
  319.                                         ioe.printStackTrace();
  320.                                 }
  321.                         } else if (args[0].equalsIgnoreCase("test")){
  322.                 try{
  323.                     /*   Io io = new Io();
  324.                          byte[] x = io.readFile(OTP4U_HOME + RANDOM_KEY + KEY4KEY +
  325.                          KEY4KEY_FILE);
  326.                          byte[] y = io.readFile(OTP4U_HOME + RANDOM_KEY + KEY4KEY +
  327.                          "test.rnd");
  328.                          io.writeFile(OTP4U_HOME + RANDOM_KEY + KEY4KEY+"autoKey.rnd",
  329.                          otp4u.autoKey(x, y));*/
  330.                 } catch(Exception e){
  331.                     e.printStackTrace();
  332.                 }
  333.                 /*            } else if (args[0].equalsIgnoreCase("t")){//testPermutation
  334.                               byte[] pr={1,2,3,4,5,6,7,8,9,10,11};
  335.                               otp4u.getKey4Message(pr,pr);*/
  336.             } else
  337.                 System.out.println(badParameter);
  338.         } else if (argsLen == 2)
  339.             if (args[0].equalsIgnoreCase("e"))
  340.                 otp4u.doEncryption(args[1]);
  341.             else if (args[0].equalsIgnoreCase("d"))
  342.                 otp4u.doDecryption(args[1]);
  343.             else if (args[0].equalsIgnoreCase("g")){
  344.                 try{
  345.                     length=Integer.parseInt(args[1])*1024;
  346.                 } catch (NumberFormatException nfe){
  347.                     nfe.printStackTrace();
  348.                     System.exit(-1);
  349.                 }
  350.                 otp4u.generateEntropyFile();
  351.             }
  352.             else if (args[0].equalsIgnoreCase("gk4k0")){
  353.                 try{
  354.                     length=Integer.parseInt(args[1])*1024;
  355.                 } catch (NumberFormatException nfe){
  356.                     nfe.printStackTrace();
  357.                     System.exit(-1);
  358.                 }
  359.                 otp4u.generateK4K0File();
  360.             }
  361.             else
  362.                 System.out.println(badParameter);
  363.         else
  364.             System.out.println("Wrong number of parameters "+argsLen);
  365.     }
  366.  
  367.     /**
  368.      *  Used to write the "public" key
  369.      */
  370.     public void writePublicKey(){
  371.         try{
  372.             keyManager.writePublicKey();
  373.  
  374.         }catch(Exception e){
  375.             e.printStackTrace();
  376.         }
  377.     }
  378.  
  379.  
  380.     /**
  381.      *  Used to write the random key (composed of K4Kn and K4Mn)
  382.      */
  383.     public void writeRandomKey(){
  384.         try{
  385.             keyManager.writeRandomKey();
  386.  
  387.         } catch (IOException ioe){
  388.             ioe.printStackTrace();
  389.             mp.sCatchException();
  390.         }
  391.     }
  392.  
  393.  
  394. }