import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import java.awt.*; import java.awt.event.*; import java.awt.GridLayout; import java.text.NumberFormat; import java.util.*; public class MyCypherMachine extends JFrame implements DocumentListener, ActionListener, KeyListener { private JButton commit; private JPanel panel; private JLabel texteRotation = new JLabel("Indiquer le nombre de rotation : "); private JFormattedTextField fieldRotation; private JLabel texteSens = new JLabel("Indiquer le sens de rotation : "); private JFormattedTextField fieldSens; private JLabel chiffrement = new JLabel("Zone du texte à crypter/decrypter : "); private JTextArea zoneTexteChiffre; private JLabel traduction = new JLabel("Texte decrypté/crypté : "); private JTextArea zoneTexteTraduc; private JLabel texteChoix = new JLabel("Choix d'utilisation : "); private ButtonGroup boutonChoix = new ButtonGroup(); private JRadioButton cypher = new JRadioButton("Cryptage", true); private JRadioButton decypher = new JRadioButton("Decryptage", false); private NumberFormat numberFormat; private int nbreRotation; private int sensRotation; public MyCypherMachine() { super("Encrypteur/Décrypteur"); numberFormat = NumberFormat.getNumberInstance(); initComponents(); } private void initComponents() { setResizable(false); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); zoneTexteChiffre = new JTextArea(); zoneTexteTraduc = new JTextArea(); zoneTexteChiffre.getDocument().addDocumentListener(this); zoneTexteChiffre.addKeyListener(this); JScrollPane scrollChiffre = new JScrollPane(zoneTexteChiffre); JScrollPane scrollTraduc = new JScrollPane(zoneTexteTraduc); zoneTexteChiffre.setEditable(false); zoneTexteChiffre.setBackground(Color.gray); zoneTexteTraduc.setEditable(false); zoneTexteTraduc.setBackground(Color.gray); commit = new JButton("Commit !!"); commit.addActionListener(this); fieldRotation = new JFormattedTextField(numberFormat); fieldSens = new JFormattedTextField(numberFormat); boutonChoix.add(cypher); boutonChoix.add(decypher); panel = new JPanel(new GridLayout(6, 2, 3, 2)); JPanel radioPanel = new JPanel(new GridLayout(0, 1)); radioPanel.add(cypher); radioPanel.add(decypher); panel.add(texteChoix); panel.add(radioPanel); panel.add(texteRotation); panel.add(fieldRotation); panel.add(texteSens); panel.add(fieldSens); panel.add(new JLabel("")); panel.add(commit); panel.add(chiffrement); panel.add(scrollChiffre); panel.add(traduction); panel.add(scrollTraduc); this.setContentPane(panel); pack(); setLocationRelativeTo(null); } public void changedUpdate(DocumentEvent ev) {} public void removeUpdate(DocumentEvent ev) { if (ev.getLength() != 1) { zoneTexteTraduc.setText(""); return; } int pos = ev.getOffset(); String content = null; try { content = zoneTexteTraduc.getText(0, pos + 1); } catch (BadLocationException e) { e.printStackTrace(); } zoneTexteTraduc.setText(new String(content.substring(0, pos))); } public void insertUpdate(DocumentEvent ev) { if (ev.getLength() != 1) { return; } int pos = ev.getOffset(); String content = null; try { content = zoneTexteChiffre.getText(0, pos + 1); } catch (BadLocationException e) { e.printStackTrace(); } int caractere = (int) content.charAt(pos); String retour = cypherText(caractere); if(decypher.isSelected()) sensRotation = -sensRotation; zoneTexteTraduc.append(retour); if (decypher.isSelected()) sensRotation = -sensRotation; } public String cypherText(int caractere) { if(caractere >= (int) 'a' && caractere <= (int) 'z') if(sensRotation < 0) if(caractere < ((int) 'a') + nbreRotation) caractere = caractere + 26 - nbreRotation; else caractere -= nbreRotation; else if(caractere > ((int) 'z') - nbreRotation) caractere = caractere - 26 + nbreRotation; else caractere += nbreRotation; char[] tabChar = {(char) caractere}; return new String(tabChar); } public void actionPerformed(ActionEvent e) { if (!fieldRotation.getText().equals("") && !fieldSens.getText().equals("")) { zoneTexteChiffre.setEditable(true); zoneTexteChiffre.setBackground(Color.white); zoneTexteTraduc.setEditable(true); zoneTexteTraduc.setBackground(Color.white); } nbreRotation = Integer.parseInt(fieldRotation.getText()); sensRotation = Integer.parseInt(fieldSens.getText()); } public void keyPressed(KeyEvent e) {} public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) { int key = (int) e.getKeyChar(); if(e.getKeyModifiersText(e.getModifiers()).equals("Ctrl") && key == 22) { if(decypher.isSelected()) sensRotation = -sensRotation; String texte = zoneTexteChiffre.getText(); char[] tabChar = texte.toCharArray(); String ret = ""; for(int i = 0; i < tabChar.length; i++) ret = ret + cypherText(tabChar[i]); zoneTexteTraduc.setText(ret); if (decypher.isSelected()) sensRotation = -sensRotation; } } public static void main(String args[]) { SwingUtilities.invokeLater(new Runnable() { public void run() { new MyCypherMachine().setVisible(true); } }); } }