Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Array out of bound exception

    Ihave written the below pieces of code, but i am not able to solve the compilation error here as it shows the error as ArrayOutOfBoundsException error. so please tell me hw to remove the error and please bring the necessary changes

    Code:
    import java.util.*;
     
    public class Cipher
    {
    	private int n;
    	private int p;
    	private int q;
    	private int d;
    	private int phi;
    	private int e;
    	public void generatePQ()
    	{
    		int pickedNumber1 = 0;
    		int pickedNumber2 = 0;
    		boolean b = false;
    		Random rand = new Random(); 
    		while(true)
    		{
    			pickedNumber1 = rand.nextInt(40)+2; 
    			b = isPrime(pickedNumber1);
    			if(b == true)
    			{
    				break;
    			}
    		}
    		setP(pickedNumber1);
    		while(true)
    		{
    			pickedNumber2 = rand.nextInt(40)+2; 
    			b = isPrime(pickedNumber2);
    			if(b == true)
    			{
    				break;
    			}
    		}	
    		setQ(pickedNumber2);
    	}	
    	public static boolean isPrime(int n) 
    	{
    		for(int i=2;i<n;i++) 
    		{
    			if(n%i==0)
    				return false;
    		}	
    		return true;
    	}
     
    	public void setP(int a)
    	{
    		p = a;
    	}
    	public void setQ(int b)
    	{
    		q = b;
    	}
    	public int getP()
    	{
    		return p;
    	}
    	public int getQ()
    	{
    		return q;
    	}
     
    	public void generateNandPhi()
    	{
    		int p = getP();
    		int q = getQ();
    		n = p * q;
    		phi = (p-1) * (q-1);
    		setN(n);
    		setPhi(phi);
    	}
    	public void setN(int a)
    	{
    		n = a;
    	}
    	public void setPhi(int b)
    	{
    		phi = b;
    	}
    	public int getN()
    	{
    		return n;
    	}
    	public int getPhi()
    	{
    		return phi;
    	}
     
    	public int generateEandD()
    	{
    		int e1 = 0;
    		int p_numbers[] = {2,5,7,11,13,17,19,23,29,31,37,41};
    		int phi = getPhi();
    		int n = getN(); 		
    		for(int i = 0; i < 12;i++)
    		{
    			if(((phi%p_numbers[i] ) != 0) && (p_numbers[i] < phi))
    			{
    				e1 = p_numbers[i];
    				setE(e1);
    				break;
    			}
    		}
    		int vals[] = gcd(phi, e1);
    		try
    		{
    			int temp = phi + vals[0];
    			int d1 = temp / e1;
    			setD(d1);
    		}
    		catch(ArithmeticException ex)
    		{
    			System.out.println("Arithmetic Exception occurs");
    		}
    		return e1;
    	}
     
    	public void setE(int a)
    	{
    		e = a;
    	}
    	public int getE()
    	{
    		return e;
    	}
    	public void setD(int a)
    	{
    		d = a;
    	}
    	public int getD()
    	{
    		return d;
    	}
    	public int[] StringToInt(String pt)
    	{
    		int j =0;
    		int k[] = new int[pt.length()];
    		for(int i=0;i<pt.length();++i)
    		{
    			char c = pt.charAt( i );
    			j = (int) c;
    			k[i] = j;
    		}
    		return k;
    	}
    	public int getCipherText(int[] c, int e)
    	{
    		int cipher = 0;
    		try
    		{
    			cipher = (int)((Math.pow(c[c.length-1],e)) % n);
    		}
    		catch(ArrayIndexOutOfBoundsException ex)
    		{
    			System.out.println("Array out of Bounds Exception occured");
    		}
    		return cipher;
    	}
    	public int[] gcd(int m, int n) 
    	{
    		if (n == 0)
    			return new int[] { m, 1, 0 };
    		int[] vals = gcd(n, m % n);
    		int d = vals[0];
    		int a = vals[2];
    		int b = vals[1] - (m / n) * vals[2];
    		return new int[] { d, a, b };
    	}
    	public String IntToString(int cipherInt) 
    	{
    		String cipherString = Integer.toString(cipherInt);
    		return cipherString;
        }
    }
    
    
    import javax.swing.*;
     
    public class CipherInterface extends JFrame
    {
    	public CipherInterface()
    	{
    		setTitle("Cipher");
    		setBounds(200,200,300,200);
    		setResizable(false);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		JPanel panel = new CipherPanel();
    		this.add(panel);
    	}
    }
    
    //Java Syntax (Toggle Plain Text)
    import javax.swing.*;
     
    public class CipherInterfaceTest
    {
    	public static void main(String ar[])
    	{
    		JFrame f = new CipherInterface();
    		f.setVisible(true);
    		Cipher r = new Cipher();
    		r.generatePQ();
    		r.generateNandPhi();
    		int er = r.generateEandD();
    		CipherPanel p = new CipherPanel();
    		String plaintext = p.getS();
    		int m[] = r.StringToInt(plaintext);
    		int l = m.length;
    		//System.out.print("length = " +l);
    		int ct = r.getCipherText(m,er);
    		String sct = r.IntToString(ct);
    		p.setCipher(sct);
    	}
    }
    
    //Java Syntax (Toggle Plain Text)
    import javax.swing.*;
    import java.awt.FlowLayout;
    import java.awt.BorderLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    public class CipherPanel extends JPanel
    {
    	private JTextField enterdata, ciphertext;
    	private JLabel datalabel, ciphertextlabel;
    	private JButton sendbutton, cancelbutton;
    	private String s = new String();
     
    	public CipherPanel()
    	{
    		// displa panel
    		JPanel displaypanel = new JPanel();
    		displaypanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    		// data label
    		datalabel = new JLabel("Enter the message: ");
    		displaypanel.add(datalabel);
    		// enter data text field
    		enterdata = new JTextField(10);
    		enterdata.addActionListener(new ActionListener() 
    		{
    			public void actionPerformed(ActionEvent e) 
    			{
    				setS(enterdata.getText());
    				System.out.print("String value =: "+s);
    			}
    		});
    		displaypanel.add(enterdata);
    		// cipher text label
    		ciphertextlabel = new JLabel("Cipher Text generated: ");
    		displaypanel.add(ciphertextlabel);
    		// ciphertext text field
    		ciphertext = new JTextField(10);
    		ciphertext.setEditable(false);
    		ciphertext.setFocusable(false);
    		displaypanel.add(ciphertext);
     
    		//button panel
    		JPanel buttonpanel = new JPanel();
    		buttonpanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    		// send button
    		sendbutton = new JButton("Send");
    		buttonpanel.add(sendbutton);
    		// cancel button
    		cancelbutton = new JButton("Cancel");
    		buttonpanel.add(cancelbutton);
     
    		// add panels to the main panel
    		this.setLayout(new BorderLayout());
    		this.add(displaypanel,BorderLayout.CENTER);
    		this.add(buttonpanel,BorderLayout.SOUTH);
    	}
    	public void setS(String txt)
    	{
    		s = txt;
    	}
    	public String getS()
    	{
    		System.out.println(s);
    		return s;
    	}
     
    	public void setCipher(String panelCipher)
    	{
    		ciphertext.setText(panelCipher);
    	}
    }
    please help me out?
    Last edited by WA; 02-15-2012 at 01:37 AM.

  • #2
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The current code calls StringToInt which is a empty string. When you pass an empty string to the getCipherText method it is trying to do Math.pow(c[0-1]) which gives you your ArrayIndexOutOfBoundsException because of c[-1].

    I moved your ActionListener to the sendButton section so that the program is not doing anything until the user hits send. I also put one in there for the cancel button so they can exit the program.

    The following should fix the issue and pass a correct string to the methods.

    Code:
    //Java Syntax (Toggle Plain Text)
    import javax.swing.*;
    import java.awt.FlowLayout;
    import java.awt.BorderLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    public class CipherPanel extends JPanel
    {
    	private JTextField enterdata, ciphertext;
    	private JLabel datalabel, ciphertextlabel;
    	private JButton sendbutton, cancelbutton;
    	private String s = "";
            private String plaintext = "";
     
    	public CipherPanel()
    	{
    		// displa panel
    		JPanel displaypanel = new JPanel();
    		displaypanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    		// data label
    		datalabel = new JLabel("Enter the message: ");
    		displaypanel.add(datalabel);
    		// enter data text field
    		enterdata = new JTextField(10);
    		//enterdata.addActionListener(new ActionListener() 
    		//{
    		//	public void actionPerformed(ActionEvent e) 
    		//	{
    		//		setS(enterdata.getText());
    		//		System.out.print("String value =: "+s);
    		//	}
    		//});
    		displaypanel.add(enterdata);
    		// cipher text label
    		ciphertextlabel = new JLabel("Cipher Text generated: ");
    		displaypanel.add(ciphertextlabel);
    		// ciphertext text field
    		ciphertext = new JTextField(10);
    		ciphertext.setEditable(false);
    		ciphertext.setFocusable(false);
    		displaypanel.add(ciphertext);
     
    		//button panel
    		JPanel buttonpanel = new JPanel();
    		buttonpanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    		// send button
    		sendbutton = new JButton("Send");
    		buttonpanel.add(sendbutton);
                    sendbutton.addActionListener(new ActionListener()
                    {
                        public void actionPerformed(ActionEvent e)
                        {
                            setS(enterdata.getText());
                            Cipher r = new Cipher();
                            r.generatePQ();
                            r.generateNandPhi();
                            int er = r.generateEandD();
                            plaintext = getS();
                            int m[] = r.StringToInt(plaintext);
                            int l = m.length;
                            //System.out.print("length = " +l);
                            int ct = r.getCipherText(m,er);
                            String sct = r.IntToString(ct);
                            setCipher(sct);
                        }
                    });
    		// cancel button
    		cancelbutton = new JButton("Cancel");
    		buttonpanel.add(cancelbutton);
                    cancelbutton.addActionListener(new ActionListener()
                    {
                        public void actionPerformed(ActionEvent e)
                        {
                            System.exit(0);
                        }
                    });
     
    		// add panels to the main panel
    		this.setLayout(new BorderLayout());
    		this.add(displaypanel,BorderLayout.CENTER);
    		this.add(buttonpanel,BorderLayout.SOUTH);
    	}
    	public void setS(String txt)
    	{
    		s = txt;
    	}
    	public String getS()
    	{
    		System.out.println(s);
    		return s;
    	}
     
    	public void setCipher(String panelCipher)
    	{
    		ciphertext.setText(panelCipher);
    	}
    }

    Code:
    //Java Syntax (Toggle Plain Text)
    import javax.swing.*;
     
    public class CipherInterfaceTest
    {
    	public static void main(String ar[])
    	{
    		JFrame f = new CipherInterface();
    		f.setVisible(true);
            }
    }
    The other two files did not need to be changed.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •