Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaivile committed Aug 19, 2016
0 parents commit 62504b0
Show file tree
Hide file tree
Showing 17 changed files with 676 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>CaesarVigenereCipher</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
11 changes: 11 additions & 0 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8
Binary file added bin/Caesar.class
Binary file not shown.
Binary file added bin/Gui1$Choice.class
Binary file not shown.
Binary file added bin/Gui1.class
Binary file not shown.
Binary file added bin/Gui2.class
Binary file not shown.
Binary file added bin/Gui3.class
Binary file not shown.
Binary file added bin/Main$1.class
Binary file not shown.
Binary file added bin/Main.class
Binary file not shown.
Binary file added bin/Vigenere.class
Binary file not shown.
42 changes: 42 additions & 0 deletions src/Caesar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
public class Caesar{
private String alphabet;
private String shiftedAlphabet;
private int theKey;

public Caesar(int key) {

// get a modulo of a given key; make a shifted alphabet by a key for an encryption
theKey = key % 26;
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
shiftedAlphabet = alphabet.substring(theKey) +
alphabet.substring(0,theKey);
alphabet = alphabet + alphabet.toLowerCase();
shiftedAlphabet = shiftedAlphabet + shiftedAlphabet.toLowerCase();
}

// check if a letter in the input string exists in the alphabet, if so - get it's index and
// take the character from a shifted alphabet by that index; if not, return the same char
private char transformLetter(char c, String from, String to) {
int idx = from.indexOf(c);
if (idx != -1) {
return to.charAt(idx);
}
return c;
}

// iterate through the whole input string, char by char, change each char
private String transform(String input, String from, String to){
StringBuilder sb = new StringBuilder(input);
for (int i = 0; i < sb.length(); i++) {
char c = sb.charAt(i);
c = transformLetter(c, from, to);
sb.setCharAt(i, c);
}
return sb.toString();
}

// return encrypted string
public String encrypt(String input) {
return transform(input, alphabet, shiftedAlphabet);
}
}
130 changes: 130 additions & 0 deletions src/Gui1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Gui1 extends JPanel implements ActionListener{
// set contents for JPanel
static String cc = "Caesar Cipher";
static String vc = "Vigenere Cipher";
private JTextField chooseCipher, chooseOption;
private JRadioButton caesarButton, vigenereButton;
private JButton text, file;
private JPanel contentPanel;

public Gui1(){
super();

// create text fields
chooseCipher = new JTextField("Choose a Cipher:", 0);
chooseCipher.setEditable(false);
chooseOption = new JTextField("Choose an Option:", 0);
chooseOption.setEditable(false);

// create buttons
text = new JButton("Enter Text");
text.setMnemonic(KeyEvent.VK_E);
text.setPreferredSize(new Dimension(160, 80));
file = new JButton("Open File");
file.setMnemonic(KeyEvent.VK_O);
file.setPreferredSize(new Dimension(160, 80));

// create radio buttons
caesarButton = new JRadioButton(cc);
caesarButton.setMnemonic(KeyEvent.VK_C);
caesarButton.setActionCommand(cc);
caesarButton.setSelected(true);
vigenereButton = new JRadioButton(vc);
vigenereButton.setMnemonic(KeyEvent.VK_V);
vigenereButton.setActionCommand(vc);

// group radio buttons
ButtonGroup group = new ButtonGroup();
group.add(caesarButton);
group.add(vigenereButton);

// register a listener to radio buttons
caesarButton.addActionListener(this);
vigenereButton.addActionListener(this);
caesarButton.doClick();

// add and lay out elements on the content panel
contentPanel = new JPanel();
contentPanel.setLayout(new FlowLayout());
contentPanel.add(chooseCipher, BorderLayout.PAGE_START);
contentPanel.add(caesarButton, BorderLayout.LINE_START);
contentPanel.add(vigenereButton, BorderLayout.LINE_END);

contentPanel.add(chooseOption, BorderLayout.CENTER);
contentPanel.add(text, BorderLayout.LINE_START);
contentPanel.add(file, BorderLayout.LINE_END);

add(contentPanel, BorderLayout.CENTER);
setBorder(BorderFactory.createEmptyBorder(0,0,0,0));

// register and add a listener to the buttons
Choice c = new Choice();
text.addActionListener(c);
file.addActionListener(c);
}

// variables to store key and option strings
String k;
String opt;

// set the value to opt if a button is pressed
public class Choice implements ActionListener{
public void actionPerformed(ActionEvent event){
JButton src = (JButton) event.getSource();

if (src.equals(text)){
opt = "text";
putTogether(opt);
}
if (src.equals(file)){
opt = "file";
putTogether(opt);
}
}
}

// set the value to k by a radio button chosen
@Override
public void actionPerformed(ActionEvent e){
JRadioButton src = (JRadioButton) e.getSource();

if (src.equals(caesarButton)){
k = "cc";
}
if (src.equals(vigenereButton)){
k = "vc";
}
}

// show next GUI
public void putTogether(String opt){
if(opt.equals("text")){
Gui2 g2 = new Gui2(k, opt);
g2.createAndShowGUI2(k, opt);
}
else{
Gui3 g3 = new Gui3(k, opt);
g3.createAndShowGUI3(k, opt);
}
}

// create the GUI and show it
void createAndShowGUI(){
//Create and set up the window.
JFrame frame = new JFrame("Encrypt the Text");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
JComponent newContentPane = new Gui1();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

//Display the window.
frame.setSize(900, 300);
frame.setVisible(true);
}
}
174 changes: 174 additions & 0 deletions src/Gui2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Gui2 extends JPanel implements ActionListener{
// set contents for JPanel
private JButton encrypt;
private JPanel contentPanel;
private JTextArea key, textArea, encrypted;
String answer, ciChosen, opChosen;

public Gui2(String k, String opt){
super();

ciChosen = k;

// string for the title of the scroll pane for the key
String encryptKey;
if(k.equals("cc")){
encryptKey = "Key as an int:";
}
else{
encryptKey = "Key as a string:";
}

// create a button
encrypt = new JButton("Encrypt");
encrypt.setMnemonic(KeyEvent.VK_E);
encrypt.setPreferredSize(new Dimension(160, 40));
encrypt.addActionListener(this);

// create text area for the program's output
encrypted = new JTextArea("");
encrypted.setEditable(false);
encrypted.setFont(new Font("Serif", Font.ITALIC, 16));
encrypted.setLineWrap(true);
encrypted.setWrapStyleWord(true);

// create a scroll pane for the program's output
JScrollPane areaEncrypted = new JScrollPane(encrypted);
areaEncrypted.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
areaEncrypted.setPreferredSize(new Dimension(300, 300));
areaEncrypted.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Encrypted text:"),
BorderFactory.createEmptyBorder(5,5,5,5)),
areaEncrypted.getBorder()));

// create text area to enter a key
key = new JTextArea("");
key.setFont(new Font("Serif", Font.ITALIC, 16));
key.setLineWrap(true);
key.setWrapStyleWord(true);

// create a scroll pane for the key
JScrollPane areaKey = new JScrollPane(key);
areaKey.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
areaKey.setPreferredSize(new Dimension(200, 100));
areaKey.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder(encryptKey),
BorderFactory.createEmptyBorder(5,5,5,5)),
key.getBorder()));

// create text area to enter a text
textArea = new JTextArea("");
textArea.setFont(new Font("Serif", Font.ITALIC, 16));
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);

// create a scroll pane for the text
JScrollPane areaScrollPane = new JScrollPane(textArea);
areaScrollPane.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
areaScrollPane.setPreferredSize(new Dimension(300, 100));
areaScrollPane.setBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("Text to encrypt:"),
BorderFactory.createEmptyBorder(5,5,5,5)),
areaScrollPane.getBorder()));

// add and lay out elements on the content panel
contentPanel = new JPanel();
contentPanel.setLayout(new BorderLayout());

contentPanel.add(areaKey, BorderLayout.LINE_START);
contentPanel.add(areaScrollPane, BorderLayout.LINE_END);
contentPanel.add(encrypt);
contentPanel.add(areaEncrypted, BorderLayout.SOUTH);

add(contentPanel);
setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
}

/* Get entered text and entered key, and attempt to encrypt it by a chosen key. New
* variables are created and parsed to the right method by the cipher chosen: Caesar cipher
* shows error dialog if entered key is not an integer; Vigenere cipher shows error dialog
* and breaks form the loop if any character in the key is not a a letter.
*/
@Override
public void actionPerformed(ActionEvent e){
String textToEncrypt = "";
String keytoParse = "";

// read text to encrypt
for(String enteredText: textArea.getText().split("\\n"))
textToEncrypt = enteredText;

// read a key used for encryption
for(String enteredKey: key.getText().split("\\n"))
keytoParse = enteredKey;

// try to parse entered key as an int, on error - key is left unchanged (0), add error dialog
if (ciChosen.equals("cc")){
int finalKey = 0;
try{
finalKey = Integer.parseInt(keytoParse);
}
catch (NumberFormatException nfe){
JOptionPane.showMessageDialog(contentPanel, "Invalid key!",
"Oops!",
JOptionPane.ERROR_MESSAGE);
}

// encrypt a string and show it on the panel
Caesar c = new Caesar(finalKey);
answer = c.encrypt(textToEncrypt);
encrypted.setText(answer);
}
else{
// check every character to be a letter, add error dialog
for (int i = 0; i < keytoParse.length(); i++){
if (!(Character.isLetter(keytoParse.charAt(i)))){
JOptionPane.showMessageDialog(contentPanel, "Invalid key!",
"Oops!",
JOptionPane.ERROR_MESSAGE);
break;
}
}
// encrypt a string and show it on the panel
Vigenere v = new Vigenere(keytoParse);
answer = v.encrypt(textToEncrypt);
encrypted.setText(answer);
}
}

// create the GUI and show it
void createAndShowGUI2(String k, String opt) {
//Create and set up the window.
String cipher;
if(k.equals("cc")){
cipher = "Caesar Cipher";
}
else{
cipher = "Vigener Cipher";
}
JFrame frame = new JFrame(cipher);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
JComponent newContentPane = new Gui2(k, opt);
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

//Display the window.
frame.setSize(900, 500);
frame.setVisible(true);
}
}
Loading

0 comments on commit 62504b0

Please sign in to comment.