1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/CaesarovaŠifra/src/cz/frantovo/příklady/Caesar.java Sat Jun 22 16:00:39 2013 +0200
1.3 @@ -0,0 +1,108 @@
1.4 +package cz.frantovo.příklady;
1.5 +
1.6 +/**
1.7 + * This program is free software: you can redistribute it and/or modify
1.8 + * it under the terms of the GNU General Public License as published by
1.9 + * the Free Software Foundation, either version 3 of the License, or
1.10 + * (at your option) any later version.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.19 + */
1.20 +import java.awt.GridLayout;
1.21 +import java.awt.HeadlessException;
1.22 +import java.awt.event.ActionEvent;
1.23 +import java.awt.event.ActionListener;
1.24 +import java.util.ArrayList;
1.25 +import java.util.Collections;
1.26 +import java.util.List;
1.27 +import javax.swing.JButton;
1.28 +import javax.swing.JFrame;
1.29 +import javax.swing.JLabel;
1.30 +import javax.swing.JTextField;
1.31 +
1.32 +/**
1.33 + * @author Ing. František Kučera (frantovo.cz)
1.34 + */
1.35 +public class Caesar extends JFrame {
1.36 +
1.37 + private JTextField prostýText = new JTextField();
1.38 + private JTextField šifrovanýText = new JTextField();
1.39 + private JButton šifruj = new JButton("Šifruj");
1.40 + private JButton dešifruj = new JButton("Dešifruj");
1.41 + private static final char[] ABECEDA_ZNAKY = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
1.42 + private static final List<Character> ABECEDA;
1.43 + private static final int SKOK = 3;
1.44 +
1.45 + static {
1.46 + List<Character> abeceda = new ArrayList<>();
1.47 + for (char z : ABECEDA_ZNAKY) {
1.48 + abeceda.add(z);
1.49 + }
1.50 + ABECEDA = Collections.unmodifiableList(abeceda);
1.51 + }
1.52 +
1.53 + public static void main(String[] args) {
1.54 + Caesar c = new Caesar();
1.55 + c.setVisible(true);
1.56 + }
1.57 +
1.58 + public Caesar() throws HeadlessException {
1.59 + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
1.60 + setTitle("Caesarova šifra");
1.61 + setLocationRelativeTo(null);
1.62 + setLayout(new GridLayout(3, 2));
1.63 +
1.64 + add(new JLabel("Prostý text"));
1.65 + add(prostýText);
1.66 +
1.67 + add(new JLabel("Šifrovaný text"));
1.68 + add(šifrovanýText);
1.69 +
1.70 + add(šifruj);
1.71 + add(dešifruj);
1.72 +
1.73 + šifruj.addActionListener(new ActionListener() {
1.74 + @Override
1.75 + public void actionPerformed(ActionEvent e) {
1.76 + šifrovanýText.setText(posuň(prostýText.getText(), SKOK));
1.77 + }
1.78 + });
1.79 +
1.80 + dešifruj.addActionListener(new ActionListener() {
1.81 + @Override
1.82 + public void actionPerformed(ActionEvent e) {
1.83 + prostýText.setText(posuň(šifrovanýText.getText(), -SKOK));
1.84 + }
1.85 + });
1.86 +
1.87 + pack();
1.88 + }
1.89 +
1.90 + private static String posuň(String text, int oKolik) {
1.91 + StringBuilder posunutý = new StringBuilder(text.length());
1.92 +
1.93 + for (char z : text.toCharArray()) {
1.94 + posunutý.append(posuň(z, oKolik));
1.95 + }
1.96 +
1.97 + return posunutý.toString();
1.98 + }
1.99 +
1.100 + private static char posuň(char znak, int oKolik) {
1.101 + int původníPozice = ABECEDA.indexOf(znak);
1.102 +
1.103 + if (původníPozice < 0) {
1.104 + return znak;
1.105 + } else {
1.106 + int nováPozice = (původníPozice + oKolik) % ABECEDA_ZNAKY.length;
1.107 + nováPozice = nováPozice < 0 ? ABECEDA_ZNAKY.length + nováPozice : nováPozice;
1.108 + return ABECEDA.get(nováPozice);
1.109 + }
1.110 + }
1.111 +}