Académique Documents
Professionnel Documents
Culture Documents
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 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);
}
@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);
@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()));
});
f.setVisible(true);
}
// 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();
}
}