1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
  JaVi - JavaVigenere -   A Java implementation of the Vigenere
                                cryptographical  algorithm
        Un'implementazione Java dell'algoritmo di
                                Vigenere per la crittografia
    Copyright (C) 2002 Pierre Blanc
    Pierre Blanc: pierre@trek.eu.org , 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.

    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


    Questo  programma è  software  libero; è  lecito redistribuirlo  o
    modificarlo secondo i termini  della Licenza Pubblica Generica GNU
    come è pubblicata dalla Free  Software Foundation, o la versione 2
    della licenza.
    Questo programma  è distribuito nella  speranza che sia  utile, ma
    SENZA  ALCUNA GARANZIA;  senza  neppure la  garanzia implicita  di
    NEGOZIABILITĄ  o di  APPLICABILITĄ PER  UN PARTICOLARE  SCOPO.  Si
    veda la Licenza Pubblica Generica GNU per avere maggiori dettagli.

    Questo  programma deve  essere  distribuito assieme  ad una  copia
    della Licenza Pubblica Generica GNU;  in caso contrario, se ne può
    ottenere  una scrivendo  alla Free  Software Foundation,  Inc., 59
    Temple Place, Suite 330, Boston, MA 02111-1307 USA  oppure da
    http://www.gnu.org/copyleft/gpl.html


    Un grazie sentito a Roberto detto "Piano Man" per l'importante aiuto.

 * Title:        JaVi BO<p>
 * Copyright:    Copyright (c) 2002 Pierre Blanc<p>
 * @author Pierre Blanc
 * @version 0.6
 */
package JaVi;

/* La classe contenente l'algoritmo di Vigenere per cifrare e decifrare
   The class containing the Vigenere algorithm to enctypt and decrypt   */

public class Vigenere {

/*   int Unic0=32 (codice Unicode per ' '), UnicZ=122 (codice Unicode per 'Z');
     I caratteri ammessi cadono nell'intervallo di codice Unicode 32-122.
     Se si volesse modificare quest'intervallo bisognerebbe modificare il
     sorgente.

     The accepted characters are within the Unicode set 32-122.
     To modify this set you need to modify the source code.
*/

  
public Vigenere() {
  }

/*  Il metodo getEncryptedPhrase serve per cifrare la frase in chiaro
    (String phrase) attraverso la chiave (String key).

    The method getEncryptedPhrase is to encrypt the phrase (String phrase)
    using the key (String key).
*/
  
public String getEncryptedPhrase (String phrase, String key){
    
int Unic0=32, UnicZ=123;
    String tmp=
"", ad="";
    String cryptedPhrase=
"";
    
char c,k;

    
int lung1=phrase.length();
    
int lung2=key.length();
    
if(lung1>lung2) {
  tmp=key;
  
for (int i=0; i<lung1/lung2; i++){
    tmp=tmp+key;
  }
  key=key+tmp;
    }

/*   In questo ciclo viene applicato l'algoritmo: vengono sommati i codici
     Unicode lettera per lettera della frase in chiaro e della chiave il
     risultato modulo (UnicZ-Unic0) in modo che il risultato sia ancora un
     codice Unicode compreso tra 48 e 122.

     In this cycle is applied the algorithm: the Unicode numbers of the phrase
     and of the key are summed character per character and the result is
     computed modulo (UnicZ-Unic0), so that the result is yet in
     the Unicode set 48-122.
*/
    
for (int i=0; i<lung1; i++){
      c=phrase.charAt(i);
      k=key.charAt(i);
      Character r=
new Character((char)(((c+k)%(UnicZ-Unic0)+Unic0)));
      cryptedPhrase=cryptedPhrase+r;
    }

    
return(cryptedPhrase);
  }

/*    Il metodo getPhrase serve per ottenere la frase in chiaro avendo la frase
      cifrata (String cryptedPhrase) e la stessa chiave usata per cifrare
      (String key)

  The method getPhrase is to get the phrase using the encrypted phrase
  (String encryptedPhrase) and the same key used to encrypt (String key)
*/

  
public String getPhrase (String cryptedPhrase, String key){
    
int temp=0;
    
int Unic0=32, UnicZ=123;
    
int mod=UnicZ-Unic0;
    String ad=
"", tmp="";
    String phrase=
"";
    
char r,k;

    
int lung1=cryptedPhrase.length();
    
int lung2=key.length();
    
if(lung1>lung2) {
  tmp=key;
  
for (int i=0; i<lung1/lung2; i++){
    tmp=tmp+key;
  }
  key=key+tmp;
    }
/*    In questo ciclo viene applicato l'algoritmo inverso: viene fatta la
      differenza modulo (UnicZ-Unic0) dei codici Unicode.

      In this cycle is applied the inverse algorithm: it is done the difference
      modulo (UnicZ-Unic0) between the Unicode numbers.
*/

    
for (int i=0; i<lung1; i++){
        r=cryptedPhrase.charAt(i);
        k=key.charAt(i);
        temp=((r-Unic0-k+
4*mod)%(mod));
        
if (temp<Unic0) temp+=mod;
        Character c=
new Character((char)temp);
        phrase=phrase+c;

    }

    
return(phrase);
  }

/*    Questo main serve unicamente per testare i metodi di questa classe.

  This main method is presented only to test this class' methods.
*/

  
public static void main(String[] args) {
 
//   System.out.println(-23%74);

    
System.out.println(Character.getNumericValue(' '));
/*
    Vigenere vigenere1 = new Vigenere();
    char pi='C'+'0';
    String chiave="ffffffffffffffffffffff";
//    System.out.println(pi+" "+new Character((char)(((pi)%74)+48)));
    String kr=vigenere1.getEncryptedPhrase("0Cicci333xyz",chiave);
    System.out.println(kr);
    System.out.println(vigenere1.getPhrase(kr,chiave));
*/
  
}
}
Java2html