Cesar Cipher program

0

Witam!

Mam spory problem... Jako początkujący programista w javie mam do napisania program który nazywa się Caesar’s Cipher Game polecenie do nego brzmi:

You are required to develop a game program that supports the Caesar cipher shift algorithm. The game is undertaken by two players using a single Java application running on one computer: the cryptographer and the cryptanalyst.

The cryptographer is presented with a window (JFrame) that allows her to enter a plain text message (of about 10 lines, with only alphabetic characters and space punctuation). She can also select a key using a JComboBox that allows a shift of between 1 and 25. Shift left and shift right JRadioButtons should be used to indicate the direction of substitution. A separate read-only text area displays the resulting cipher text. To make life a little easier for the cryptanalyst the space punctuation is preserved in the cipher text. When the cryptographer hits the “Send secret message” button the control of the window disappears and the game switches to the other player, the cryptanalyst.

The cryptanalyst’s window is then displayed, just showing the read-only cipher text. The cryptanalyst is then required to decrypt the cipher and typing the decrypted plain text in a separate text area input field. When satisfied with the decryption she hits the “Decrypted” button. The program must compare the decrypted text with the original plain text and provide a score showing the percentage of characters that were entered in the correct position.

The cryptanalyst should be able to “give up” and ask for the plain text to be displayed, even if they have hit the “Decrypted” button and a score produced. This ends the game. It should be possible to gracefully exit the game at any time.

Hints:
• Use the java.lang.String and java.lang.Character classes. Lots of useful methods.
• Characters have Unicode integer values. So you can add or subtract an integer to/from a character; although you have to do appropriate casting between primitive types.

Napisałem już część kodu (dokładniej część odpowiedzialną za wyświetlenie okien między którymi mozna się przełanczać):

import BreezySwing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.StringTokenizer;
import javax.swing.*;
import javax.swing.border.*;

public class CesarCipher extends JFrame implements ActionListener {
    private JLabel plainLabel = new JLabel("Pleas input a plain text that you want to cipher");
    private JPanel firstPanel = new JPanel();

    private JTextArea plainText = new JTextArea(2,30);
    private JPanel secondPanel = new JPanel();

    private String[] key = {"Select key", "1", "2", "3", "4", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25"};
    private JComboBox box = new JComboBox(key);
    private JPanel thirdPanel = new JPanel();

    private ButtonGroup group = new ButtonGroup();
    private JRadioButton plus = new JRadioButton("Plus");
    private JRadioButton minus = new JRadioButton("Minus");
    private JPanel fourthPanel = new JPanel();

    private JButton cipherText = new JButton("Cipher text!");
    private JPanel fifthPanel = new JPanel();

    private JLabel cipheredLabel = new JLabel("The text after cipher is:" + "\n");
    private JPanel sixthPanel = new JPanel();

    private JButton sendText = new JButton("Send text to other player!");
    private JPanel seventhPanel = new JPanel();

    public CesarCipher()
    {
        firstPanel.setLayout(new BorderLayout());
        firstPanel.add(plainLabel);

        secondPanel.setLayout(new BorderLayout());
        secondPanel.add(plainText);

        thirdPanel.setLayout(new BorderLayout());
        thirdPanel.add(box);

        fourthPanel.setLayout(new BorderLayout());
        group.add(plus);
        group.add(minus);
        fourthPanel.add("North", plus);
        fourthPanel.add("South", minus);

        fifthPanel.setLayout(new BorderLayout());
        fifthPanel.add(cipherText);

        sixthPanel.setLayout(new BorderLayout());
        sixthPanel.add(cipheredLabel);

        seventhPanel.setLayout(new BorderLayout());
        seventhPanel.add("South", sendText);

        add(firstPanel);
        add(secondPanel);
        add(thirdPanel);
        add(fourthPanel);
        add(fifthPanel);
        add(sixthPanel);
        add(seventhPanel);

        box.addActionListener(this);
        plus.addActionListener(this);
        minus.addActionListener(this);
        cipherText.addActionListener(this);
        sendText.addActionListener(this);

        setTitle("Cesar Cipher Game - the cryptographer window");
        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocation(0, 0);
        setVisible(true);   
    }

    public void actionPerformed(ActionEvent e) {
        String item = (String) box.getSelectedItem();
        if (item.equals('1')) {
            getContentPane();
        }
        if (e.getSource() == plus) {

        }
        if (e.getSource() == minus) {

        }
        if (e.getSource() == cipherText) {

        }
        if (e.getSource() == sendText) {
            setVisible(false);
            CesarCipher2 cC2 = new CesarCipher2();
            cC2.setVisible(true);
        }
    }

    public static void main(String args[]) {
        new CesarCipher();
        new CesarCipher2();

    }

}

import java.awt.BorderLayout;
import java.awt.FlowLayout;
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.JTextArea;

public class CesarCipher2 extends JFrame implements ActionListener {
    private JLabel cipheredText = new JLabel("The ciphered text is:" + "\n");
    private JPanel firstPanel = new JPanel();

    private JTextArea inputArea = new JTextArea("Input correct text here...", 2, 30);
    private JPanel secondPanel = new JPanel();

    private JButton decryptText = new JButton("Decrypt text!");
    private JPanel thirdPanel = new JPanel();

    private JLabel statistics = new JLabel("The correct amount of letters in text you written is:" + "\n");
    private JPanel fourthPanel = new JPanel();

    private JButton giveUp = new JButton("Give Up");
    private JPanel fifthPanel = new JPanel();

    private JButton playAgain = new JButton("Play Again");
    private JPanel sixthPanel = new JPanel();

    public CesarCipher2() {
        firstPanel.setLayout(new BorderLayout());
        firstPanel.add(cipheredText);

        secondPanel.setLayout(new BorderLayout());
        secondPanel.add(inputArea);

        thirdPanel.setLayout(new BorderLayout());
        thirdPanel.add(decryptText);

        fourthPanel.setLayout(new BorderLayout());
        fourthPanel.add(statistics);

        fifthPanel.setLayout(new BorderLayout());
        fifthPanel.add(giveUp);

        sixthPanel.setLayout(new BorderLayout());
        sixthPanel.add(playAgain);

        add(firstPanel);
        add(secondPanel);
        add(thirdPanel);
        add(fourthPanel);
        add(fifthPanel);
        add(sixthPanel);

        decryptText.addActionListener(this);
        giveUp.addActionListener(this);
        playAgain.addActionListener(this);

        setTitle("Cesar Cipher Game - the cryptoanalys window");
        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocation(401, 301);

    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == decryptText) {

        }
        if (e.getSource() == giveUp) {

        }
        if (e.getSource() == playAgain) {
            setVisible(false);
            CesarCipher cC = new CesarCipher();
            cC.setVisible(true);

        }

    }

}

I teraz mam prośbę aby pomógł mi ktoś nakierować mnie na odopwiedni tok myślenia abym mógł stworzyć metodę która odpowiednio przesunie litery i przeniesie gotowy text do nowego okna. Siedzę nad tym od 2 dni i nawet linijki poprawnego kodu nie napisałem... Będę bardzo wdzięczny za pomoc. Pozdrawiam.</b>

0

Źle zaplanowales logike aplikacji. Ramki pojawiaja sie sekwencyjnie, ale nie na równych prawach.
Zamiast

        public static void main(String args[]) {
                new CesarCipher();
                new CesarCipher2();

        }

to wywolanie akcji klikniecia przycisku w CesarCipher powinno wywolywac CesarCipher2 z zaszyfrowanycm tekstem jako argument. Jesli chodzi o algorytm to nie jest to nic trudnego, w pythonie (nie chce mi sie teraz latac po dokumnetacjach) wygladaloby to

def transform(letter, step):
if letter == ' ':
return letter
pos = ord(letter)
end_pos = ord('z')
begin_pos = ord('a')
# Jesli przesuniety znak miesci sie w alfabecie
if pos + step <= end_pos:
return chr(pos + step)
else:
#Nie miesci sie, wiec zawin
return chr(begin_pos + step - (end_pos - pos) - 1)

def cipher(plain, step):
cipher = ''
for c in plain:
cipher += transform(c, step)
return cipher

/* Jakby to pisać w pythonie, to pewnie byłoby tylko coś takiego:
import string
def cipher(plain, step):
letters = string.ascii_lowercase
codes = letters[step:]+letters[:step]
tab = string.maketrans(letters, codes)
return string.translate(plain, tab)

Ale dla przeróbek w javie, lepiej to napisałeś :) - dryo */

0

Wielkie dzięki za pomoc. Teraz z kolei mam inny problem... Nie udaje mi się zrobić aby za pomocą przycisków JRadioButton zmienić wartość odpowiednio w JComboBox. Mało tego nie wiem kompletnie już jak to zrobić aby zaszyfrować to uwzględniając dane jak i przesłać je do drugiego okna... Normalnie pustka w głowie... siedzę i prubóje ale jakoś mi nie wyhodzi mimo że wcale to nie jest nic trudnego chyba... Za wszelką pomoc będę wdzięczny. Pozdrawiam.

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0