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

KeyPermutator.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.crypt;
  25.  
  26. import tk.teutoburgo.otp4u.io.*;
  27. import tk.teutoburgo.otp4u.util.*;
  28.  
  29. /**
  30.  * Title:        OTP4U
  31.  * Description:
  32.  * Copyright:    Copyright (c) 2002
  33.  * Company:      Teutoburgo
  34.  * @author Pierre Blanc
  35.  * @version 1.0
  36.  */
  37.  
  38. public class KeyPermutator {
  39.  
  40.         Xor xor = new Xor();
  41.  
  42.   public KeyPermutator() {
  43.   }
  44.  
  45.         public byte[][] recurrence(byte[] p, byte[] k){
  46.                 int n, b=0, v=0, kf=0, c, I, l=p.length, kLen=k.length, i=0, tot=l-kLen,
  47.                         aRes=100, pf=0;
  48.                 byte[] r=new byte[tot], xorArr=new byte[kLen], pPos=new byte[l];
  49.                 byte[][] rAndK = new byte[2][];
  50.  
  51. //              System.out.println("inizio b="+b+" kf"+kf+"r[0]="+r[0]+" "+(char)r[0]);
  52.                 for(n=0; n<tot; n++){
  53.               c=128+p[b%l]+(128+k[b%kLen])*256+
  54.                           (128+p[(b+1)%l])*65536+(128+k[(b+1)%kLen])*123456;
  55.                         v=128+p[c%l]+(128+k[c%kLen])*256+
  56.                           (128+p[(c+1)%l])*65536+(128+k[(c+1)%kLen])*123456;
  57.                        // System.out.println("v="+v);
  58.                         b=((b+v)%l+l)%l;
  59.                         r[n]=p[b];
  60.                         l--;
  61.                         p[b]=p[l];
  62.                         /*if(n%10000==0)
  63.                                 System.out.println("\nn "+n+" b="+b+"r[n]="+r[n]+" "+(char)r[n]+
  64.                                         " p[l-1]="+p[l-1]+" l="+l+"\nv="+v);*/
  65.                 }
  66. //System.out.print("size "+p.length+" rl="+r.length+" kl="+kLen+"\nv="+v);
  67.                 System.arraycopy(p, 0, k, 0, kLen);
  68.                 rAndK[0] = r;
  69.                 rAndK[1] = k;
  70.                 return rAndK;
  71.         }
  72.  
  73.         public static void main(String args[]) {
  74.                 String SSP_HOME="d:/";
  75.                 Io io = new Io();
  76.                 KeyPermutator it = new KeyPermutator();
  77.                 MessagesPrinter pm = new MessagesPrinter();
  78.                         pm.printCopyright();
  79.                 int argsLen = args.length;
  80.                 String badParameter="Bad parameter.. type: java Sysepub -h";
  81.                 try{
  82.                         byte[] file=io.readFile(SSP_HOME+"prova.rnd");
  83.                         byte[] k=io.readFile(SSP_HOME+"k.rnd");
  84.                         byte[][] prova=it.recurrence(file, k);
  85.         //System.out.print("adesso size "+file.size());
  86.                         io.writeFile(SSP_HOME+"out.rnd",prova[0]);
  87.                         io.writeFile(SSP_HOME+"k1.rnd",prova[1]);
  88.                 }catch(Exception e){
  89.                         e.printStackTrace();
  90.                 }
  91.     }
  92.  
  93.  
  94. }