Package org.andrearaso.redvernam

Source Code of org.andrearaso.redvernam.NumVernam

/*
� 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
*/

package org.andrearaso.redvernam;

import java.lang.String;

/**
* <p>
* Title: RedVernam
* </p>
* <p>
* Description: Encryption Software
* </p>
* <p>
* Copyright: Copyleft (c) 2009
* </p>
* <p>
* Company: andrearaso.org
* </p>
*
* @author Andrea Raso
* @version aplha
*/

/* ---------------------------------------------------------------- */
/* NUMVERNAM: l'algoritmo di cifratura a numeri */
/* ---------------------------------------------------------------- */
/* Il Funzionamento ? il medesimo della classe Vernam. */
/* L'unica differenza ? nel fatto che l'output non sono caratteri ma */
/* Numeri separati da un '-' . */
/* Per questo motivo sono commentati solo le parti differenti */

public class NumVernam extends Vernam {
  public NumVernam()  {
  }

  public static String encrypt(String chiaro, String chiave) {
    int i = 0;
    char aC[], bC[];
    aC = chiaro.toCharArray();
    bC = chiave.toCharArray();
    int n = chiaro.length();
    int m = chiave.length();
    int a[] = new int[n];
    int b[] = new int[m];
    int c[] = new int[n];
    String cifrato = new String("");

    while (i < n) {
      a[i] = (int) aC[i];
      b[i] = (int) bC[i];
      c[i] = (a[i] + b[i]) % 255;
      cifrato = cifrato + c[i] + '-'; // L'ouput questa volta ? una
                      // stringa del tipo
                      // (123-456-789-...)
      i++;
    }

    return cifrato;
  }

  public static String decrypt(String chiave, String cifrato) {

    int j = 0, k = 0, count = 0;
    String tmp = new String("");
    char bC[], cC[];
    bC = chiave.toCharArray();
    cC = cifrato.toCharArray();
    int m = chiave.length();
    int cifratoLenght = m;
    int b[] = new int[m];
    int c[] = new int[m];
    int d[] = new int[m];
    boolean ok = false;
    for (j = 0; j < m; j++) {

      try {
        for (k = 0; k < 3; k++) { // Questo ciclo carica i numeri
                      // scritti nella forma 123-234-...
                      // uno alla voltain una stringa
                      // temporanea
          if (cC[count + k] == '-') {
            ok = false;
            break;
          }
          tmp = tmp + cC[count + k];
          ok = true;
        }
      } catch (Exception e) {
        cifratoLenght = j;
        break;
      }

      if (ok)
        try {
          c[j] = Integer.parseInt(tmp);
        } // A questo punto carico la stringa ottenuta nell'array di
          // interi
        catch (Exception e) {
        }
      count = count + tmp.length() + 1;
      tmp = "";
    }
    String decifrato = new String("");
    int i = 0;
    while (i < m) {
      b[i] = (int) bC[i];
      d[i] = ((c[i] - b[i]) + 255) % 255;
      decifrato = decifrato + (char) d[i]; // Ritrasformo i numeri in
                          // caratteri e li carico
                          // nella stringa di output
      i++;
    }
    return decifrato.substring(0, cifratoLenght);
  }

}// Andrea Raso
TOP

Related Classes of org.andrearaso.redvernam.NumVernam

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.