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
JaVi - Java Vigenere - Javi2Applet.java

Javi2Applet.java

Caricato da: Teutoburgo
Scarica il programma completo

  1. /*
  2.        JaVi v1.0 - JavaVigenere -   A Java implementation of the Vigenere
  3.                                     cryptographical algorithm
  4.                                     Un'implementazione Java dell'algoritmo di
  5.                                     Vigenere per la crittografia
  6.     Copyright (C) 2002-2010 Pierre Blanc
  7.     Pierre Blanc: blanc_teutoburgo@yahoo.it
  8.     http://www.teutoburgo.tk                   (italiano)
  9.     http://teutoburgo.web44.net/indexEn.html   (english)
  10.  
  11.     This program is free software; you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License as published by
  13.     the Free Software Foundation; either version 2 of the License.
  14.  
  15.     This program is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.     GNU General Public License for more details.
  19.  
  20.     You should have received a copy of the GNU General Public License
  21.     along with this program; if not, write to the Free Software
  22.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  23.     or go to      http://www.gnu.org/copyleft/gpl.html
  24.  
  25.  
  26.     Questo  programma è  software  libero; è  lecito redistribuirlo  o
  27.     modificarlo secondo i termini  della Licenza Pubblica Generica GNU
  28.     come è pubblicata dalla Free  Software Foundation, o la versione 2
  29.     della licenza.
  30.     Questo programma  è distribuito nella  speranza che sia  utile, ma
  31.     SENZA  ALCUNA GARANZIA;  senza  neppure la  garanzia implicita  di
  32.     NEGOZIABILITÀ  o di  APPLICABILITÀ PER  UN PARTICOLARE  SCOPO.  Si
  33.     veda la Licenza Pubblica Generica GNU per avere maggiori dettagli.
  34.  
  35.     Questo  programma deve  essere  distribuito assieme  ad una  copia
  36.     della Licenza Pubblica Generica GNU;  in caso contrario, se ne può
  37.     ottenere  una scrivendo  alla Free  Software Foundation,  Inc., 59
  38.     Temple Place, Suite 330, Boston, MA 02111-1307 USA  oppure da
  39.     http://www.gnu.org/copyleft/gpl.html
  40.  
  41.  
  42.     Un grazie sentito a Roberto detto "Piano Man" per l'importante aiuto.
  43. */
  44. package JaVi;
  45.  
  46. import java.awt.*;
  47. import java.awt.event.*;
  48. import java.applet.*;
  49.  
  50. /**
  51.  *
  52.  * <p>Title: JaVi Applet</p>
  53.  * <p>Description: The class implementing the JaVi applet. It requires
  54.  * the Vigenere and the FrequenzaNijk objects</p>
  55.  * <p>Copyright: Copyright (c) 2002 Pierre Blanc</p>
  56.  * <p>Company: Teutoburgo</p>
  57.  * @author Pierre Blanc
  58.  * @version 1.0
  59.  */
  60. public class Javi2Applet extends Applet {
  61.   int defAcc=30;
  62.   boolean isTAKEditable = true;
  63.   boolean isTAKVisible = true;
  64.   boolean isStandalone = false;
  65.   boolean isEvaluable = true;
  66.   Vigenere vi;
  67.   BorderLayout borderLayout1 = new BorderLayout();
  68.   Panel panelLanguage = new Panel();
  69.   GridLayout gridLayout3 = new GridLayout(3,1);
  70.   Button buttonEnglish = new Button();
  71.   Button buttonItaliano = new Button();
  72.   Panel panel1 = new Panel();
  73.   GridLayout gridLayout1 = new GridLayout(3,1);
  74.   Panel panelPhrase = new Panel();
  75.   Panel panelKey = new Panel();
  76.   Panel panelEncrypted = new Panel();
  77.   FlowLayout flowLayout1 = new FlowLayout();
  78.   Label labelPhrase = new Label();
  79.   TextArea textArea1 = new TextArea("Insert your phrase here",3,30,
  80.                                    TextArea.SCROLLBARS_VERTICAL_ONLY);
  81.   FlowLayout flowLayout2 = new FlowLayout();
  82.   Button buttonCrypt = new Button();
  83.   Label labelKey = new Label();
  84.   TextArea textAreaKey = new TextArea("Key",3,30,
  85.                                       TextArea.SCROLLBARS_VERTICAL_ONLY);
  86.   Button buttonDecrypt = new Button();
  87.   FlowLayout flowLayout3 = new FlowLayout();
  88.   Label labelEncrypted = new Label();
  89.   TextArea textArea3 = new TextArea("Encrypted",3,30,
  90.                                     TextArea.SCROLLBARS_VERTICAL_ONLY);
  91.   Panel panelCheck = new Panel();
  92.   Panel panelEditable = new Panel();
  93.   Panel panelVisible = new Panel();
  94.   FlowLayout flowLayout4 = new FlowLayout();
  95.   Checkbox checkbox1 = new Checkbox("CBVisible", isTAKVisible);
  96.   FlowLayout flowLayout5 = new FlowLayout();
  97.   Checkbox checkbox2 = new Checkbox("CBEditable", isTAKEditable);
  98.   FlowLayout flowLayout6 = new FlowLayout();
  99.   Button buttonReset = new Button();
  100.   Panel panelAbout = new Panel();
  101.   GridLayout gridLayout2 = new GridLayout(3,1);
  102.   Label label1 = new Label();
  103.   Label label3 = new Label();
  104.   Panel panel2 = new Panel();
  105.   FlowLayout flowLayout7 = new FlowLayout();
  106.   Label label2 = new Label();
  107.   Label label4 = new Label();
  108.   Panel panelGauge = new Panel();
  109.   GridLayout gridLayout4 = new GridLayout();
  110.   Label labelGauge = new Label();
  111.   Label labelGauge2 = new Label();
  112.   Label labelGauge3 = new Label();
  113.  
  114.    /**
  115.     * Get a parameter value
  116.     * @param key
  117.     * @param def
  118.     * @return the value
  119.     */
  120.   public String getParameter(String key, String def) {
  121.     return isStandalone ? System.getProperty(key, def) :
  122.       (getParameter(key) != null ? getParameter(key) : def);
  123.   }
  124.  
  125.   /**Construct the applet*/
  126.   public Javi2Applet() {
  127.   }
  128.   /**Initialize the applet*/
  129.   public void init() {
  130.     try {
  131.       jbInit();
  132.     }
  133.     catch(Exception e) {
  134.       e.printStackTrace();
  135.     }
  136.   }
  137.  
  138.   /**
  139.    * Component initialization
  140.    * @throws Exception
  141.    */
  142.   private void jbInit() throws Exception {
  143.     String accuracy=this.getParameter("ACCURACY","30");
  144.     String methodKey=this.getParameter("Method","MAX");
  145.     String UnicFirst=this.getParameter("UnicodeFirstChar","32");
  146.     String UnicLast=this.getParameter("UnicodeLastChar","123");
  147.     defAcc=Integer.parseInt(accuracy);
  148.     int Unic0=Integer.parseInt(UnicFirst);
  149.     int UnicZ=Integer.parseInt(UnicLast);
  150.     vi=new Vigenere(Unic0, UnicZ, methodKey);
  151.     labelGauge2.setForeground(Color.black);
  152.     this.resize(620,380);
  153.     this.setLayout(borderLayout1);
  154.     panelLanguage.setLayout(gridLayout3);
  155.     buttonEnglish.setBackground(new Color(0, 192, 138));
  156.     buttonEnglish.setLabel("English");
  157.     buttonEnglish.addMouseListener(new java.awt.event.MouseAdapter() {
  158.       public void mouseClicked(MouseEvent e) {
  159.         buttonEnglish_mouseClicked(e);
  160.       }
  161.     });
  162.     buttonItaliano.setBackground(new Color(255, 138, 0));
  163.     buttonItaliano.setForeground(Color.white);
  164.     buttonItaliano.setLabel("Italiano");
  165.  
  166.     buttonItaliano.addMouseListener(new java.awt.event.MouseAdapter() {
  167.       public void mousePressed(MouseEvent e) {
  168.         buttonItaliano_mousePressed(e);
  169.       }
  170.     });
  171.  
  172.     panelLanguage.setBackground(Color.gray);
  173.     panel1.setBackground(Color.blue);
  174.     panel1.setLayout(gridLayout1);
  175.     panelPhrase.setLayout(flowLayout1);
  176.     labelPhrase.setForeground(Color.yellow);
  177.     panelKey.setLayout(flowLayout2);
  178.     buttonCrypt.addMouseListener(new java.awt.event.MouseAdapter() {
  179.       public void mouseClicked(MouseEvent e) {
  180.         buttonCrypt_mouseClicked(e);
  181.       }
  182.     });
  183.  
  184.     textAreaKey.setText(Double.toString(java.lang.Math.PI)+
  185.     Double.toString(java.lang.Math.E));
  186.     textAreaKey.setBackground(Color.lightGray);
  187.     textAreaKey.setColumns(35);
  188.     textAreaKey.setEditable(isTAKEditable);
  189.     textAreaKey.setVisible(isTAKVisible);
  190.     textAreaKey.addTextListener(new java.awt.event.TextListener() {
  191.       public void textValueChanged(TextEvent e) {
  192.         textAreaKey_textValueChanged(e);
  193.       }
  194.     });
  195.     buttonDecrypt.addMouseListener(new java.awt.event.MouseAdapter() {
  196.       public void mouseClicked(MouseEvent e) {
  197.         buttonDecrypt_mouseClicked(e);
  198.       }
  199.     });
  200.     panelEncrypted.setLayout(flowLayout3);
  201.     labelEncrypted.setForeground(Color.yellow);
  202.     textArea3.setBackground(Color.blue);
  203.     textArea3.setColumns(35);
  204.     textArea3.setForeground(Color.yellow);
  205.     textArea3.setText("");
  206.     panelCheck.setLayout(flowLayout6);
  207.     panelVisible.setLayout(flowLayout4);
  208.     checkbox1.setBackground(SystemColor.text);
  209.  
  210.     checkbox1.addMouseListener(new java.awt.event.MouseAdapter() {
  211.       public void mousePressed(MouseEvent e) {
  212.         checkbox1_mousePressed(e);
  213.       }
  214.     });
  215.     panelEditable.setLayout(flowLayout5);
  216.     checkbox2.setBackground(SystemColor.text);
  217.     checkbox2.addMouseListener(new java.awt.event.MouseAdapter() {
  218.       public void mousePressed(MouseEvent e) {
  219.         checkbox2_mousePressed(e);
  220.       }
  221.     });
  222.     panelKey.setBackground(Color.pink);
  223.     panelCheck.setBackground(Color.gray);
  224.     panelVisible.setBackground(Color.gray);
  225.     panelEditable.setBackground(Color.gray);
  226.     panelPhrase.setBackground(SystemColor.desktop);
  227.     panelEncrypted.setBackground(SystemColor.desktop);
  228.     buttonReset.addMouseListener(new java.awt.event.MouseAdapter() {
  229.       public void mouseClicked(MouseEvent e) {
  230.         buttonReset_mouseClicked(e);
  231.       }
  232.     });
  233.     textArea1.addMouseListener(new java.awt.event.MouseAdapter() {
  234.       public void mouseClicked(MouseEvent e) {
  235.         textArea1_mouseClicked(e);
  236.       }
  237.     });
  238.     textArea1.setBackground(Color.blue);
  239.     textArea1.setColumns(35);
  240.     textArea1.setForeground(Color.yellow);
  241.     panelAbout.setLayout(gridLayout2);
  242.     label3.setText("Copyright (C) P.Blanc 2002 - blanc_teutoburgo@yahoo.it - "+
  243.                    "http://www.teutoburgo.tk");
  244.     this.setBackground(SystemColor.text);
  245.     panel2.setLayout(flowLayout7);
  246.     label2.setText("b<AE=<");
  247.     englishVersion();
  248.     panelGauge.setLayout(gridLayout4);
  249.     labelGauge.setText("label5");
  250.     gridLayout4.setRows(3);
  251.     labelGauge2.setText("label5");
  252.     labelGauge3.setForeground(Color.white);
  253.     this.add(panelLanguage, BorderLayout.EAST);
  254.     panelLanguage.add(buttonEnglish, null);
  255.     panelLanguage.add(buttonItaliano, null);
  256.     this.add(panel1, BorderLayout.CENTER);
  257.     panel1.add(panelPhrase, null);
  258.     panelPhrase.add(labelPhrase, null);
  259.     panelPhrase.add(textArea1, null);
  260.     panel1.add(panelKey, null);
  261.     panelKey.add(buttonCrypt, null);
  262.     panelKey.add(labelKey, null);
  263.     panelKey.add(textAreaKey, null);
  264.     panelKey.add(buttonDecrypt, null);
  265.     panel1.add(panelEncrypted, null);
  266.     panelEncrypted.add(labelEncrypted, null);
  267.     panelEncrypted.add(textArea3, null);
  268.     this.add(panelCheck, BorderLayout.SOUTH);
  269.     panelCheck.add(buttonReset, null);
  270.     panelCheck.add(panelVisible, null);
  271.     panelVisible.add(checkbox1, null);
  272.     panelCheck.add(panelEditable, null);
  273.     panelEditable.add(checkbox2, null);
  274.     this.add(panelAbout, BorderLayout.NORTH);
  275.     panelAbout.add(label1, null);
  276.     panelAbout.add(label3, null);
  277.     panelAbout.add(panel2, null);
  278.     panel2.add(label4, null);
  279.     panel2.add(label2, null);
  280.     this.add(panelGauge,  BorderLayout.WEST);
  281.     panelGauge.add(labelGauge, null);
  282.     panelGauge.add(labelGauge2, null);
  283.     panelGauge.add(labelGauge3, null);
  284.     labelGauge.setText("Key Strength:");
  285.     keyStrength();
  286.     buttonReset.addMouseListener(new java.awt.event.MouseAdapter() {
  287.       public void mouseClicked(MouseEvent e) {
  288.         buttonReset_mouseClicked(e);
  289.       }
  290.     });
  291.     textArea1.addTextListener(new java.awt.event.TextListener() {
  292.       public void textValueChanged(TextEvent e) {
  293.         textArea1_textValueChanged(e);
  294.       }
  295.     });
  296.  
  297.   }
  298.  
  299.   /**
  300.    * Get Applet information
  301.    * @return the applet information
  302.    */
  303.   public String getAppletInfo() {
  304.     return "Applet Information";
  305.   }
  306.  
  307.   /**
  308.    * Get parameter info
  309.    * @return parameter info
  310.    */
  311.   public String[][] getParameterInfo() {
  312.     return null;
  313.   }
  314.  
  315.   /**
  316.    * Richiama il metodo getKSPerCent dell'oggetto Vigenere e colora la
  317.    * labelGauge3 di rosso, giallo o verde a seconda della percentuale
  318.    * <br>
  319.    * Calls the getKSPerCent method of the Vigenere object and fills the
  320.    * labelGauge3 of ref, yellow or green depending of the percentual value
  321.    */
  322.   void keyStrength(){
  323.     if (isEvaluable){
  324.     int s=vi.getKSPerCent(textAreaKey.getText(),defAcc);
  325.     if (s<=60) labelGauge3.setBackground(Color.red);
  326.     if (s<=90 &&s>60) labelGauge3.setBackground(Color.yellow);
  327.     if (s>90) labelGauge3.setBackground(Color.green);
  328.     labelGauge2.setText(s+" %");
  329.     } else {
  330.       labelGauge2.setText("--");
  331.       labelGauge3.setBackground(Color.lightGray);
  332.     }
  333.     if (textAreaKey.getText().length()< textArea1.getText().length())
  334.       {
  335.         labelGauge3.setBackground(Color.red);
  336.         labelGauge3.setText("Key Short!");
  337.       } else labelGauge3.setText("");
  338.   }
  339.  
  340.   /**
  341.    * Controlla che la chiave non sia piu' corta della frase; in tal caso stampa
  342.    * il messaggio "Key Short!" ("Chiave corta!")
  343.    * <br>
  344.    * Checks if the key is shorter than the phrase; if so, prints the message
  345.    * "Key Short!"
  346.    */
  347.   void keyLength(){
  348.     String st=labelGauge2.getText();
  349.     if (isEvaluable){
  350.     if (st.length()==4) st=st.substring(0,2); else
  351.       if (st.length()==5) st=st.substring(0,3); else st=st.substring(0,1);
  352.     int s=Integer.parseInt(st);
  353.  
  354.     if (s<=60) labelGauge3.setBackground(Color.red);
  355.     if (s<=90 &&s>60) labelGauge3.setBackground(Color.yellow);
  356.     if (s>90) labelGauge3.setBackground(Color.green);
  357.     }
  358.     if (textAreaKey.getText().length()< textArea1.getText().length())
  359.       {
  360.         labelGauge3.setBackground(Color.red);
  361.         labelGauge3.setText("Key Short!");
  362.       } else {
  363.         labelGauge3.setText("");
  364.         if (!isEvaluable) labelGauge3.setBackground(Color.lightGray);
  365.       }
  366.   }
  367.  
  368.   /**
  369.    * Imposta il linguaggio italiano alla pressione del buttonItaliano
  370.    * <br>
  371.    * Sets the italian language when buttonItaliano is pressed
  372.    * @param e the MouseEvent
  373.    */
  374.   void buttonItaliano_mousePressed(MouseEvent e) {
  375.     labelGauge.setText("Forza Chiave:");
  376.     labelPhrase.setText("Frase");
  377.     buttonCrypt.setLabel("Critta");
  378.     labelKey.setText("Chiave");
  379.     buttonDecrypt.setLabel("Decritta");
  380.     labelEncrypted.setText("Frase Crittata");
  381.     checkbox1.setLabel("Chiave visibile");
  382.     checkbox2.setLabel("Valuta Chiave");
  383.     buttonReset.setLabel("Resetta Chiave");
  384.     textArea1.setText("Inserisci qui la tua frase");
  385.     textArea1.selectAll();
  386.     label1.setText("JaVi - v1.0 - Un'implementazione Java dell'algoritmo "+
  387.                    "di Vigenere.");
  388.     label4.setText("Questo software e' tutelato dalla GNU GPL license v.2");
  389.  
  390.   }
  391.  
  392.  
  393.   void checkbox1_mousePressed(MouseEvent e) {
  394.     textAreaKey.setVisible(!textAreaKey.isVisible());
  395.   }
  396.  
  397.   void checkbox2_mousePressed(MouseEvent e) {
  398.     isEvaluable=!isEvaluable;
  399.     keyStrength();
  400.   }
  401.  
  402.   void textArea1_mouseClicked(MouseEvent e) {
  403.     textArea1.selectAll();
  404.   }
  405.  
  406.   /**
  407.    * Richiama il metodo getEncryptedPhrase dell'oggetto Vigenere alla pressione
  408.    * del buttonCrypt
  409.    * <br>
  410.    * Calls the method getEncryptedPhrase of the Vigenere object when buttonCrypt
  411.    * is pressed
  412.    * @param e the MouseEvent
  413.    */
  414.   void buttonCrypt_mouseClicked(MouseEvent e) {
  415.     textArea3.setText(vi.getEncryptedPhrase(textArea1.getText(),
  416.                                          textAreaKey.getText()));
  417.     textArea1.setText("");
  418.  
  419.   }
  420.  
  421.   /**
  422.    * Richiama il metodo getPhrase dell'oggetto Vigenere alla pressione
  423.    * del buttonDecrypt
  424.    * <br>
  425.    * Calls the method getPhrase of the Vigenere object when buttonDecrypt
  426.    * is pressed
  427.    * @param e the MouseEvent
  428.    */
  429.   void buttonDecrypt_mouseClicked(MouseEvent e) {
  430.     textArea1.setText(vi.getPhrase(textArea3.getText(),
  431.                                           textAreaKey.getText()));
  432.   }
  433.  
  434.   /**
  435.    * Richiama il metodo englishVersion alla pressione del buttonEnglish
  436.    * <br>
  437.    * Calls the englishVersion method when buttonEnglish is pressed
  438.    * @param e the MouseEvent
  439.    */
  440.   void buttonEnglish_mouseClicked(MouseEvent e) {
  441.     englishVersion();
  442.   }
  443.  
  444.   void buttonReset_mouseClicked(MouseEvent e) {
  445.     textAreaKey.setText(Double.toString(java.lang.Math.PI)+
  446.     Double.toString(java.lang.Math.E));
  447.     keyStrength();
  448.   }
  449.  
  450.   /**
  451.    * Imposta il linguaggio inglese
  452.    * <br>
  453.    * Sets the english language
  454.    */
  455.   void englishVersion (){
  456.     labelGauge.setText("Key Strength:");
  457.     labelPhrase.setText("Phrase");
  458.     buttonCrypt.setLabel("Encrypt");
  459.     labelKey.setText(" Key   ");
  460.     buttonDecrypt.setLabel("Decrypt");
  461.     labelEncrypted.setText("Encrypted Phrase");
  462.     checkbox1.setLabel("Visible Key    ");
  463.     checkbox2.setLabel("Evaluate Key   ");
  464.     buttonReset.setLabel("   Reset Key   ");
  465.     textArea1.setText("Insert your phrase here");
  466.     textArea1.selectAll();
  467.     label1.setText("JaVi - v1.0 - A Java implementation of the Vigenere"+
  468.                    " algorithm.");
  469.     label4.setText("This software is under the GNU GPL license v.2           ");
  470.   }
  471.  
  472.  
  473.   void textArea1_textValueChanged(TextEvent e) {
  474.     keyLength();
  475.   }
  476.  
  477.   void textAreaKey_textValueChanged(TextEvent e) {
  478.     keyStrength();
  479.   }
  480.  
  481.  
  482. }