ソケットを介して相互に通信する 2 つの Java アプリケーションのセットがあります。通信のほとんどは自動化されています (1 つは何かを送信し、もう 1 つはそれを受信して計算し、別のものを送信します)。ほとんどのプロセスは、メンバーからのキーボード入力で開始され、サーバーとの交換が開始されます。
サーバ:
public class Server implements Runnable {
private static int port = 1234;
private Socket connection;
@SuppressWarnings("unused")
private int id;
private static Primer p;
private static Map<String, Object> users;
private static BigInteger two;
public static void main(String[] args){
two = new BigInteger("2");
int count = 0;
users = new HashMap<String, Object>();
p = new Primer();
p.keygen();
try {
ServerSocket ss1 = new ServerSocket(port);
System.out.println("Serwer włączony");
while(true){
Socket connection = ss1.accept();
Runnable runn = new Server(connection, ++count);
Thread t = new Thread(runn);
t.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Server(Socket s, int i) {
this.connection = s;
this.id = i;
}
@SuppressWarnings("deprecation")
@Override
public void run() {
// TODO Auto-generated method stub
try {
InputStream inStream;
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;
String message="";
String received="";
System.out.println("Chat Server Started");
do{
inStream = connection.getInputStream();
inDataStream = new DataInputStream ( inStream );
received = inDataStream.readUTF();
System.out.println("Client sent: " + received);
outStream = connection.getOutputStream();
outDataStream = new DataOutputStream (outStream);
analyzeREC(received, outDataStream);
outDataStream.flush();
//DataInputStream dis = new DataInputStream(System.in);
//message = dis.readLine();
//System.out.println("Enter your message here: ");
//outDataStream.writeUTF(message);
}while(!message.equals("bye"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
connection.close();
}
catch (IOException e){}
}
}
public void analyzeREC(String msg, DataOutputStream dos){
String[] m = msg.split(" ");
if(m[0].equals("hello")){
m[1] = checkName(m[1]);
users.put(m[1], null);
try {
dos.writeUTF("registered " + m[1]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(m[0].equals("join")){
System.out.println("Client requested join");
try {
dos.writeUTF("gpp " + p.getGpp().toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(m[0].equals("gppConf")){
try {
dos.writeUTF("gpk " + p.getGpk().toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(m[0].equals("gpkConf")){
try {
dos.writeUTF("startJoining");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(m[0].equals("verifyProof")){
Random r = new Random();
if(verifyProof(new BigInteger(m[1]), new BigInteger(m[2]), new BigInteger(m[2]), new BigInteger(m[4]))){
System.out.println("Proof verified");
BigInteger alfafala = new BigInteger(p.getGpp().getLx(), r);
alfafala.setBit(0);
BigInteger betafala = new BigInteger(p.getGpp().getLx(), r);
try {
dos.writeUTF("sendAB " + alfafala.toString() + " " + betafala.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println("Wrong proof");
}
}
}
メンバー:
public class Member {
private static String host = "localhost";
private static int port = 1234;
private static Primer pr;
private static String ident;
private static GroupPublicKey gpk;
private static GroupPublicParameter gpp;
private MemberSignatureKey msk;
private static ProofOfKnowledge U;
@SuppressWarnings("deprecation")
public static void main(String[] args){
InputStream inStream;
pr = new Primer();
DataInputStream inDataStream;
OutputStream outStream;
DataOutputStream outDataStream;
String message = "";
String received = "";
try {
InetAddress addr = InetAddress.getByName(host);
Socket sock;
sock = new Socket(addr, port);
System.out.println("Chat Client Started");
do{
DataInputStream dis = new DataInputStream(System.in);
message = dis.readLine();
outStream = sock.getOutputStream();
outDataStream = new DataOutputStream (outStream);
outDataStream.writeUTF(message);
inStream = sock.getInputStream();
inDataStream = new DataInputStream ( inStream );
received = inDataStream.readUTF();
System.out.println("Server Sent: " + received);
analyzeMSG(received, outDataStream);
outDataStream.flush();
}while(!message.equals("bye"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//private static boolean isPortAvailable(int port) {
//}
public static void analyzeMSG(String msg, DataOutputStream dos){
String[] m = msg.split(" ");
if(m[0].equals("registered")){
ident = m[1];
}
if(m[0].equals("gpp")){
gpp = new GroupPublicParameter(Integer.parseInt(m[1]), Integer.parseInt(m[2]), Integer.parseInt(m[3]), Integer.parseInt(m[4]), Integer.parseInt(m[5]), Integer.parseInt(m[6]), Double.parseDouble(m[7]));
try {
dos.writeUTF("gppConf");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(m[0].equals("gpk")){
gpk = new GroupPublicKey(new BigInteger(m[1]), new BigInteger(m[2]), new BigInteger(m[3]), new BigInteger(m[4]), new BigInteger(m[5]), new BigInteger(m[6]));
try {
dos.writeUTF("gpkConf");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(m[0].equals("startJoining")){
try {
dos.writeUTF("verifyProof " + startJoining());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(m[0].equals("sendAB")){
System.out.println("Proof confirmed");
}
}
public static String startJoining(){
System.out.println("Started join");
Random r = new Random();
int scale = r.nextInt(gpp.getLx());
BigInteger xfala = new BigInteger(scale, r);
scale = ((gpk.getN().multiply(new BigInteger("2"))).subtract(new BigInteger("1"))).bitLength();
BigInteger rfala = Primer.primeLessThan((gpk.getN().multiply(new BigInteger("2"))).subtract(new BigInteger("1")), scale);
BigInteger C1 = (pr.pow(gpk.getG(), xfala).multiply(pr.pow(gpk.getH(), rfala))).mod(gpk.getN());
genProof(C1, xfala, rfala);
return C1.toString() + " " + U.toString();
}
private static void genProof(BigInteger C1, BigInteger xfala, BigInteger rfala){
System.out.println("Generating Proof");
Random r = new Random();
BigInteger t1 = new BigInteger((int) (gpp.getTeta()*(gpp.getLx() + gpp.getK())), r);
BigInteger t2 = new BigInteger((int) (gpp.getTeta()*((2*gpp.getLp()) + gpp.getK())) - 1, r);
BigInteger D = (pr.pow(gpk.getG(), t1).multiply(pr.pow(gpk.getH(), t2))).mod(gpk.getN());
BigInteger cfala = pr.H(gpk.getG().toString() + gpk.getH().toString() + C1.toString() + D.toString());
BigInteger s1fala = t1.subtract(cfala.multiply(xfala));
BigInteger s2fala = t2.subtract(cfala.multiply(rfala));
U = new ProofOfKnowledge(cfala, s1fala, s2fala);
}
問題は、いくつかのメッセージを交換した後、サーバーで writeUTF メソッドが呼び出されますが、以前に送信された確認メッセージ (gppConf) を再入力するまで、「gpk」で始まるメッセージが送信されないことです。それ以降、メッセージを受信するには、すべてのメッセージを 2 倍にする必要があります。その理由は何ですか?