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
Sysepub - A1timeP.java

A1timeP.java

Caricato da: Teutoburgo
Scarica il programma completo

  1. /*
  2.     Sysepub 0.9 - A Symmetric Semi-Public key cipher
  3.     Copyright (C) 2002 Pierre Blanc
  4.     Pierre Blanc: blanc_teutoburgo@yahoo.it
  5.     http://it.geocities.com/teutoburgo
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20.     or go to      http://www.gnu.org/copyleft/gpl.html
  21. */
  22. import java.util.*;
  23. /**
  24.  * Class A1timeP (a one time pad key generator)
  25.  * Creation date: (30/08/2002 10.35.14)
  26.  * @author: Pierre Blanc
  27.  */
  28. public class A1timeP {
  29.     Random rnd = new Random();
  30.     public static final long MSEC_DAY=86400000;
  31.     int period;
  32. /**
  33.  * A1timeP constructor comment.
  34.  */
  35. public A1timeP() {
  36.         super();
  37. }
  38. /**
  39.  * A1timeP constructor comment.
  40.  */
  41. public A1timeP(int period) {
  42.         super();
  43.         this.period=period;
  44. }
  45. /**
  46.  * Using the Pseudo-Random Number Generator provided by the class java.util.Random,
  47.  * initialized with the secret seed and the current period (of length user-specified),
  48.  * this method returns a pseudo-random byte array
  49.  * @param l The length of the array
  50.  * @param seed The secret seed
  51.  * @return The pseudo-random byte array
  52.  */
  53. public byte[] getBytes(int l, long seed) {
  54.     byte[] random = new byte[l];
  55.     long current=System.currentTimeMillis();
  56.     long deviation=current/(period*MSEC_DAY);
  57.     rnd.setSeed(seed+deviation);
  58.     rnd.nextBytes(random);
  59.  
  60.     return random;
  61. }
  62. /**
  63. *       Returns a pseudo-random integer using the java.util.Random class initialized
  64. *       with the secret seed and the current period (of length user-specified)
  65. * @param seed The secret seed
  66. * @param max the maximum value of the positive integer
  67. * @return The pseudo-random positive integer
  68. */
  69. public int getInt(long seed, int max){
  70.     long current=System.currentTimeMillis();
  71.     long deviation=current/(period*MSEC_DAY);
  72.     rnd.setSeed(seed+deviation);
  73.     return rnd.nextInt(max);
  74. }
  75. /**
  76. *       Returns a pseudo-random byte array taking back time up to a desired number
  77. *       of periods. Used by Sysepub.tryDecryption
  78. * @param l The length of the array
  79. * @param seed The secret seed
  80. * @param periods the number of tries desired
  81. * @return The pseudo-random byte array
  82. */
  83. public byte[] getOtherBytes(int l, long seed, int periods) {
  84.     byte[] random = new byte[l];
  85.     long current=System.currentTimeMillis();
  86.     long deviation=(current-periods*MSEC_DAY)/(period*MSEC_DAY);
  87.     rnd.setSeed(seed+deviation);
  88.     rnd.nextBytes(random);
  89.  
  90.     return random;
  91. }
  92. /**
  93. *       Returns a pseudo-random positive integer taking back time up to a desired number
  94. *       of periods. Used by Sysepub.tryDecryption
  95. * @param max the maximum value of the positive integer
  96. * @param seed The secret seed
  97. * @param periods the number of tries desired
  98. * @return The pseudo-random byte array
  99. */
  100. public int getAnotherInt(long seed, int max, int periods){
  101.     long current=System.currentTimeMillis();
  102.     long deviation=(current-periods*MSEC_DAY)/(period*MSEC_DAY);
  103.     rnd.setSeed(seed+deviation);
  104.     return rnd.nextInt(max);
  105. }
  106. }