/*
JaVi v1.0 - 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.awt.*;
import java.awt.event.*;
import java.applet.*;
/**
*
* <p>Title: JaVi Applet</p>
* <p>Description: The class implementing the JaVi applet. It requires
* the Vigenere and the FrequenzaNijk objects</p>
* <p>Copyright: Copyright (c) 2002 Pierre Blanc</p>
* <p>Company: Teutoburgo</p>
* @author Pierre Blanc
* @version 1.0
*/
public class Javi2Applet
extends Applet {
int defAcc=30;
boolean isTAKEditable = true;
boolean isTAKVisible = true;
boolean isStandalone = false;
boolean isEvaluable = true;
Vigenere vi;
/**
* Get a parameter value
* @param key
* @param def
* @return the value
*/
return isStandalone
? System.
getProperty(key, def
) :
(getParameter(key) != null ? getParameter(key) : def);
}
/**Construct the applet*/
public Javi2Applet() {
}
/**Initialize the applet*/
public void init() {
try {
jbInit();
}
e.printStackTrace();
}
}
/**
* Component initialization
* @throws Exception
*/
String accuracy
=this.
getParameter("ACCURACY",
"30");
String methodKey
=this.
getParameter("Method",
"MAX");
String UnicFirst
=this.
getParameter("UnicodeFirstChar",
"32");
String UnicLast
=this.
getParameter("UnicodeLastChar",
"123");
int Unic0
=Integer.
parseInt(UnicFirst
);
int UnicZ
=Integer.
parseInt(UnicLast
);
vi=new Vigenere(Unic0, UnicZ, methodKey);
labelGauge2.
setForeground(Color.
black);
this.resize(620,380);
this.setLayout(borderLayout1);
panelLanguage.setLayout(gridLayout3);
buttonEnglish.
setBackground(new Color(0, 192, 138
));
buttonEnglish.setLabel("English");
buttonEnglish.
addMouseListener(new java.
awt.
event.
MouseAdapter() {
buttonEnglish_mouseClicked(e);
}
});
buttonItaliano.
setBackground(new Color(255, 138, 0
));
buttonItaliano.
setForeground(Color.
white);
buttonItaliano.setLabel("Italiano");
buttonItaliano.
addMouseListener(new java.
awt.
event.
MouseAdapter() {
buttonItaliano_mousePressed(e);
}
});
panelLanguage.
setBackground(Color.
gray);
panel1.
setBackground(Color.
blue);
panel1.setLayout(gridLayout1);
panelPhrase.setLayout(flowLayout1);
labelPhrase.
setForeground(Color.
yellow);
panelKey.setLayout(flowLayout2);
buttonCrypt.
addMouseListener(new java.
awt.
event.
MouseAdapter() {
buttonCrypt_mouseClicked(e);
}
});
textAreaKey.
setText(Double.
toString(java.
lang.
Math.
PI)+
textAreaKey.
setBackground(Color.
lightGray);
textAreaKey.setColumns(35);
textAreaKey.setEditable(isTAKEditable);
textAreaKey.setVisible(isTAKVisible);
textAreaKey.
addTextListener(new java.
awt.
event.
TextListener() {
textAreaKey_textValueChanged(e);
}
});
buttonDecrypt.
addMouseListener(new java.
awt.
event.
MouseAdapter() {
buttonDecrypt_mouseClicked(e);
}
});
panelEncrypted.setLayout(flowLayout3);
labelEncrypted.
setForeground(Color.
yellow);
textArea3.
setBackground(Color.
blue);
textArea3.setColumns(35);
textArea3.
setForeground(Color.
yellow);
textArea3.setText("");
panelCheck.setLayout(flowLayout6);
panelVisible.setLayout(flowLayout4);
checkbox1.
addMouseListener(new java.
awt.
event.
MouseAdapter() {
checkbox1_mousePressed(e);
}
});
panelEditable.setLayout(flowLayout5);
checkbox2.
addMouseListener(new java.
awt.
event.
MouseAdapter() {
checkbox2_mousePressed(e);
}
});
panelKey.
setBackground(Color.
pink);
panelCheck.
setBackground(Color.
gray);
panelVisible.
setBackground(Color.
gray);
panelEditable.
setBackground(Color.
gray);
buttonReset.
addMouseListener(new java.
awt.
event.
MouseAdapter() {
buttonReset_mouseClicked(e);
}
});
textArea1.
addMouseListener(new java.
awt.
event.
MouseAdapter() {
textArea1_mouseClicked(e);
}
});
textArea1.
setBackground(Color.
blue);
textArea1.setColumns(35);
textArea1.
setForeground(Color.
yellow);
panelAbout.setLayout(gridLayout2);
label3.setText("Copyright (C) P.Blanc 2002 - blanc_teutoburgo@yahoo.it - "+
"http://www.teutoburgo.tk");
panel2.setLayout(flowLayout7);
label2.setText("b<AE=<");
englishVersion();
panelGauge.setLayout(gridLayout4);
labelGauge.setText("label5");
gridLayout4.setRows(3);
labelGauge2.setText("label5");
labelGauge3.
setForeground(Color.
white);
panelLanguage.add(buttonEnglish, null);
panelLanguage.add(buttonItaliano, null);
panel1.add(panelPhrase, null);
panelPhrase.add(labelPhrase, null);
panelPhrase.add(textArea1, null);
panel1.add(panelKey, null);
panelKey.add(buttonCrypt, null);
panelKey.add(labelKey, null);
panelKey.add(textAreaKey, null);
panelKey.add(buttonDecrypt, null);
panel1.add(panelEncrypted, null);
panelEncrypted.add(labelEncrypted, null);
panelEncrypted.add(textArea3, null);
panelCheck.add(buttonReset, null);
panelCheck.add(panelVisible, null);
panelVisible.add(checkbox1, null);
panelCheck.add(panelEditable, null);
panelEditable.add(checkbox2, null);
panelAbout.add(label1, null);
panelAbout.add(label3, null);
panelAbout.add(panel2, null);
panel2.add(label4, null);
panel2.add(label2, null);
panelGauge.add(labelGauge, null);
panelGauge.add(labelGauge2, null);
panelGauge.add(labelGauge3, null);
labelGauge.setText("Key Strength:");
keyStrength();
buttonReset.
addMouseListener(new java.
awt.
event.
MouseAdapter() {
buttonReset_mouseClicked(e);
}
});
textArea1.
addTextListener(new java.
awt.
event.
TextListener() {
textArea1_textValueChanged(e);
}
});
}
/**
* Get Applet information
* @return the applet information
*/
public String getAppletInfo
() {
return "Applet Information";
}
/**
* Get parameter info
* @return parameter info
*/
public String[][] getParameterInfo
() {
return null;
}
/**
* Richiama il metodo getKSPerCent dell'oggetto Vigenere e colora la
* labelGauge3 di rosso, giallo o verde a seconda della percentuale
* <br>
* Calls the getKSPerCent method of the Vigenere object and fills the
* labelGauge3 of ref, yellow or green depending of the percentual value
*/
void keyStrength(){
if (isEvaluable){
int s=vi.getKSPerCent(textAreaKey.getText(),defAcc);
if (s
<=60
) labelGauge3.
setBackground(Color.
red);
if (s
<=90
&&s
>60
) labelGauge3.
setBackground(Color.
yellow);
if (s
>90
) labelGauge3.
setBackground(Color.
green);
labelGauge2.setText(s+" %");
} else {
labelGauge2.setText("--");
labelGauge3.
setBackground(Color.
lightGray);
}
if (textAreaKey.getText().length()< textArea1.getText().length())
{
labelGauge3.
setBackground(Color.
red);
labelGauge3.setText("Key Short!");
} else labelGauge3.setText("");
}
/**
* Controlla che la chiave non sia piu' corta della frase; in tal caso stampa
* il messaggio "Key Short!" ("Chiave corta!")
* <br>
* Checks if the key is shorter than the phrase; if so, prints the message
* "Key Short!"
*/
void keyLength(){
String st
=labelGauge2.
getText();
if (isEvaluable){
if (st.length()==4) st=st.substring(0,2); else
if (st.length()==5) st=st.substring(0,3); else st=st.substring(0,1);
if (s
<=60
) labelGauge3.
setBackground(Color.
red);
if (s
<=90
&&s
>60
) labelGauge3.
setBackground(Color.
yellow);
if (s
>90
) labelGauge3.
setBackground(Color.
green);
}
if (textAreaKey.getText().length()< textArea1.getText().length())
{
labelGauge3.
setBackground(Color.
red);
labelGauge3.setText("Key Short!");
} else {
labelGauge3.setText("");
if (!isEvaluable
) labelGauge3.
setBackground(Color.
lightGray);
}
}
/**
* Imposta il linguaggio italiano alla pressione del buttonItaliano
* <br>
* Sets the italian language when buttonItaliano is pressed
* @param e the MouseEvent
*/
labelGauge.setText("Forza Chiave:");
labelPhrase.setText("Frase");
buttonCrypt.setLabel("Critta");
labelKey.setText("Chiave");
buttonDecrypt.setLabel("Decritta");
labelEncrypted.setText("Frase Crittata");
checkbox1.setLabel("Chiave visibile");
checkbox2.setLabel("Valuta Chiave");
buttonReset.setLabel("Resetta Chiave");
textArea1.setText("Inserisci qui la tua frase");
textArea1.selectAll();
label1.setText("JaVi - v1.0 - Un'implementazione Java dell'algoritmo "+
"di Vigenere.");
label4.setText("Questo software e' tutelato dalla GNU GPL license v.2");
}
textAreaKey.setVisible(!textAreaKey.isVisible());
}
isEvaluable=!isEvaluable;
keyStrength();
}
textArea1.selectAll();
}
/**
* Richiama il metodo getEncryptedPhrase dell'oggetto Vigenere alla pressione
* del buttonCrypt
* <br>
* Calls the method getEncryptedPhrase of the Vigenere object when buttonCrypt
* is pressed
* @param e the MouseEvent
*/
textArea3.setText(vi.getEncryptedPhrase(textArea1.getText(),
textAreaKey.getText()));
textArea1.setText("");
}
/**
* Richiama il metodo getPhrase dell'oggetto Vigenere alla pressione
* del buttonDecrypt
* <br>
* Calls the method getPhrase of the Vigenere object when buttonDecrypt
* is pressed
* @param e the MouseEvent
*/
textArea1.setText(vi.getPhrase(textArea3.getText(),
textAreaKey.getText()));
}
/**
* Richiama il metodo englishVersion alla pressione del buttonEnglish
* <br>
* Calls the englishVersion method when buttonEnglish is pressed
* @param e the MouseEvent
*/
englishVersion();
}
textAreaKey.
setText(Double.
toString(java.
lang.
Math.
PI)+
keyStrength();
}
/**
* Imposta il linguaggio inglese
* <br>
* Sets the english language
*/
void englishVersion (){
labelGauge.setText("Key Strength:");
labelPhrase.setText("Phrase");
buttonCrypt.setLabel("Encrypt");
labelKey.setText(" Key ");
buttonDecrypt.setLabel("Decrypt");
labelEncrypted.setText("Encrypted Phrase");
checkbox1.setLabel("Visible Key ");
checkbox2.setLabel("Evaluate Key ");
buttonReset.setLabel(" Reset Key ");
textArea1.setText("Insert your phrase here");
textArea1.selectAll();
label1.setText("JaVi - v1.0 - A Java implementation of the Vigenere"+
" algorithm.");
label4.setText("This software is under the GNU GPL license v.2 ");
}
void textArea1_textValueChanged
(TextEvent e
) {
keyLength();
}
void textAreaKey_textValueChanged
(TextEvent e
) {
keyStrength();
}
}