0% found this document useful (0 votes)
43 views4 pages

Network Security Assignment No 4 RSA Implementation Group Members

This document contains the code for an RSA encryption and decryption program in Java. It includes classes for generating the public and private keys, encrypting messages by exponentiating the message bytes to the public key modulo N, and decrypting ciphertexts by exponentiating them to the private key modulo N. The main method creates a GUI with text fields for the encrypted and decrypted messages along with buttons to encrypt and decrypt strings entered by the user.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views4 pages

Network Security Assignment No 4 RSA Implementation Group Members

This document contains the code for an RSA encryption and decryption program in Java. It includes classes for generating the public and private keys, encrypting messages by exponentiating the message bytes to the public key modulo N, and decrypting ciphertexts by exponentiating them to the private key modulo N. The main method creates a GUI with text fields for the encrypted and decrypted messages along with buttons to encrypt and decrypt strings entered by the user.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Network Security

Assignment No 4
RSA Implementation
Group Members:
Muhammad Junaid Ali(P13-6129)
Yaseen Iqbal(p13-6011)
Danish Hassan(P12-6044)

Code:

package rsans;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class RSA


{
private BigInteger p;
private BigInteger q;
private BigInteger N;
private BigInteger phi;
private BigInteger e;
private BigInteger d;
private int bitlength = 1024;
private Random r;

public RSA()
{
r = new Random();
p = BigInteger.probablePrime(bitlength, r);
q = BigInteger.probablePrime(bitlength, r);
N = p.multiply(q);
phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
e = BigInteger.probablePrime(bitlength / 2, r);
while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0)
{
e.add(BigInteger.ONE);
}
d = e.modInverse(phi);
}

public RSA(BigInteger e, BigInteger d, BigInteger N)


{
this.e = e;
this.d = d;
this.N = N;
}

@SuppressWarnings("deprecation")
public static void main(String[] args) throws IOException
{
JFrame f=new JFrame();
f.getContentPane().setBackground( Color.cyan );
f.setSize(400,400);
f.setBackground(Color.RED);
FlowLayout fl=new FlowLayout();
f.setLayout(fl);
JLabel name=new JLabel("Network Security RSA Algorithm");
f.add(name);

JLabel l=new JLabel("Encrypted Message");


f.add(l);
JTextField en=new JTextField(30);
f.add(en);
JLabel l4=new JLabel("String in Bytes");
f.add(l4);
JTextField stb=new JTextField(30);
f.add(stb);
JLabel l1=new JLabel("Decrypted Message");
f.add(l1);
JTextField de=new JTextField(30);
f.add(de);
JLabel l41=new JLabel("Decrypted String in Bytes");
f.add(l41);
JTextField stb1=new JTextField(30);
f.add(stb1);
JButton b=new JButton("Encrypt");
f.add(b);
b.addActionListener(new ActionListener(){

@Override
public void actionPerformed(ActionEvent arg0) {
String s1=en.getText();
RSA rsa = new RSA();
DataInputStream in = new DataInputStream(System.in);
String teststring;
teststring = s1;
stb.setText(bytesToString(teststring.getBytes()));

byte[] encrypted = rsa.encrypt(teststring.getBytes());

byte[] decrypted = rsa.decrypt(encrypted);


String s2=new String(decrypted);
stb1.setText(bytesToString(decrypted));
System.out.println("Decrypted String: " + new
String(decrypted));
de.setText(s2);

});
f.setVisible(true);
}

private static String bytesToString(byte[] encrypted)


{
String test = "";
for (byte b : encrypted)
{
test += Byte.toString(b);
}
return test;
}

// Encrypt message
public byte[] encrypt(byte[] message)
{
return (new BigInteger(message)).modPow(e, N).toByteArray();
}

// Decrypt message
public byte[] decrypt(byte[] message)
{
return (new BigInteger(message)).modPow(d, N).toByteArray();
}
}

You might also like