0

im writing a simple RSA encryption program that will send the coded message across a socket, but im getting an error on the output stream. I dont know if the class is getting passed the string correctly or if the output stream is just not working.

import java.util.*;
import java.lang.*;
import java.io.*;
import java.net.*;

public class connect{

    public static Socket socket;

    public connect(Socket t)
    {
        t = socket;
    }


    public void send(String msg)
    {
        try
        {
            OutputStream os = socket.getOutputStream();
            PrintStream out = new PrintStream(os);
            out.print(msg);
            out.flush();


        }catch(Exception e){System.out.println("Error on send : " + e.getMessage());}
    }
}
4
  • 3
    What is the exception's stack trace? Commented Feb 23, 2015 at 12:48
  • One observation: be careful with socket variable as static here. As soon as you will have more than one connection this might lead to nasty bugs... Commented Feb 23, 2015 at 12:55
  • Side note: RSA is more usually used to encrypt a key for another algorithm (e.g. AES) or for signing, not for encrypting a large amount of data Commented Feb 23, 2015 at 12:56
  • By large amount I mean bigger than RSA key size (more details here). Commented Feb 23, 2015 at 13:02

2 Answers 2

1

Your socket is null,

in your constructor

public connect(Socket t)
{
    t = socket;
}

you are setting passed value t to be equal socket, you need to do this other way around

Sign up to request clarification or add additional context in comments.

Comments

0

Shouldn't it be socket = t in the connect method?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.