Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit d01ed85901cb7c46922674988f559e1ec0bb9db6 0 parents
@johanbrook authored
6 .classpath
@@ -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"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
3  .gitignore
@@ -0,0 +1,3 @@
+.classpath/
+bin/
+.project/
17 .project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>RSA</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>
7 README.md
@@ -0,0 +1,7 @@
+# Simple RSA encryption/decryption with Java (GUI)
+
+Since we're studying discrete mathematics in school, and currently reading about modular mathematics and RSA encryption, I thought it would be a nice exercise to whip something up in Java. I didn't want to take advantage of the various built in keypair generators in the JDK, but used a couple of handy methods in the `BigInteger` class instead.
+
+Nothing fancy. Works only with integers, for the time being.
+
+More about RSA: http://en.wikipedia.org/wiki/RSA_(algorithm)
BIN  bin/rsa/Main.class
Binary file not shown
BIN  bin/rsa/RSA.class
Binary file not shown
BIN  bin/rsa/RSAFrame$ButtonListener.class
Binary file not shown
BIN  bin/rsa/RSAFrame.class
Binary file not shown
15 src/rsa/Main.java
@@ -0,0 +1,15 @@
+package rsa;
+
+public class Main {
+
+ /**
+ * @param args
+ */
+ public static void main(String[] args) {
+ RSAFrame j = new RSAFrame();
+ j.setLocation(300, 300);
+ j.setVisible(true);
+
+ }
+
+}
60 src/rsa/RSA.java
@@ -0,0 +1,60 @@
+package rsa;
+
+import java.math.BigInteger;
+import java.security.SecureRandom;
+import java.util.Random;
+
+public class RSA {
+
+ private final static SecureRandom random = new SecureRandom();
+ private final static BigInteger one = new BigInteger("1");
+
+ private BigInteger modulus;
+
+ private BigInteger publicKey;
+ private BigInteger privateKey;
+
+ public RSA(){
+ //BigInteger p = BigInteger.probablePrime(5, new Random());
+ //BigInteger q = BigInteger.probablePrime(5, new Random());
+ BigInteger p = new BigInteger("17");
+ BigInteger q = new BigInteger("23");
+
+ this.modulus = p.multiply(q);
+ this.publicKey = new BigInteger("65537");
+ this.privateKey = publicKey.modInverse(modulus);
+
+ }
+
+
+ public BigInteger encrypt(BigInteger message){
+ return message.modPow(publicKey, modulus);
+ }
+
+ public BigInteger decrypt(BigInteger encrypted){
+ return encrypted.modPow(privateKey, modulus);
+ }
+
+ public String encrypt(String message){
+ return this.encrypt(new BigInteger(message)).toString();
+ }
+
+ public String decrypt(String message){
+ return this.decrypt(new BigInteger(message)).toString();
+ }
+
+ public BigInteger getModulus(){
+ return this.modulus;
+ }
+
+ public String getPublicKey(){
+ return this.publicKey.toString() + this.modulus.toString();
+ }
+
+ @Override
+ public String toString(){
+ return "Public:\t" + this.publicKey +
+ "Private:\t" + this.privateKey +
+ "Modulus:\t" + this.modulus;
+ }
+}
82 src/rsa/RSAFrame.java
@@ -0,0 +1,82 @@
+package rsa;
+
+import java.awt.*;
+import java.awt.event.*;
+
+import javax.swing.*;
+
+
+public class RSAFrame extends JFrame {
+
+ private final int WIDTH = 400;
+ private final int HEIGHT = 300;
+
+ private JTextArea resultArea;
+ private JTextArea input;
+ private JButton decrypt;
+ private JButton encrypt;
+
+ private RSA tool;
+
+ public RSAFrame(){
+ super("Johans RSA");
+ setDefaultCloseOperation(EXIT_ON_CLOSE);
+ setPreferredSize(new Dimension(WIDTH, HEIGHT));
+ setResizable(false);
+
+ setLayout(new GridLayout(3, 1));
+
+ tool = new RSA();
+
+ JLabel pubKey = new JLabel(tool.getPublicKey().toString());
+
+ Font resultFont = new Font("Monaco", Font.PLAIN, 13);
+
+ resultArea = new JTextArea(8, 20);
+ input = new JTextArea(8, 20);
+ resultArea.setFont(resultFont);
+ input.setFont(resultFont);
+ resultArea.setMargin(new Insets(5, 5, 5, 5));
+ input.setMargin(new Insets(5, 5, 5, 5));
+
+
+ decrypt = new JButton("Decrypt");
+ encrypt = new JButton("Encrypt");
+
+ decrypt.addActionListener(new ButtonListener());
+ encrypt.addActionListener(new ButtonListener());
+
+
+ JPanel buttonPane = new JPanel();
+ buttonPane.add(encrypt);
+ buttonPane.add(decrypt);
+ buttonPane.add(pubKey);
+
+ add(input);
+ add(buttonPane);
+ add(resultArea);
+
+ pack();
+ }
+
+
+ private class ButtonListener implements ActionListener {
+
+ @Override
+ public void actionPerformed(ActionEvent e){
+ String result;
+
+ if(e.getSource() == encrypt){
+ result = tool.encrypt(input.getText().trim());
+ System.out.println("lol");
+ }else{
+ result = tool.decrypt(input.getText().trim());
+ }
+
+ resultArea.setText(result);
+
+ }
+ }
+
+
+}
Please sign in to comment.
Something went wrong with that request. Please try again.