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());}
    }
}
Wojciech Wirzbicki
  • 3,887
  • 6
  • 36
  • 59
The Frosty
  • 25
  • 1
  • 1
  • 3
    What is the exception's stack trace? – Emile Pels Feb 23 '15 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... – Dario Feb 23 '15 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 – m0skit0 Feb 23 '15 at 12:56
  • By *large amount* I mean bigger than RSA key size ([more details here](http://stackoverflow.com/questions/1199058/how-to-use-rsa-to-encrypt-files-huge-data-in-c-sharp)). – m0skit0 Feb 23 '15 at 13:02

2 Answers2

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

user902383
  • 8,420
  • 8
  • 43
  • 63
0

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

MJ01
  • 21
  • 1