/*
JaVi - JavaVigenere - A Java implementation of the Vigenere
cryptographical algorithm
Un'implementazione Java dell'algoritmo di
Vigenere per la crittografia
Copyright (C) 2002-2010 Pierre Blanc
Pierre Blanc: blanc_teutoburgo@yahoo.it
http://www.teutoburgo.tk (italiano)
http://teutoburgo.web44.net/indexEn.html (english)
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.
*/
package JaVi;
import java.util.Vector;
import java.util.Random;
/**
*
* <p>Title: JaVi BO</p>
* <p>Description: La classe contenente l'algoritmo di Vigenere per cifrare e decifrare
* <br> The class containing the Vigenere algorithm to encrypt and decrypt</p>
* <p>Copyright: Copyright (c) 2002 Pierre Blanc</p>
* <p>Company: Teutoburgo</p>
* @author Pierre Blanc
* @version 1.02
*/
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.
*/
int Unic0=32, UnicZ=123;
public Vigenere() {
}
public Vigenere
(int u0,
int uZ,
String methodKey
) {
Unic0=u0;
UnicZ=uZ;
mKey=methodKey;
}
/** Il metodo getEncryptedPhrase serve per cifrare la frase in chiaro
(String phrase) attraverso la chiave (String key).
<br>
The method getEncryptedPhrase is to encrypt the phrase (String phrase)
using the key (String key).
@param phrase The phrase to be encrypted
@param key The key for the algorithm
@return The encrypted phrase
*/
char c,k;
int lung1=phrase.length();
int lung2=key.length();
if (lung2==0) return ("Insert Key! - Inserire Chiave!");
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 32 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 32-122.
*/
for (int i=0; i<lung1; i++){
c=phrase.charAt(i);
k=key.charAt(i);
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)
<br>
The method getPhrase is to get the phrase using the encrypted phrase
(String encryptedPhrase) and the same key used to encrypt (String key)
*
* @param cryptedPhrase the encrypted phrase
* @param key the key
* @return the plain text phrase
*/
int temp=0;
int mod=UnicZ-Unic0;
char r,k;
int lung1=cryptedPhrase.length();
int lung2=key.length();
if (lung2==0) return ("Insert Key! - Inserire Chiave!");
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));
while (temp<Unic0) temp+=mod;
phrase=phrase+c;
}
return(phrase);
}
/**
* Prende il double restituito da getKeyStrength e lo converte in un int
tra 0 e 100 per dare la percentuale di forza
<br>
Converts the double returned by getKeyStrength into an int from 0 to 100
to give the strength in percent
* @param key the key
* @param accur the accuracy. The more is big, the more JaVi is slow.
* @return the key strength from 0 to 100.
*/
public int getKSPerCent
(String key,
int accur
){
double kS=100-100*getKeyStrength(key, accur);
if (kS<0) kS=0;
return (int)kS;
}
/**
* Restituisce un double tra 0 e 1 per valutare la forza della chiave.
Prende la chiave key, l'accuratezza accur (0 è il massimo della forza,
1 è la forza peggiore)
<br>
Returns a double from 0 to 1 to evalue the strength of the key.
The parameters are the key key, the accuracy accur
(0 is the maximum strength, 1 is the minimum strength)
* @param key the key
* @param accur the accuracy
* @return a double from 0 to 1
*/
public double getKeyStrength
(String key,
int accur
){
double [] devDepth=new double[accur];
double [] freqStringhe=new double[10000];
int kMax=0, l=key.length();
int dim,j=0;
double dDtmp=0, dP;
double fMediaAtt=0;
double devMedia=0, devStand=0, devStandAtt=0;
int q=UnicZ-Unic0;
if (l==0) return 1;
if (l-3<accur) accur=l-3;
if (accur<1) accur=1;
for (int d=0; d<accur; d++){
freqStringheVect=trovaStringhe(key,d,q);
dim=freqStringheVect.size();
for (j=0; j<dim;j++){
freqStringhe[j]=((FrequenzaNijk)freqStringheVect.elementAt(j)).getFreq();
}
kMax
=(int)Math.
pow(q,d
+1
);
dP
=(double)Math.
min(kMax,l
-d
);
fMediaAtt=1/dP;
for (j=0; j<dim; j++)
dDtmp
=dDtmp
+Math.
pow((freqStringhe
[j
]-fMediaAtt
),2
);
devStand
=Math.
sqrt(dDtmp
+(dP
-dim
)*Math.
pow(fMediaAtt,2
));
/*come misura della forza viene usata la deviazione standard della frequenze
dalla frequenza teorica attesa
in order to evalue the strength, the standard deviation of the frequencies
from the expected frequency is computed*/
devDepth[d]=devStand;
dDtmp=0;
/******* uncomment to view in the console the standard deviations
System.out.println("Depth: "+d+" Standard deviation: "+devStand);
*/
}
if (mKey.equals("MAX"))
return maxKeyStrength(key, devDepth, accur);
else if (mKey.equals("AVE")) return averageKeyStrength(key, devDepth, accur);
else return defaultKeyStrength(key, devDepth, accur);
}
/*i metodi defaultKeyStrength, minKeyStrength, averageKeyStrength
servono per staccare il calcolo della forza globale dal calcolo delle forze
considerando solo le stringhe di lunghezza j, che vengono passate nell'array
dD. Quello che cambia è il peso che viene dato alle forze di profondità j:
nel default il peso maggiore è dato alla forza di profondità j=1, per j=2 il
peso è la metà del peso per j=1, per j=3 la metà di j=2 e cosi' via
the methods defaultKeyStrength, min.. , average.. are used to divide the
computing of the global strength from the computing of the strength obtained
by considering only the strings of length j, that are passed in the array
dD. It is different the weigth given to the strength of depth j: the
defaultKeyStrength gives the maximum weigth to the strength of depth j=1,
the half for j=2, and so on.. */
public double defaultKeyStrength
(String key,
double [] dD,
int dM
){
double kS=0;
for (int j=0;j<dM;j++)
kS
=kS
+dD
[j
]/Math.
pow(2,j
+1
);
return kS;
}
/**
* nel maxKeyStrength viene considerata unicamente la forza peggiore,
ovvero la massima (1 è la peggiore, 0 la migliore in assoluto)
<br>
in maxKeyStrength it is considered only the worst strength, i.e. the
maximum (1 is the worst, 0 the best)
* @param key the key
* @param dD an array containing the strengths for the levels 1,2,...,dM
* @param dM the accuracy
* @return a double from 0 to 1
*/
public double maxKeyStrength
(String key,
double [] dD,
int dM
){
double kS=0;
for (int j=0;j<dM;j++)
if (kS<dD[j]) kS=dD[j];
return kS;
}
/**
* nell'average viene restituita la forza media: è il metodo meno affidabile
averageKeyStrength returns the average Strength: it's the less accurate
method
* @param key the key
* @param dD an array containing the strengths for the levels 1,2,...,dM
* @param dM the accuracy
* @return a double from 0 to 1
*/
public double averageKeyStrength
(String key,
double [] dD,
int dM
){
double kS=0;
int l=key.length();
if (l<dM) dM=l;
for (int j=0;j<dM/3;j++)
kS=kS+dD[j];
kS=3*kS/dM;
return kS;
}
/**
* il metodo restituisce un Vector di oggetti FrequenzaNijk
<br>
returns a Vector of objects FrequenzaNijk
* @param key the key
* @param k the depth level (length of the String)
* @param q the symbols in the alphabet (UnicZ-Unic0)
* @return a Vector containing FrequenzaNijk objects
*/
int kMax
=(int)Math.
pow(q,k
+1
);
int kMin
=(int)Math.
pow(q,k
);
double v;
FrequenzaNijk fNijk=new FrequenzaNijk();
int l=key.length();
int r=l-k;
int j,jMax=0;
double incr=1/((double)l-k);
boolean trovato=false;
FrequenzaNijk vt=new FrequenzaNijk();
for (int i=0; i<r; i++){
trovato=false;
trovata=key.substring(i,i+k+1);
j=0;
while(!trovato && j<jMax){
vt=(FrequenzaNijk)vk.elementAt(j);
if(vt.getNijk().equals(trovata))
{
v=vt.getFreq();
vk.setElementAt(new FrequenzaNijk(trovata,v+incr),j);
trovato=true;
}
j++;
}
if (!trovato) {
vk.addElement(new FrequenzaNijk(trovata,incr));
jMax++;
}
}
int dim=vk.size();
return vk;
}
/* 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();
Random r=new Random();
/* System.out.println(vigenere1.getKeyStrength("aaaaaa",5));
for (int i=0; i<500; i++){
System.out.print((char)(32+r.nextInt(92)));
}*/
/* for (int i=0; i<91; i++){
System.out.print((char)(32+i%91));
System.out.print((char)(32+i%91));
System.out.print((char)(32+i%91));
System.out.print((char)(32+i%91));
System.out.print((char)(32+i%91));
}*/
/* for (int i=32; i<124; i++){
System.out.print((char)i);
}
for (int i=123; i>31; i--){
System.out.print((char)i);
}
/* System.out.println(vigenere1.getKeyStrength(
"ZoC%m]n.Y-q'gB'{@Xv/r;2%=kS.I.b@ka3mWG*utfq+{)no&F)[a;k,sWa6y.4eUh:xjV7SI#]/",12,92));
/* 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));
*/
}
}