ここで私は一緒に入れているVigenere暗号です。
私は、GUIを使用していますが、コンソール入力と出力とTranslateTextListenerクラスの暗号とuncipherメソッドを使用することができます。ここで
はコードです:
package com.ggl.testing;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
public class VigenèreCipher implements Runnable {
private static final Insets normalInsets = new Insets(10, 10, 0, 10);
private static final Insets finalInsets = new Insets(10, 10, 10, 10);
private JTextArea originalTextArea;
private JTextArea keyTextArea;
private JTextArea cipherTextArea;
private JTextArea uncipheredTextArea;
public static void main(String[] args) {
SwingUtilities.invokeLater(new VigenèreCipher());
}
@Override
public void run() {
JFrame frame = new JFrame("Vigenère Cipher");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createCipherPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createCipherPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
int gridy = 0;
JLabel originalTextLabel = new JLabel("Original Text:");
addComponent(panel, originalTextLabel, 0, gridy, 1, 1, normalInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
originalTextArea = new JTextArea(5, 30);
originalTextArea.setLineWrap(true);
originalTextArea.setWrapStyleWord(true);
JScrollPane originalTextScrollPane = new JScrollPane(originalTextArea);
addComponent(panel, originalTextScrollPane, 1, gridy++, 1, 1,
normalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel keyTextLabel = new JLabel("Key Text:");
addComponent(panel, keyTextLabel, 0, gridy, 1, 1, normalInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
keyTextArea = new JTextArea(5, 30);
keyTextArea.setLineWrap(true);
keyTextArea.setWrapStyleWord(true);
JScrollPane keyTextScrollPane = new JScrollPane(keyTextArea);
addComponent(panel, keyTextScrollPane, 1, gridy++, 1, 1, normalInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JLabel cipherTextLabel = new JLabel("Cipher Text:");
addComponent(panel, cipherTextLabel, 0, gridy, 1, 1, finalInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
cipherTextArea = new JTextArea(5, 30);
cipherTextArea.setLineWrap(true);
JScrollPane cipherTextScrollPane = new JScrollPane(cipherTextArea);
addComponent(panel, cipherTextScrollPane, 1, gridy++, 1, 1,
finalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JLabel uncipheredTextLabel = new JLabel("Unciphered Text:");
addComponent(panel, uncipheredTextLabel, 0, gridy, 1, 1, finalInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
uncipheredTextArea = new JTextArea(5, 30);
uncipheredTextArea.setLineWrap(true);
uncipheredTextArea.setWrapStyleWord(true);
JScrollPane uncipheredTextScrollPane = new JScrollPane(
uncipheredTextArea);
addComponent(panel, uncipheredTextScrollPane, 1, gridy++, 1, 1,
finalInsets, GridBagConstraints.LINE_START,
GridBagConstraints.HORIZONTAL);
JButton submitButton = new JButton("Translate text");
submitButton.addActionListener(new TranslateTextListener());
addComponent(panel, submitButton, 0, gridy++, 2, 1, finalInsets,
GridBagConstraints.CENTER, GridBagConstraints.NONE);
return panel;
}
private void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight, Insets insets,
int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
public class TranslateTextListener implements ActionListener {
private char[] cipherAlphabet;
private int lowerLimit;
private int upperLimit;
public TranslateTextListener() {
this.lowerLimit = 32;
this.upperLimit = 126;
this.cipherAlphabet = new char[upperLimit - lowerLimit + 1];
// Grab all the ASCII characters between space and ~, inclusive
for (int i = lowerLimit; i <= upperLimit; i++) {
cipherAlphabet[i - lowerLimit] = (char) i;
}
}
@Override
public void actionPerformed(ActionEvent event) {
String text = originalTextArea.getText().trim();
String key = keyTextArea.getText().trim();
String cipher = cipherTextArea.getText().trim();
String uncipher = "";
if (!text.equals("") && !key.equals("")) {
cipher = cipher(text, key);
}
if (!key.equals("") && !cipher.equals("")) {
uncipher = uncipher(cipher, key);
}
cipherTextArea.setText(cipher);
uncipheredTextArea.setText(uncipher);
}
private String cipher(String text, String key) {
StringBuilder builder = new StringBuilder(text.length());
int keyIndex = 0;
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
int pos = (int) c;
if (pos < lowerLimit || pos > upperLimit) {
builder.append(c);
} else {
char k = key.charAt(keyIndex);
pos = getCharacterPosition(c);
int pos2 = getCharacterPosition(k);
int sum = (pos + pos2) % cipherAlphabet.length;
builder.append(getCharacter(sum));
keyIndex = ++keyIndex % key.length();
}
}
return builder.toString();
}
private String uncipher(String cipher, String key) {
StringBuilder builder = new StringBuilder(cipher.length());
int keyIndex = 0;
for (int i = 0; i < cipher.length(); i++) {
char c = cipher.charAt(i);
int pos = (int) c;
if (pos < lowerLimit || pos > upperLimit) {
builder.append(c);
} else {
char k = key.charAt(keyIndex);
pos = getCharacterPosition(c);
int pos2 = getCharacterPosition(k);
int sum = pos - pos2;
while (sum < 0) {
sum += cipherAlphabet.length;
}
sum = sum % cipherAlphabet.length;
builder.append(getCharacter(sum));
keyIndex = ++keyIndex % key.length();
}
}
return builder.toString();
}
private int getCharacterPosition(char c) {
for (int i = 0; i < cipherAlphabet.length; i++) {
if (c == cipherAlphabet[i]) {
return i;
}
}
return -1;
}
private char getCharacter(int index) {
if (index >= 0 && index < cipherAlphabet.length) {
return cipherAlphabet[index];
} else {
return '?';
}
}
}
}