0% found this document useful (0 votes)
4 views6 pages

TXT

The document contains a Java program implementing the Vigenère cipher for encryption and decryption. It features a graphical user interface (GUI) that allows users to input plaintext and a key, and provides options to encrypt, decrypt, save results, and import text from files. The program utilizes Swing components for the GUI and includes functionalities for handling user actions like button clicks for encryption and file operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

TXT

The document contains a Java program implementing the Vigenère cipher for encryption and decryption. It features a graphical user interface (GUI) that allows users to input plaintext and a key, and provides options to encrypt, decrypt, save results, and import text from files. The program utilizes Swing components for the GUI and includes functionalities for handling user actions like button clicks for encryption and file operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

package km.crypto.vigenere.

principale;

import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;

//By Samirdine
//By Radika
//By Seifoudine

public class PrincipaleVigenere {

public static String encryptVigenere(String message, String key) {


String encrypted = "";
message = message.toUpperCase();
key = key.toUpperCase();
int j = 0;

for(int i = 0; i < message.length(); i++) {


char c = message.charAt(i);

if(c < 'A' || c > 'Z') {


continue;
}

encrypted+=(char)((c + key.charAt(j) - 2 * 'A') % 26 +'A');

j = ++j % key.length();
}

return encrypted;
}
public static String decryptVigenere(String message, String key) {
String decrypt = "";
message = message.toUpperCase();
key = key.toUpperCase();
key.split("");

int j = 0;

for(int i = 0; i < message.length(); i++) {


char c = message.charAt(i);

if(c < 'A' || c > 'Z') {


continue;
}

decrypt += (char)((c - key.charAt(j) + 26) % 26 + 'A');


j=++j % key.length();
}

return decrypt;
}

public static void main(String[] args) {

JFrame f=new JFrame();


f.setBounds(100, 10, 700, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setResizable(false);
f.setLocationRelativeTo(null);

Container a=f.getContentPane();

JLabel l1=new JLabel("Chiffrement/Déchiffrement de Vigènere");


l1.setBounds(90,0,400,60);
l1.setForeground(Color.BLUE);
a.add(l1);

//champs 1: exploitation
JLabel l3=new JLabel("Texte claire:");
l3.setBounds(19,40,400,50);
a.add(l3);

JTextArea t1=new JTextArea();


t1.setLineWrap(true);
t1.setBounds(80,80,320,100);
a.add(t1);

//champs 2: code
JLabel l2=new JLabel("Clef:");
l2.setBounds(50,180,400,50);
a.add(l2);

JTextField t3=new JTextField();


t3.setBounds(80,195,100,30);
a.add(t3);

//champs3: exploitation
JLabel l=new JLabel("Texte chiffré:");
l.setBounds(20,200,100,50);

JTextArea t2=new JTextArea();


t2.setLineWrap(true);
t2.setBounds(80,250,320,100);
a.add(t2);
t2.disable();

JButton b1 = new JButton("Chiffrer");


b1.setBounds(190,200,100, 25);
a.add(b1);

JButton saveButton = new JButton("Télécharger le résultat");


saveButton.setBounds(100 , 365, 150, 25);
a.add(saveButton);

saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
int userSelection = fileChooser.showSaveDialog(f);

if (userSelection == JFileChooser.APPROVE_OPTION) {
File fileToSave = fileChooser.getSelectedFile();

try (PrintWriter writer = new PrintWriter(new


FileWriter(fileToSave))) {
writer.print(t2.getText());
JOptionPane.showMessageDialog(a, "Contenu enregistré avec
succès !");

} catch (IOException es) {


JOptionPane.showMessageDialog(a, this, "Erreur lors de
l'enregistrement du fichier : \" + es.getMessage()", userSelection);

}
}

});

// fin

b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String k = t1.getText();
String z = t3.getText();
String res = encryptVigenere(k,z);
t2.setText(res);
}

});

JButton b2 = new JButton("Déchiffrer");


b2.setBounds(300,200,100, 25);
a.add(b2);
b2.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
String k = t1.getText();
String z = t3.getText();

String resultat = decryptVigenere(k,z);


t2.setText(resultat);
}
});

JButton b7 = new JButton("Tout éffacer");


b7.setBounds(420 , 225, 200, 25);
a.add(b7);
b7.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
t1.setText("");
t1.enable();
t2.setText("");
t3.setText("");

}
});

JButton b6 = new JButton("Teste Reverse", new ImageIcon("fle.jpg"));


b6.setBounds(260 , 365, 140, 25);
// b6.setIcon();
a.add(b6);
b6.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
t1.setText(t2.getText());
t2.setText("");

}
});

JButton b3 = new JButton("import de Fichier");


b3.setBounds(420 , 173, 200, 25);
a.add(b3);
b3.setBackground(Color.BLUE);
b3.setForeground(Color.WHITE);
b3.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {

//Créer un selector de fichier


final JFileChooser fc = new JFileChooser();

//En réponse à un clic sur un bouton :


// int returnVal = fc.showOpenDialog(b2);

if (e.getSource() == b3) {
int returnVal = fc.showOpenDialog(b2);

if (returnVal == JFileChooser.APPROVE_OPTION) {

File file = fc.getSelectedFile();


//This is where a real application would open the
file.
//t2.setText("Localisation: " + file.getName() +
"."+file.getAbsolutePath());

FileReader in = null;
try {
//OutputStream os = new FileOutputStream("");
in = new FileReader(file);
} catch (FileNotFoundException e2) {

e2.printStackTrace();
}
try {
in = new FileReader(file.getAbsolutePath());
} catch (FileNotFoundException e1) {

e1.printStackTrace();
}
int c = 0;

try {
StringBuffer sb = new StringBuffer();
char de;

while((c = in.read())!=-1){

de = (char)c;

System.out.print(de );

sb.append(de);

t1.setText(sb.toString());
t1.disable();

} catch (IOException e1) {

e1.printStackTrace();
}

}
else {
t2.setText("Annulation du lecture de
fichier." );
}
}

});

a.add(l);

try {
UIManager.setLookAndFeel(new NimbusLookAndFeel());
} catch (UnsupportedLookAndFeelException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

f.setVisible(true);

You might also like