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) - Io.java

Io.java

Caricato da: Teutoburgo
Scarica il programma completo

  1. /*
  2.     OTP4U 0.8.0 - 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.io;
  25.  
  26. import tk.teutoburgo.otp4u.constants.*;
  27. import tk.teutoburgo.otp4u.util.*;
  28. import java.io.*;
  29.  
  30. public class Io{
  31.         public static final String SEPARATOR=Constants.SEPARATOR,
  32.                 PUBLIC_KEY_FILE=Constants.PUBLIC_KEY_FILE,
  33.                 RANDOM_KEY_FILE=Constants.RANDOM_KEY_FILE,
  34.                 KEY_SMALL = Constants.KEY_SMALL,
  35.                 PLAIN= Constants.PLAIN, CIPHER=Constants.CIPHER,
  36.                 RANDOM_KEY=Constants.RANDOM_KEY, PUBLIC_KEY=Constants.PUBLIC_KEY,
  37.                 ENTROPY=Constants.ENTROPY;
  38.         public String OTP4U_HOME="";
  39.         boolean isGUI=false;
  40.         MessagesPrinter mp;
  41.  
  42.         public Io(){
  43.                 mp = new MessagesPrinter();
  44.         }
  45.  
  46.         public Io(String home, boolean isGUI){
  47.                 OTP4U_HOME=home;
  48.                 this.isGUI=isGUI;
  49.                 mp = new MessagesPrinter(isGUI);
  50. //System.out.println("\n---- honme io "+OTP4U_HOME);
  51.         }
  52.  
  53.     public byte[][] initArrays(String pathName) throws IOException {
  54.                 byte[][] arrays=new byte[2][];
  55.                 byte[] fileToXOR=null;
  56. //mp.sPrintLog("home "+OTP4U_HOME );
  57.                 fileToXOR = this.readFile(pathName);
  58.                 byte[] randomKeyBytes = this.readFile(OTP4U_HOME + RANDOM_KEY +
  59.                                                                         RANDOM_KEY_FILE);
  60.                 arrays[0]=fileToXOR;
  61.                 arrays[1]=randomKeyBytes;
  62.  
  63.                 return arrays;
  64.     }
  65.     public String getFileName (String pathName){
  66.       int index=pathName.lastIndexOf(Constants.SEPARATOR);
  67.       if (index!=-1)
  68.         pathName = pathName.substring(index+1);
  69.       return pathName;
  70.     }
  71.  
  72.     public byte[] readFile(String pathName) throws IOException {
  73.                 File f = new File(pathName);
  74.                 long fLength = f.length();
  75.                 byte[] file = new byte[(int) fLength];
  76.  
  77.             FileInputStream fis = new FileInputStream(f);
  78.             fis.read(file);
  79.  
  80.                 return file;
  81.     }
  82.     /**
  83.      * Reads the array pubKey containig the files in the entropySource directory
  84.      * @param pathName The pathname
  85.      * @return The array pubKey
  86.      */
  87.     public byte[][] readRandomKey(String pathName) throws IOException {
  88. //              mp.sPrintLog("read "+pathName);
  89.                 File f = new File(pathName);
  90.                 File[] list = f.listFiles();
  91.                 int len = list.length, i = 0, j = 0;
  92.                 long fLength = 0;
  93.                 byte[][] mngs = new byte[len][];
  94.                 mp.sPrintLog("Reading "+len+" entropy source file(s):");
  95.                 for (i = 0; i < len; i++) {
  96.                         mp.sPrintLog(list[i].toString());
  97.                         if (list[i].isFile()) {
  98.                                 fLength = list[i].length();
  99.                                 mngs[i] = new byte[(int) fLength];
  100.                                 (new FileInputStream(list[i])).read(mngs[i]);
  101.                                 j++;
  102.                         }
  103.                 }
  104.                 mp.sPrintLog("");
  105.                 byte[][] pubKey = new byte[j][];
  106.                 for (i = 0; i < j; i++) {
  107.                         pubKey[i] = mngs[i];
  108.                 }
  109.                 return pubKey;
  110.     }
  111.  
  112.         public void writeFile(String name, byte[] bytes) throws FileNotFoundException,
  113.                 IOException {
  114.  
  115.                 FileOutputStream fos = new FileOutputStream(name);
  116.                 fos.write(bytes);
  117.                 mp.sPrintLog("Written "+name);
  118.         }
  119.  
  120. }