Jump to content



Photo

[JAVA] error check please?


  • Please log in to reply
2 replies to this topic

#1 Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 25 October 2012 - 23:48

These are my classes.... SwingChatServer wont compile cause of the error

"SwingChatServer.java:80: error: constructor DesEncrypter in class DesEncrypter cannot be applied to given types;
DesEncrypter encrypter = new DesEncrypter(key);
^
required: no arguments
found: SecretKey
reason: actual and formal argument lists differ in length

"
any ideas what this means lol xD I have been sitting here for hours getting all the issues out the way and then it craps on me with this error and I have no idea now... help??

SwingChatServer.class
import java.awt.*;
import java.net.*;
import java.awt.*;		 
import java.awt.event.*;   
import javax.swing.*;	  
import javax.swing.event.*;
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.spec.*;
import chat.*;
public class SwingChatServer extends SwingChatGUI
{
PrintWriter out;
BufferedReader in;
BufferedReader stdin;
String inputLine, outputLine, collect;
public ButtonHandler bHandler = new ButtonHandler();
   
public SwingChatServer (String title)
{
  super (title);
  bHandler = new ButtonHandler ();
  sendButton.addActionListener (bHandler);
}
private class ButtonHandler implements ActionListener
{
  public void actionPerformed (ActionEvent event)
  {
   outputLine = txArea.getText ();
   System.out.println ("Server > " + outputLine);
   out.println (outputLine);
	 }
}
   
public void run () throws IOException
{
  ServerSocket serverSocket = null;
	  
  try
	    {
		 serverSocket = new ServerSocket (4444);
	    }
	    catch (IOException e)
	    {
		 System.err.println ("Could not listen on port: 4444.");
		 System.exit (1);
	    }
	    Socket clientSocket = null;
	    try
	    {
		 clientSocket = serverSocket.accept ();
	    }
	    catch (IOException e)
	    {
		 System.err.println ("Accept failed.");
		 System.exit(1);
	    }
	 out = new PrintWriter (clientSocket.getOutputStream (), true);
	 in = new BufferedReader (new InputStreamReader (clientSocket.getInputStream ()));
	 //stdin = new BufferedReader (new InputStreamReader (System.in));
	    out.println ("Welcome to the Chat Server\n");
	    while ((inputLine = in.readLine ()) != null)
	    {
		 collect = (collect +" \n"+ inputLine);
  System.out.println ("Server < " + inputLine);
		 //rxArea.setText (collect);
try {
    // Generate a temporary key. In practice, you would save this key.
    // See also Encrypting with DES Using a Pass Phrase.
    SecretKey key = KeyGenerator.getInstance("DES").generateKey();
    // Create encrypter/decrypter class
    DesEncrypter encrypter = new DesEncrypter(key);
    // Encrypt
    String encrypted = encrypter.encrypt(collect);
rxArea.setText ("" + encrypted);
    // Decrypt
   // String decrypted = encrypter.decrypt(encrypted);
System.out.println("this is the message" + encrypted);
} catch (Exception e) {
}
	 

	    }
 
	    out.close();
	    in.close();
	    clientSocket.close();
	    serverSocket.close();
    }
    public static void main(String[] args) //throws IOException
    {
   SwingChatServer f = new SwingChatServer ("Chat Server Program");
 
  f.pack ();
  f.setVisible(true);
  try
  {
   f.run ();
  }
  catch (IOException e)
  {
   System.err.println("Couldn't get I/O for the connection to: 194.81.104.118.");
   System.exit(1);
  }
    }
}

SwingChatGUI.class

import java.awt.*;		 
import java.awt.event.*;   
import javax.swing.*;	  
import javax.swing.event.*;
public class SwingChatGUI extends JFrame
{
public JButton sendButton;
public JTextArea txArea, rxArea;
 
  public Container container;
	 
	
public SwingChatGUI (String title)
{
  super (title);
 
	    container = getContentPane();			
	    container.setLayout( new FlowLayout() );
	   
  txArea = new JTextArea (6, 40);
 
  rxArea = new JTextArea (6, 40);

  sendButton = new JButton ("Send");
  
  container.add (rxArea);
  container.add (txArea);
  container.add (sendButton);
}

public static void main (String[] args)
{
  Frame f = new SwingChatGUI ("Chat Program");
  f.pack ();
  f.setVisible(true);
}
}

DesEncrypter.class
package chat;
import javax.crypto.*;
import java.security.spec.*;
import java.io.*;
public class DesEncrypter {
    Cipher ecipher;
    Cipher dcipher;
   public void DesEncrypter(SecretKey key) {
	    try {
		    ecipher = Cipher.getInstance("DES");
		    dcipher = Cipher.getInstance("DES");
		    ecipher.init(Cipher.ENCRYPT_MODE, key);
		    dcipher.init(Cipher.DECRYPT_MODE, key);
	    } catch (javax.crypto.NoSuchPaddingException e) {
	    } catch (java.security.NoSuchAlgorithmException e) {
	    } catch (java.security.InvalidKeyException e) {
	    }


    }
    public String encrypt(String str) {
	    try {
		    // Encode the string into bytes using utf-8
		    byte[] utf8 = str.getBytes("UTF8");
		    // Encrypt
		    byte[] enc = ecipher.doFinal(utf8);
		    // Encode bytes to base64 to get a string
		    return new sun.misc.BASE64Encoder().encode(enc);
	    } catch (javax.crypto.BadPaddingException e) {
	    } catch (IllegalBlockSizeException e) {
	    } catch (UnsupportedEncodingException e) {
	   
	    }
	    return null;
    }
    public String decrypt(String str) {
	    try {
		    // Decode base64 to get bytes
		    byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
		    // Decrypt
		    byte[] utf8 = dcipher.doFinal(dec);
		    // Decode using utf-8
		    return new String(utf8, "UTF8");
	    } catch (javax.crypto.BadPaddingException e) {
	    } catch (IllegalBlockSizeException e) {
	    } catch (UnsupportedEncodingException e) {
	    } catch (java.io.IOException e) {
	    }
	    return null;
    }
}



#2 DaManZ

DaManZ

    Neowinian

  • Joined: 21-June 07

Posted 26 October 2012 - 00:24

Remove the void from your DesEncrypter constructor...

#3 OP Original Poster

Original Poster

    Systems Developer

  • Tech Issues Solved: 1
  • Joined: 15-July 08
  • Location: my room
  • OS: windows 7/8, Kali, ubuntu, OSx 10.9
  • Phone: Android

Posted 26 October 2012 - 01:01

Remove the void from your DesEncrypter constructor...


derp :p cheers works! now time to build on it... the reason that void was there is because it wouldnt run before i added the try catch statment without it