
Originariamente inviata da
andbin
Beh, da una veloce verifica: inviaRichiesta non è invocato nel run del thread ma nel contesto del main, appena dopo la start(). La inizializzazione di cipher è verso la fine del run.
Nel momento in cui invochi inviaRichiesta e si arriva alla riga incriminata, non è affatto detto che cipher sia già stato assegnato.
E comunque anche se:
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
fosse già eseguito, lo assegni nel contesto del tuo thread ma poi inviaRichiesta lo usa nel contesto del main-thread .... e non hai fatto nulla per garantire anche solo la "visibilità" della modifica a 'cipher'.
Quindi il tuo design è "dubbio".

Ho modificato il client.... non l'ho istanziato come thread.
Ancora mi da l'eccezione null pointer :
java.lang.NullPointerException
at appello4Febbraio2014.Client.inviaRichiesta(Client. java:93)
at appello4Febbraio2014.Client.main(Client.java:124)
return e.equals("Richiesta Accettata!"); riga 93
if(client.inviaRichiesta("Gio@falco85@ciao")) riga 124
E' sempre per il motivo che hai detto prima ?
Client modificato :
codice:
package appello4Febbraio2014;
import java.io.*;
import java.net.*;
import java.util.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
public class Client {
MulticastSocket mSocket;
InetAddress groupAddress;
int multicastPort;
InetAddress serverAddress;
Socket gkSocket; //SOCKET PER RICHIEDERE E RICEVERE LA CHIAVE PUBBLICA
int gkPort;
Socket socket;
int serverPort;
Cipher cipher;
public Client(InetAddress groupAddress , InetAddress serverAddress , int multicastPort , int gkPort , int serverPort){
this.groupAddress = groupAddress;
this.serverAddress = serverAddress;
this.multicastPort = multicastPort;
this.gkPort = gkPort;
this.serverPort = serverPort;
try{
mSocket = new MulticastSocket(multicastPort);
mSocket.joinGroup(groupAddress);
gkSocket = new Socket(serverAddress , gkPort);
socket = new Socket(serverAddress , serverPort);
}catch(Exception err){
err.printStackTrace();
}
}//COSTRUTTORE CLIENT
public void riceviChiavePubblica(){
try{
OutputStream out = gkSocket.getOutputStream();
out.write(1);
InputStream in = gkSocket.getInputStream();
byte [] publicKeyByte = new byte [1000];
int numByte = in.read(publicKeyByte);
publicKeyByte = Arrays.copyOf(publicKeyByte, numByte);
X509EncodedKeySpec ks = new X509EncodedKeySpec(publicKeyByte);
KeyFactory kf = KeyFactory.getInstance("RSA");
PublicKey publicKey = kf.generatePublic(ks);
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
out.close();
in.close();
}catch(Exception err){
try{
gkSocket.close();
}catch(Exception err2){
err2.printStackTrace();
}
}
}
public boolean inviaRichiesta(String messaggio){
String e = "";
try{
OutputStream out = socket.getOutputStream();
out.write(cipher.doFinal(messaggio.getBytes()));
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
e = in.readLine();
out.close();
in.close();
socket.close();
}catch(Exception err){
err.printStackTrace();
}
return e.equals("Richiesta Accettata!");
}// INVIA RICHIESTA
public void riceviMessaggio(){
byte [] buf = new byte [256];
DatagramPacket packet = new DatagramPacket(buf , buf.length);
try{
mSocket.receive(packet);
System.out.println(packet);
}catch(Exception err){
err.printStackTrace();
}
}// RICEVI MESSAGGIO
public static void main (String [] args){
try{
InetAddress groupAddress = InetAddress.getByName("224.0.0.1");
int multicastPort = 5700;
InetAddress serverAddress = InetAddress.getByName("127.0.0.1");
int serverPort = 5600;
int gkPort = 5800;
Client client = new Client(groupAddress , serverAddress , multicastPort , gkPort , serverPort );
client.riceviChiavePubblica();
if(client.inviaRichiesta("Gio@falco85@ciao"))
client.riceviMessaggio();
}catch(Exception err){
err.printStackTrace();
}
}//MAIN
}//CLIENT