Total members 11895 |It is currently Sat Dec 21, 2024 5:59 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka






* Project Name:   RSA encryption decryption cipher implementation( Ron Rivest, Adi Shamir and Leonard Adleman) java code
* Programmer:   msi_333
* Type:   Security
* Technology:  Java
* IDE:   NetBeans


java code
package rsa;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.DocumentFilter;


public class RSAFrame {

/** Creates a new instance of DesMain */


public static void main(String [] rr){
RSAPanel mm = new RSAPanel();
mm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mm.setVisible(true);
}
}
class RSAPanel extends JFrame{
public RSAPanel(){
this.setTitle("RSA");
this.setSize(600,600);
this.setVisible(true);

GenerateGUI();
}

public static JTextArea StepsText = new JTextArea(10,40);


private Container c=this.getContentPane();
private JButton btnCihper = new JButton("Cipher");
private JButton btnDeCihper = new JButton("Decipher");

private JTextArea CipherText = new JTextArea(4,40);
private JTextArea OriginalText = new JTextArea(4,40);
private JTextArea DeCipherText = new JTextArea(4,40);


private JScrollPane OriginalScorl=new JScrollPane(OriginalText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private JScrollPane CipherScorl=new JScrollPane(CipherText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private JScrollPane DecipherScorl=new JScrollPane(DeCipherText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private JScrollPane StepScorl=new JScrollPane(StepsText,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
private JTextField KeyWord = new JTextField(16);
private JTextField KeyWordTwo = new JTextField(16);
private JPanel PanelCipher = new JPanel();
private JPanel PanelDecipher = new JPanel();
private JPanel PanelKeyWord = new JPanel();

private JPanel PanelOriginaltxt = new JPanel();
private JPanel jpstep=new JPanel();

private JLabel lblKeyWord= new JLabel("KeyWord : ");

MainEncry rsa;

private void GenerateGUI(){
c.setLayout(new GridLayout(3,1));

c.setLayout(new GridLayout(4,1));

Document doc = KeyWord.getDocument();
AbstractDocument absDoc = (AbstractDocument )doc;
absDoc.setDocumentFilter(new DocumentSizeFilter(8));
KeyWord.setSize(10,1);


c.setLayout(new FlowLayout());

PanelKeyWord.setLayout(new FlowLayout());
PanelKeyWord.add(lblKeyWord);
PanelKeyWord.add(KeyWord);


PanelOriginaltxt.setBorder(BorderFactory.createTitledBorder("Original Text"));
PanelOriginaltxt.setLayout(new FlowLayout());
PanelOriginaltxt.add(OriginalScorl);

PanelCipher.setBorder(BorderFactory.createTitledBorder("Cipher Text"));
PanelCipher.setLayout(new FlowLayout());
PanelCipher.add(CipherScorl);
PanelCipher.add(btnCihper);

PanelDecipher.setBorder(BorderFactory.createTitledBorder("Decipher Text"));
PanelDecipher.setLayout(new FlowLayout());
PanelDecipher.add(DecipherScorl);
PanelDecipher.add(btnDeCihper);
jpstep.setBorder(BorderFactory.createTitledBorder("Mointor"));
jpstep.setLayout(new FlowLayout());
jpstep.add(StepScorl);


// c.add(PanelKeyWord);

c.add(PanelOriginaltxt);
c.add(PanelCipher);
c.add(PanelDecipher);
c.add(jpstep);

DoActions();

}
private void DoActions(){
ActionHandler action = new ActionHandler();

btnCihper.addActionListener(action);
btnDeCihper.addActionListener(action);

}

private class ActionHandler implements ActionListener{


public void actionPerformed(ActionEvent e) {
String plaintext=OriginalText.getText();
rsa=new MainEncry(plaintext);
if(e.getSource()==btnCihper){
CipherText.append(rsa.getC());






}
if(e.getSource()==btnDeCihper){
DeCipherText.append(rsa.getM());


}
}

}

}

class DocumentSizeFilter extends DocumentFilter {
int maxCharacters;

public DocumentSizeFilter(int maxChars) {
maxCharacters = maxChars;
}

public void insertString(FilterBypass fb, int offs, String str, AttributeSet a) throws BadLocationException {

if ((fb.getDocument().getLength() + str.length()) <= maxCharacters)
super.insertString(fb, offs, str, a);
else
Toolkit.getDefaultToolkit().beep();
}

public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a) throws BadLocationException {

if ((fb.getDocument().getLength() + str.length()- length) <= maxCharacters)
super.replace(fb, offs, length, str, a);
else
Toolkit.getDefaultToolkit().beep();
}

}


java code
package rsa;

import java.math.BigInteger;
import java.util.Random;


public class Miller {

/** Creates a new instance of Miller */
public Miller() {
}
public boolean Test(BigInteger n) {
k=0;
// System.out.println("I am here");
if(n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(0))==0) {
System.out.println("1");
return false;
}
n_1=n.subtract(BigInteger.valueOf(1));
System.out.println(n_1.longValue());
while(n_1.mod(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(0))==0) {
n_1=n_1.divide(BigInteger.valueOf(2));
System.out.println(n_1.longValue());
k++;
if(k>30)return false;
}
q=n_1;

n_1=n.subtract(BigInteger.valueOf(1));

a=BigInteger.valueOf((int)(Math.random()*n_1.longValue()));
System.out.println("a= "+a.longValue());
int i=0;
while(a.compareTo(BigInteger.valueOf(1))<=0||a.compareTo(n_1)>=0) {
System.out.println("a= "+a.longValue());
a=BigInteger.valueOf(Math.abs((int)(Math.random()*n_1.longValue())));
i++;
System.out.println("a= "+a.longValue());
if(i>10)return false;
}

if(((BigInteger)(a.pow((int)q.longValue()))).mod(n).compareTo(BigInteger.valueOf(1))==0) {
return true;
}
for (long j=0;j<k;j++) {
System.out.println("j= "+j+" k="+k);
if(((BigInteger)(a.pow((int)Math.pow(2,j)*(int)q.longValue()))).mod(n).compareTo(n_1)==0) {
return true;
}

}
return false;
}
long k;
BigInteger a;
Random rand=new Random();
BigInteger q;
BigInteger n_1;
class myThread extends Thread {
BigInteger n;
public myThread(BigInteger newn) {
n=newn;
}
public void run() {

Test(n);
}

}
}

java code
package rsa;


import java.math.BigInteger;
import java.util.Random;
import java.util.Scanner;


public class MainEncry {
private Random myrand=new Random();
/** Creates a new instance of Main */
public MainEncry(String Message) {

int x=Integer.parseInt(Message);
M=BigInteger.valueOf(x) ;



p=(int)(Math.random()*M.longValue());
while(primeMiller.Test(BigInteger.valueOf(p))==false||p<=1) {

p=(int)(Math.random()*M.longValue());
System.out.println("P= "+p);
}
q=(int)(Math.random()*M.longValue());
while(primeMiller.Test(BigInteger.valueOf(q))==false||q<=1||p==q) {
System.out.println("Q= "+q);
q=(int)(Math.random()*M.longValue());
}
GF=(p-1)*(q-1);



e=(int)(myrand.nextInt(2000));

while(myGCD.get_GCD(GF,e)!=1||e<=1) {
e=(int)(myrand.nextInt(2000));
}

d=Ext_Eul.ExtendedGCD(e,GF);

n=p*q;

System.out.println("n= "+n+" e= "+e+" d="+d+" M="+M.longValue());
C=(M.pow(e)).mod(BigInteger.valueOf(n));



System.out.println("C ="+C.longValue());
MM=(C.pow(d)).mod(BigInteger.valueOf(n));

System.out.println("M ="+MM.longValue());
// TODO code application logic here
}

/**
* @param args the command line arguments
*/
public String getC(){
return C.toString();

}
public String getM(){
return MM.toString();
}
private Miller primeMiller=new Miller();
private Extended_Eulidean Ext_Eul=new Extended_Eulidean();
private GCD myGCD=new GCD();
private int p=17;
private int q=11;
private int GF;
private int n;
private int e;
private int d;
private BigInteger M;
private BigInteger MM;
private BigInteger C;
}


java code
package rsa;

import java.util.Scanner;


public class GCD {

/** Creates a new instance of GCD */


public int get_GCD(int a,int b) {
int remmender;
while(b>0) {
remmender=a%b;
a=b;
b=remmender;
}
return a;
}
}


java code
package rsa;
import java.util.Scanner;

public class Extended_Eulidean {

public int ExtendedGCD(int e,int GF){
int m=e,b=GF,Q=0;
int T1,T2,T3;


System.out.println("You want to know MI for "+ m + " in " + "GF("+b+")");

A1=1;
A2=0;
A3=b;
B1=0;
B2=1;
B3=m;
System.out.println("Q"+" "+"A1"+" "+"A2"+" "+"A3"+" "+"B1"+" "+"B2"+" "+"B3");
System.out.println(Q+" | "+A1+" | "+A2+" | "+A3+" | "+B1+" | "+B2+" | "+B3);
while(B3!=1){

if(B3==0){
System.out.println("No Multiplicative Inverse "+ A3);
}

Q=A3/B3;

T1=(A1-Q*B1);
T2=(A2-Q*B2);
T3=(A3-Q*B3);
A1=B1;
A2=B2;
A3=B3;
B1=T1;
B2=T2;
B3=T3;
System.out.println(Q+" | "+A1+" | "+A2+" | "+A3+" | "+B1+" | "+B2+" | "+B3);
}
if(B3==1){
System.out.println("The Multiplicative Inverse "+ B2);
int z=0;
float y=0.0f;
if(B2<0) {
System.out.println("Negative");
y=(float)B2/GF;
z=(int)y;
z--;
z*=GF;
B2-=z;
//
// B2*=-1;
// B2=(B2%Q);
// B2 =( B2-(B2/GF)*GF);
}
return B2;
}
System.out.println("The Multiplicative Inverse error !!" );
return 1;
}



private int A1=0;
private int A2=0;
private int A3=0;
private int B1=0;
private int B2=0;
private int B3=0;

}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : RSA encryption decryption cipher algorithm java
 Row Transposition cipher - encryption-decryption java     -  
 Encryption and Decryption encryption Affine cipher code     -  
 Row Transposition cipher - encryption-decryption Csharp(C#)     -  
 Ceasar encryption-decryption-cipher-decipher code     -  
 encryption/ decryption without key using C++     -  
 encryption and decryption in c++     -  
 codes for encryption/decryption algorithms     -  
 Encryption Algorithm{Data Encryption Standard}     -  
 Advanced Encryption Standard (AES)-Example-Cipher (Step1)     -  
 How Row Transposition Encryption Algorithm Work     -  



Topic Tags

Java Algorithms
cron






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com