Last active
January 2, 2018 20:02
-
-
Save tbenjis/9778334 to your computer and use it in GitHub Desktop.
This uses keyword substitution cryptography. Encodes and decodes a text string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @tbenjis | |
* Keyword substitution cryptography | |
* Author: tunde | |
* | |
* */ | |
import java.awt.EventQueue; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import javax.swing.border.EmptyBorder; | |
import javax.swing.JOptionPane; | |
import javax.swing.JTextPane; | |
import javax.swing.JTextField; | |
import javax.swing.JButton; | |
import javax.swing.JLabel; | |
import java.awt.Font; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
public class KeywordCipher extends JFrame implements ActionListener { | |
private JPanel contentPane; | |
private JTextField keytxt; | |
private JButton btnEncode; | |
private JButton btnDecode; | |
private String values = "abcdefghijklmnopqrstuvwxyz "; | |
private JTextPane textPaneInput; | |
private JTextPane textPaneOutput; | |
/** | |
* Launch the application. | |
*/ | |
public static void main(String[] args) { | |
EventQueue.invokeLater(new Runnable() { | |
public void run() { | |
try { | |
KeywordCipher frame = new KeywordCipher(); | |
frame.setVisible(true); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
}); | |
} | |
/** | |
* Create the frame. | |
*/ | |
public KeywordCipher() { | |
setResizable(false); | |
setTitle("Keyword Cipher"); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setBounds(100, 100, 529, 346); | |
contentPane = new JPanel(); | |
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); | |
setContentPane(contentPane); | |
contentPane.setLayout(null); | |
textPaneInput = new JTextPane(); | |
textPaneInput.setBounds(10, 28, 497, 90); | |
contentPane.add(textPaneInput); | |
textPaneOutput = new JTextPane(); | |
textPaneOutput.setBounds(10, 207, 497, 90); | |
contentPane.add(textPaneOutput); | |
keytxt = new JTextField(); | |
keytxt.setBounds(124, 151, 158, 20); | |
contentPane.add(keytxt); | |
keytxt.setColumns(10); | |
btnEncode = new JButton("Encode"); | |
btnEncode.setBounds(292, 150, 89, 23); | |
contentPane.add(btnEncode); | |
btnDecode = new JButton("Decode"); | |
btnDecode.setBounds(391, 150, 89, 23); | |
contentPane.add(btnDecode); | |
JLabel lblKey = new JLabel("Keyword:"); | |
lblKey.setFont(new Font("Tahoma", Font.BOLD, 11)); | |
lblKey.setBounds(36, 154, 78, 14); | |
contentPane.add(lblKey); | |
JLabel lblText = new JLabel("Text:"); | |
lblText.setFont(new Font("Tahoma", Font.BOLD, 11)); | |
lblText.setBounds(10, 11, 46, 14); | |
contentPane.add(lblText); | |
JLabel lblOutput = new JLabel("Output:"); | |
lblOutput.setFont(new Font("Tahoma", Font.BOLD, 11)); | |
lblOutput.setBounds(10, 189, 46, 14); | |
contentPane.add(lblOutput); | |
btnDecode.addActionListener(this); | |
btnEncode.addActionListener(this); | |
} | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
// TODO Auto-generated method stub | |
if (e.getSource() == btnEncode) { | |
if (keytxt.getText().matches("[a-zA-Z]+")) | |
textPaneOutput.setText(Encode(textPaneInput.getText(), | |
keytxt.getText())); | |
else | |
JOptionPane.showMessageDialog(this, "Enter letters only"); | |
} | |
if (e.getSource() == btnDecode) { | |
if (keytxt.getText().matches("[a-zA-Z]+")) | |
textPaneOutput.setText(Decode(textPaneInput.getText(), | |
keytxt.getText())); | |
else | |
JOptionPane.showMessageDialog(this, "Enter letters only"); | |
} | |
} | |
private String Encode(String text, String key) { | |
char charEnc; | |
int valEnc; | |
char newEnc; | |
String encrypted = ""; | |
key = key.toLowerCase(); | |
text = text.toLowerCase(); | |
//replace repeated characters in key | |
key = key.replaceAll("(.)(?=.*\\1)", ""); | |
String newValue = key; | |
// loop the value and add the rest to newValue | |
for (int i = 0; i < values.length(); i++) { | |
charEnc = values.charAt(i); | |
// check if char exixt in the new key | |
if (newValue.indexOf(charEnc) < 0) { | |
// add the char to new key if is not there | |
newValue += values.charAt(i); | |
} | |
} | |
// now loop through the string | |
for (int i = 0; i < text.length(); i++) { | |
charEnc = text.charAt(i); | |
// check the index of the character at the main values string | |
valEnc = values.indexOf(charEnc); | |
// pick the character of the corresponding newvalue index | |
newEnc = newValue.charAt(valEnc); | |
encrypted += newEnc; | |
} | |
return encrypted.toUpperCase(); | |
} | |
private String Decode(String text, String key) { | |
char charEnc; | |
int valEnc; | |
char newEnc; | |
String decrypted = ""; | |
key = key.toLowerCase(); | |
text = text.toLowerCase(); | |
//replace repeated characters in key | |
key = key.replaceAll("(.)(?=.*\\1)", ""); | |
String newValue = key; | |
// loop the value and add the rest to newValue | |
for (int i = 0; i < values.length(); i++) { | |
charEnc = values.charAt(i); | |
// check if char exixt in the new key | |
if (newValue.indexOf(charEnc) < 0) { | |
// add the char to new key if is not there | |
newValue += values.charAt(i); | |
} | |
} | |
// now loop through the string | |
for (int i = 0; i < text.length(); i++) { | |
charEnc = text.charAt(i); | |
// check the index of the character at the main values string | |
valEnc = newValue.indexOf(charEnc); | |
// pick the character of the corresponding newvalue index | |
newEnc = values.charAt(valEnc); | |
decrypted += newEnc; | |
} | |
return decrypted.toUpperCase(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment