/*
Sysepub 0.9 - A Symmetric Semi-Public key cipher
Copyright (C) 2002 Pierre Blanc
Pierre Blanc: blanc_teutoburgo@yahoo.it
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
*/
import java.util.*;
/**
* Class A1timeP (a one time pad key generator)
* Creation date: (30/08/2002 10.35.14)
* @author: Pierre Blanc
*/
public class A1timeP {
public static final long MSEC_DAY=86400000;
int period;
/**
* A1timeP constructor comment.
*/
public A1timeP() {
super();
}
/**
* A1timeP constructor comment.
*/
public A1timeP(int period) {
super();
this.period=period;
}
/**
* Using the Pseudo-Random Number Generator provided by the class java.util.Random,
* initialized with the secret seed and the current period (of length user-specified),
* this method returns a pseudo-random byte array
* @param l The length of the array
* @param seed The secret seed
* @return The pseudo-random byte array
*/
public byte[] getBytes(int l, long seed) {
byte[] random = new byte[l];
long current
=System.
currentTimeMillis();
long deviation=current/(period*MSEC_DAY);
rnd.setSeed(seed+deviation);
rnd.nextBytes(random);
return random;
}
/**
* Returns a pseudo-random integer using the java.util.Random class initialized
* with the secret seed and the current period (of length user-specified)
* @param seed The secret seed
* @param max the maximum value of the positive integer
* @return The pseudo-random positive integer
*/
public int getInt(long seed, int max){
long current
=System.
currentTimeMillis();
long deviation=current/(period*MSEC_DAY);
rnd.setSeed(seed+deviation);
return rnd.nextInt(max);
}
/**
* Returns a pseudo-random byte array taking back time up to a desired number
* of periods. Used by Sysepub.tryDecryption
* @param l The length of the array
* @param seed The secret seed
* @param periods the number of tries desired
* @return The pseudo-random byte array
*/
public byte[] getOtherBytes(int l, long seed, int periods) {
byte[] random = new byte[l];
long current
=System.
currentTimeMillis();
long deviation=(current-periods*MSEC_DAY)/(period*MSEC_DAY);
rnd.setSeed(seed+deviation);
rnd.nextBytes(random);
return random;
}
/**
* Returns a pseudo-random positive integer taking back time up to a desired number
* of periods. Used by Sysepub.tryDecryption
* @param max the maximum value of the positive integer
* @param seed The secret seed
* @param periods the number of tries desired
* @return The pseudo-random byte array
*/
public int getAnotherInt(long seed, int max, int periods){
long current
=System.
currentTimeMillis();
long deviation=(current-periods*MSEC_DAY)/(period*MSEC_DAY);
rnd.setSeed(seed+deviation);
return rnd.nextInt(max);
}
}