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

Xor.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 java.io.IOException;
  27. public class Xor {
  28. /**
  29.  * Xor constructor comment.
  30.  */
  31. public Xor() {
  32.         super();
  33. }
  34.     public byte[] decrypt(byte[] encrypted, byte[] key) {
  35.                 int i=0;
  36.         int length= encrypted.length;
  37.         int keyLength=key.length;
  38.         byte[] message = new byte[length];
  39.  
  40.         for (i = 0; i < length && i<keyLength ; i++) {
  41.           message[i]=(byte)(encrypted[i]^key[i]);
  42.         }
  43.  
  44.         return message;
  45.     }
  46. public byte[] encrypt(byte[] message, byte[] key) {
  47.     int length=message.length;
  48.     byte[] encrypted = new byte[length];
  49.  
  50.     for (int i = 0; i < length && i<key.length; i++) {
  51.       encrypted[i]=(byte)(message[i]^key[i]);
  52.     }
  53.     return encrypted;
  54. }
  55. public static void main(String args[]){
  56.   Xor s= new Xor();
  57.   tk.teutoburgo.otp4u.application.OTP4U o =
  58.           new tk.teutoburgo.otp4u.application.OTP4U();
  59.  
  60.  /* byte[] x = {2,6,4,6,0,6,4,6};
  61.   byte[] y = {11,3,1,7,1,3,1,15};
  62.   byte[] a = s.encrypt(x,y);
  63. //  byte[] a = o.autoKey(x);
  64.   /*  byte[] mng = new byte[10];*/
  65.  /* for (int i = 0; i < a.length; i++) {
  66.     System.out.print("i="+i+" a[i]="+a[i]+"   ");
  67.   }
  68.   /*byte [] test=s.encrypt(x, mng);
  69.   byte [] testd=s.decrypt(test, mng);
  70.   try{
  71.     System.out.write(x);
  72.     System.out.println();
  73.     System.out.write(mng);
  74.     System.out.println("result");
  75.     System.out.write(test);
  76.     System.out.println("result2");
  77.     System.out.write(testd);
  78.  
  79.   } catch (IOException ioe){
  80.     ioe.printStackTrace();
  81.   }
  82. */
  83. }
  84. }