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

RandomGenerator.java

Caricato da: Teutoburgo
Scarica il programma completo

  1. /*
  2.     OTP4U 0.9.3 - 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.crypt;
  25.  
  26. import java.io.File;
  27. import java.security.SecureRandom;
  28.  
  29. import tk.teutoburgo.otp4u.constants.Constants;
  30. import tk.teutoburgo.otp4u.io.Io;
  31. import tk.teutoburgo.otp4u.util.MessagesPrinter;
  32.  
  33. /**
  34.  * <p>Title: OTP4U</p>
  35.  * <p>Description: One Time Pad for you</p>
  36.  * <p>Copyright: Copyright (c) 2002</p>
  37.  * <p>Company: Teutoburgo</p>
  38.  * @author Pierre Blanc
  39.  * @version 1.0
  40.  */
  41.  
  42. public class RandomGenerator {
  43.  
  44.   String OTP4U_HOME="";
  45.   MessagesPrinter mp;
  46.   //Random rnd = new Random();
  47.   //12 Apr 2013 used SecureRandom instead of insecure Random
  48.   SecureRandom rnd = new SecureRandom();
  49.   Io io;
  50.   boolean isGUI=false;
  51.   public static final String PSEUDO_RANDOM=Constants.PSEUDO_RANDOM;
  52.  
  53.   public RandomGenerator() {
  54.   }
  55.   public RandomGenerator(String home, boolean isGUI){
  56.           OTP4U_HOME=home;
  57.           this.isGUI=isGUI;
  58.           mp = new MessagesPrinter(isGUI);
  59.           io = new Io(home, isGUI);
  60.   }
  61.  
  62.  
  63.     /**
  64.      * Used to generate a PSEUDO-random file in the entropySource directory
  65.      * or in the key4key directory.
  66.      * Use it to test the functionalities of OTP4U.
  67.      * @param pathName the path name of the pseudo-random file
  68.      * @param fileName the file name
  69.      * @param length the file length
  70.      */
  71.     public void generateRandomFile(String pathName, String fileName,
  72.                                    int length){
  73.         String generated="";
  74.         int fLength=fileName.length();
  75.         int max=0;
  76.         byte[] randomBytes = new byte[length];
  77.         rnd.nextBytes(randomBytes);
  78.         if(fileName.equals(PSEUDO_RANDOM)){
  79.             File f = new File(pathName);
  80.             File[] list = f.listFiles();
  81.             try{
  82.                 for (int i=0; i<list.length; i++){
  83.                     if (!(fLength>list[i].getName().toString().length())){
  84.                         if(list[i].getName().toString().substring(
  85.                           0,fLength).equals(fileName))
  86.                             max++;
  87.                     }
  88.                 }
  89.             }catch (Exception e){
  90.                 e.printStackTrace();
  91.             }
  92.             generated=pathName+fileName+(max+1)+".rnd";
  93.         } else
  94.             generated=pathName+fileName;
  95.         try{
  96.             io.writeFile(generated,randomBytes);
  97.         }catch (Exception e){
  98.             e.printStackTrace();
  99.         }
  100.         mp.sPrintLog("Generated "+length+" pseudo random bytes in file "+
  101.           generated);
  102.     }
  103.  
  104. }