franta-hg@19: package cz.frantovo.příklady; franta-hg@19: franta-hg@19: /** franta-hg@19: * This program is free software: you can redistribute it and/or modify franta-hg@19: * it under the terms of the GNU General Public License as published by franta-hg@19: * the Free Software Foundation, either version 3 of the License, or franta-hg@19: * (at your option) any later version. franta-hg@19: * franta-hg@19: * This program is distributed in the hope that it will be useful, franta-hg@19: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@19: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@19: * GNU General Public License for more details. franta-hg@19: * franta-hg@19: * You should have received a copy of the GNU General Public License franta-hg@19: * along with this program. If not, see . franta-hg@19: */ franta-hg@19: import java.awt.GridLayout; franta-hg@19: import java.awt.HeadlessException; franta-hg@19: import java.awt.event.ActionEvent; franta-hg@19: import java.awt.event.ActionListener; franta-hg@19: import java.util.ArrayList; franta-hg@19: import java.util.Collections; franta-hg@19: import java.util.List; franta-hg@19: import javax.swing.JButton; franta-hg@19: import javax.swing.JFrame; franta-hg@19: import javax.swing.JLabel; franta-hg@19: import javax.swing.JTextField; franta-hg@19: franta-hg@19: /** franta-hg@19: * @author Ing. František Kučera (frantovo.cz) franta-hg@19: */ franta-hg@19: public class Caesar extends JFrame { franta-hg@19: franta-hg@19: private JTextField prostýText = new JTextField(); franta-hg@19: private JTextField šifrovanýText = new JTextField(); franta-hg@19: private JButton šifruj = new JButton("Šifruj"); franta-hg@19: private JButton dešifruj = new JButton("Dešifruj"); franta-hg@19: private static final char[] ABECEDA_ZNAKY = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray(); franta-hg@19: private static final List ABECEDA; franta-hg@19: private static final int SKOK = 3; franta-hg@19: franta-hg@19: static { franta-hg@19: List abeceda = new ArrayList<>(); franta-hg@19: for (char z : ABECEDA_ZNAKY) { franta-hg@19: abeceda.add(z); franta-hg@19: } franta-hg@19: ABECEDA = Collections.unmodifiableList(abeceda); franta-hg@19: } franta-hg@19: franta-hg@19: public static void main(String[] args) { franta-hg@19: Caesar c = new Caesar(); franta-hg@19: c.setVisible(true); franta-hg@19: } franta-hg@19: franta-hg@19: public Caesar() throws HeadlessException { franta-hg@19: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); franta-hg@19: setTitle("Caesarova šifra"); franta-hg@19: setLocationRelativeTo(null); franta-hg@19: setLayout(new GridLayout(3, 2)); franta-hg@19: franta-hg@19: add(new JLabel("Prostý text")); franta-hg@19: add(prostýText); franta-hg@19: franta-hg@19: add(new JLabel("Šifrovaný text")); franta-hg@19: add(šifrovanýText); franta-hg@19: franta-hg@19: add(šifruj); franta-hg@19: add(dešifruj); franta-hg@19: franta-hg@19: šifruj.addActionListener(new ActionListener() { franta-hg@19: @Override franta-hg@19: public void actionPerformed(ActionEvent e) { franta-hg@19: šifrovanýText.setText(posuň(prostýText.getText(), SKOK)); franta-hg@19: } franta-hg@19: }); franta-hg@19: franta-hg@19: dešifruj.addActionListener(new ActionListener() { franta-hg@19: @Override franta-hg@19: public void actionPerformed(ActionEvent e) { franta-hg@19: prostýText.setText(posuň(šifrovanýText.getText(), -SKOK)); franta-hg@19: } franta-hg@19: }); franta-hg@19: franta-hg@19: pack(); franta-hg@19: } franta-hg@19: franta-hg@19: private static String posuň(String text, int oKolik) { franta-hg@19: StringBuilder posunutý = new StringBuilder(text.length()); franta-hg@19: franta-hg@19: for (char z : text.toCharArray()) { franta-hg@19: posunutý.append(posuň(z, oKolik)); franta-hg@19: } franta-hg@19: franta-hg@19: return posunutý.toString(); franta-hg@19: } franta-hg@19: franta-hg@19: private static char posuň(char znak, int oKolik) { franta-hg@19: int původníPozice = ABECEDA.indexOf(znak); franta-hg@19: franta-hg@19: if (původníPozice < 0) { franta-hg@19: return znak; franta-hg@19: } else { franta-hg@19: int nováPozice = (původníPozice + oKolik) % ABECEDA_ZNAKY.length; franta-hg@19: nováPozice = nováPozice < 0 ? ABECEDA_ZNAKY.length + nováPozice : nováPozice; franta-hg@19: return ABECEDA.get(nováPozice); franta-hg@19: } franta-hg@19: } franta-hg@19: }