i am facing problem with the button b 3 which is used here for encryption and is not producing any output..
can u help me with this plzz..

import java.io.*;
import java.security.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public final class MsgDigestApp extends java.applet.Applet implements ActionListener
{
private  Button b0,b1,b2,b3;
private TextArea message=new TextArea("ENTER PURCHASE INFORMATION");
private TextArea message1=new TextArea("ENTER ORDER INFORMATION");
private TextArea a=new TextArea();
private TextArea a1=new TextArea();
private TextField digest=new TextField();
private TextField digest1=new TextField();
private TextField digest2=new TextField();
private TextField t=new TextField();
private TextField n=new TextField();
private TextField phi=new TextField();
private TextField pub=new TextField("17");
private TextField en_msg=new TextField();
private MessageDigest sha;
private Label l,l1,s1,s2,s3,s4;
private Label la1,la2,la3,la4,la5,la6;


public void init()
{
this.setLayout(new BorderLayout(10,10)); 

Panel p1=new Panel();
l=new Label("purchase info");
p1.add(l);
p1.add(message);
l1=new Label("Order info");
p1.add(l1);
p1.add(message1);
add(p1,"North");

Panel p2 = new Panel(new GridLayout(4,2, 5, 5));
s1=new Label("hash1");
p2.add(s1);
p2.add(digest);
s2=new Label("hash2");
p2.add(s2);
p2.add(digest1);
s3=new Label("concat");
p2.add(s3);
p2.add(t);
s4=new Label("hash resultant");
p2.add(s4);
p2.add(digest2);
add(p2,"South");
digest.setFont(new Font("Courier", Font.PLAIN, 12));
digest1.setFont(new Font("Courier", Font.PLAIN, 12));
t.setFont(new Font("Courier", Font.PLAIN, 12));
digest2.setFont(new Font("Courier", Font.PLAIN, 12));
b0=new Button("Clear text");
Panel p=new Panel(new GridLayout(2,1,4,4));
b0.addActionListener(this);
p.add(b0);
b1=new Button("Hash");
b1.addActionListener(this);
p.add(b1);
add(p,"West");
setAlgo("sha-1");
Panel p5=new Panel();
la1=new Label("enter p");
p5.add(la1);
p5.add(a);
la2=new Label("enter q");
p5.add(la2);
p5.add(a1);
b2=new Button("Cal N");
b2.addActionListener(this);
p5.add(b2);
p5.add(n);
la3=new Label("phi");
p5.add(la3);
p5.add(phi);
la4=new Label("e");
p5.add(la4);
p5.add(pub);
b3=new Button("Encrypt");
b3.addActionListener(this);
p5.add(b3);
p5.add(en_msg);
add(p5,"Center");
}

 public void itemStateChanged(ItemEvent evt)
   {  if (evt.getStateChange() == ItemEvent.SELECTED) 
         setAlgo((String)evt.getItem());
   }

   private void setAlgo(String alg)
   {  try
      {  sha = MessageDigest.getInstance(alg);
} catch(NoSuchAlgorithmException e)
      {  digest.setText("" + e);
digest1.setText("" + e);
digest2.setText(""+e);
      }
   }
  


public void actionPerformed(ActionEvent evt)
{
String arg=evt.getActionCommand();
if(arg.equals("Hash"))
{
String m=message.getText();
String m1=message1.getText();
String m2=t.getText();

computeDigest(m.getBytes());
computeDigest1(m1.getBytes());
con();
computeDigest2(m2.getBytes());

}
else if(arg.equals("Cal N"))
{
cal_n();

}
else if(arg.equals("Encrypt"))
{
en();
}
else if(arg.equals("Clear text"))
{
message.setText("");
digest.setText("");
message1.setText("");
digest1.setText("");
t.setText("");
digest2.setText("");
n.setText("");
phi.setText("");
pub.setText("");
a.setText("");
a1.setText("");
en_msg.setText("");
}
 }

private void computeDigest(byte[] b) 
{
sha.reset();
sha.update(b);
byte[] hash1=sha.digest();
String d="";
String e="";
int usbyte=0;
for(int i=0;i<hash1.length;i++)
{
usbyte=hash1[i]&0xFF;
if(usbyte<16)
d+="0"+Integer.toHexString(usbyte);
else
d+=Integer.toHexString(usbyte);
}
e=d.toUpperCase();
digest.setText(""+e);

}
private void computeDigest1(byte[] b) 
{

sha.reset();
sha.update(b);
byte[] hash2=sha.digest();
String d="";
String f="";
int usbyte=0;
for(int i=0;i<hash2.length;i++)
{
usbyte=hash2[i]&0xFF;
if(usbyte<16)
d+="0"+Integer.toHexString(usbyte);
else
d+=Integer.toHexString(usbyte);
}
f=d.toUpperCase();
digest1.setText(""+f);

}
private void computeDigest2(byte[] b) 
{

sha.reset();
sha.update(b);
byte[] hash1=sha.digest();
String d="";
String e="";
int usbyte=0;
for(int i=0;i<hash1.length;i++)
{
usbyte=hash1[i]&0xFF;
if(usbyte<16)
d+="0"+Integer.toHexString(usbyte);
else
d+=Integer.toHexString(usbyte);
}
e=d.toUpperCase();
digest2.setText(""+e);
}

private void con()
{
String str,x,y;
x=digest.getText();
y=digest1.getText();
str=x+y;
t.setText(""+str);
}
private void cal_n()
{
String mul1,mul2,mul0,mul;
int x,y,x1,y1,mul3,mul4;
mul1=a.getText();
mul2=a1.getText();
x=Integer.parseInt(mul1);
y=Integer.parseInt(mul2);
mul3=x*y;
mul=String.valueOf(mul3);
n.setText(""+mul);
x1=Integer.parseInt(mul1)-1;
y1=Integer.parseInt(mul2)-1;
mul4=x1*y1;
mul0=String.valueOf(mul4);
phi.setText(""+mul0);
}


public static int mo(int phi2,int e_key)
{
do
{
if((phi2 % e_key)==0)
{
return e_key;
}
else
 return (phi2 % mo(phi2,e_key+2));
}while(e_key<phi2);
}
private void en()
{
double c;
String n1=n.getText();
int n2=Integer.parseInt(n1);
String  hexmsg=digest2.getText();
double Hex=Double.parseDouble(hexmsg);
double E1=1;
for(int i=1;i<=17;i++)
{
E1=E1*Hex;
}
c=E1 % n2;
String c1=Double.toString(c);
en_msg.setText(""+c1);
}

 
public void paint(Graphics g)
{
setBackground(Color.black);
g.setColor(Color.blue);
g.drawRect(0,0,this.getSize().width-1, this.getSize().height-1) ; 

}
}

Please edit your code and wrap it in code tags. See the [ code ] icon above the input area.

Try debugging your code by:
Add a println to your actionlist method to print out the values of the variables it is using.
Add a trailing else clause at the end of the chain of if/else if statements to print out if none of the above were true.

The technique of using separate Strings as labels and to test for in the listener leaves you open to typos. If you mis type the String, it will not be the same in the two places your code requires it to be. Use a
final String NameOfLabel = "The Label";
as the label and to test for.

Member Avatar for masterofpuppets

Hi,

I agree with NormR1 - instead of the getActionCommand() method, you could try evt.getSource(). Here's an example using your code:

public void actionPerformed( ActionEvent evt ) {
    if( evt.getSource() == b1 ) {
        String m=message.getText();
        String m1=message1.getText();
        String m2=t.getText();

        computeDigest(m.getBytes());
        computeDigest1(m1.getBytes());
        con();
        computeDigest2(m2.getBytes());
    }
    else if( evt.getSource() == b2 ) {
        cal_n();
    }
    else if( evt.getSource() == b3 ) {
        en();
    }
    else if( evt.getSource() == b0 ) {
        message.setText("");
        digest.setText("");
        message1.setText("");
        digest1.setText("");
        t.setText("");
        digest2.setText("");
        n.setText("");
        phi.setText("");
        pub.setText("");
        a.setText("");
        a1.setText("");
        en_msg.setText("");
    }
}

and please use code tags next time - it's really hard to see what's going on like that.

Finally, you could try to add print statements in your ActionListener. For example, I usually make sure the listener gets called first, and then add commands to it. Also, try to use more meaningfull names for your elements - b0 doesn't say "Clear" to the person who is reading your code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.