Project TA
Class Main.java
package TA;
import java.math.BigInteger;
import java.util.Random;
import java.io.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Main extends Thread {
static BigInteger p = new BigInteger(“0″), q = new BigInteger(“0″), n = new BigInteger(“0″);
static BigInteger t = new BigInteger(“0″), b = new BigInteger(“0″), s = new BigInteger(“0″);
static Random random = new Random();
static BigInteger ZERO = new BigInteger(“0″);
static BigInteger UNU = new BigInteger(“1″);
static BigInteger DOI = new BigInteger(“2″);
String Ja2, Ia, read;
SHA sha = new SHA();
BigInteger Ja,Ja_inv,sa;
private static ServerSocket serverSocket = null;
private Socket clientSocket = null;
public static final int PORT = 1987;
static void getParameters() {
while (n.bitLength() <= 160) {
while (p.compareTo(UNU) == -1 || q.compareTo(UNU) == -1) {
p = BigInteger.probablePrime(128, random);
q = BigInteger.probablePrime(128, random);
}
n = p.multiply(q);
}
t = p.subtract(UNU).multiply(q.subtract(UNU));
while (b.compareTo(new BigInteger(“3″)) == -1 || b.gcd(t).compareTo(UNU) != 0 || b.compareTo(t.subtract(UNU)) == 1) {
b = BigInteger.probablePrime(t.subtract(UNU).bitLength(), random);
}
s = b.modInverse(t);
}
static void afisareParam() {
System.out.println(“p:” + p);
System.out.println(“q:” + q);
System.out.println(“n:” + n + ” lungime:” + n.bitLength());
System.out.println(“t:” + t);
System.out.println(“b:” + b);
System.out.println(“gcd(b,t)=” + b.gcd(t));
System.out.println(“s:” + s);
System.out.println(“b*s=” + b.multiply(s).mod(t) + ” mod t”);
}
public void run() {
//Executa solicitarea clientului
try {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
read = in.readLine();
System.out.println(read);
out.write(“Hello from TA”+”\n”);
out.flush();
Ia = in.readLine();
System.out.println(“Ia: ” + Ia);
Ja2 = sha.SHA2(Ia);
System.out.println(“Ja string: ” + Ja2);
Ja = sha.SHA1(Ia);
System.out.println(“Ja BigInteger: ” + Ja + ” lungime: “+ Ja.bitLength());
Ja_inv=Ja.modInverse(n);
System.out.println(“Ja_inv: ” + Ja_inv);
sa=Ja_inv.modPow(s, n);
System.out.println(“s_a: ” + sa);
out.write(n+”\n”);
out.write(b+”\n”);
out.write(sa+”\n”);
out.flush();
} catch (Exception e) {
System.err.println(e);
} finally {
// Inchid socketul deschis pentru clientul curent
try {
clientSocket.close();
} catch (IOException e) {
System.err.println(“Socketul nu poate fi inchis \n” + e);
}
}
}
public Main() throws IOException {
getParameters();
afisareParam();
// while (true) {
serverSocket = new ServerSocket(PORT);
System.out.println(” TA -> initiated!”);
try {
//Asteapta un client
clientSocket = serverSocket.accept();
//Executa solicitarea clientului intr-un fir de executie
new Thread(this).start();
} finally {
serverSocket.close();
System.out.println(” TA -> finished!”);
}
// }
}
public static void main(String[] args) throws IOException {
Main m = new Main();
}
}
Class SHA.java
package TA;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class SHA {
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0×0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9)) {
buf.append((char) (‘0′ + halfbyte));
} else {
buf.append((char) (‘a’ + (halfbyte – 10)));
}
halfbyte = data[i] & 0×0F;
} while (two_halfs++ < 1);
}
return buf.toString();
}
public static BigInteger convertToBigInteger(byte[] input) {
BigInteger r = new BigInteger(input);
return r;
}
public static BigInteger SHA1(String text)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md;
md = MessageDigest.getInstance(“SHA-1″);
byte[] sha1hash = new byte[40];
md.update(text.getBytes(“iso-8859-1″), 0, text.length());
sha1hash = md.digest();
return convertToBigInteger(sha1hash);
}
public static String SHA2(String text)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md;
md = MessageDigest.getInstance(“SHA-1″);
byte[] sha1hash = new byte[40];
md.update(text.getBytes(“iso-8859-1″), 0, text.length());
sha1hash = md.digest();
return convertToHex(sha1hash);
}
}
Project A
Class Main.java
package A;
import java.net.*;
import java.io.*;
import java.math.BigInteger;
import java.util.Random;
public class Main {
String identitate = “Eu sunt A”,raspuns=”accept”;
String Ja2, sa2, n2, b2,r2;
BigInteger Ja, sa, n, b, k = new BigInteger(“0″),alfa= new BigInteger(“0″),r,y=new BigInteger(“0″);
byte[] aux;
SHA sha = new SHA();
Random random = new Random();
int NR_VERIFICARI,incercari=0;
static BigInteger ZERO = new BigInteger(“0″);
static BigInteger UNU = new BigInteger(“1″);
static BigInteger DOI = new BigInteger(“2″);
public void Communication() throws Exception {
System.out.println(” A -> initiated!”);
String adresaServer = “127.0.0.1″;
int PORT = 1987;
Socket clientSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
clientSocket = new Socket(adresaServer, PORT);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out.write(“Hello from A” + “\n”);
out.write(identitate + “\n”);
out.flush();
String read = in.readLine();
System.out.println(read);
Ja2 = sha.SHA2(identitate);
System.out.println(“Ja string: ” + Ja2);
Ja = sha.SHA1(identitate);
System.out.println(“Ja BigInteger: ” + Ja);
n2 = in.readLine();
n = new BigInteger(n2, 10);
System.out.println(“n: ” + n);
b2 = in.readLine();
b = new BigInteger(b2, 10);
System.out.println(“b: ” + b);
sa2 = in.readLine();
sa = new BigInteger(sa2, 10);
System.out.println(“sa: ” + sa);
} catch (Exception e) {
System.err.println(e);
System.exit(1);
} finally {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
if (clientSocket != null) {
clientSocket.close();
}
}
clientSocket = null;
out = null;
in = null;
PORT = 1988;
try {
clientSocket = new Socket(adresaServer, PORT);
out = new PrintWriter(clientSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String read = in.readLine();
System.out.println(read);
NR_VERIFICARI=Integer.parseInt(in.readLine());
System.out.println(“Nr verificari: “+ NR_VERIFICARI);
out.write(“Hello from A” + “\n”);
out.flush();
while(raspuns.compareTo(“accept”)==0 && incercari<NR_VERIFICARI)
{
incercari++;
out.write(identitate + “\n”);
out.flush();
int lungime = n.bitLength();
int random_lungime = 0;
while (k.compareTo(UNU) == -1 || k.compareTo(n) >= 0) {
while (random_lungime == 0) {
random_lungime = random.nextInt(lungime);
}
k = BigInteger.probablePrime(random_lungime, random);
}
System.out.println(“k: ” + k);
alfa=k.modPow(b, n);
System.out.println(“alfa: ” + alfa);
out.write(n+”\n”);
out.write(b+”\n”);
out.write(alfa+”\n”);
out.flush();
r2 = in.readLine();
r = new BigInteger(r2, 10);
System.out.println(“r: ” + r);
y=sa.modPow(r, n).multiply(k).mod(n);
System.out.println(“y: ” + y);
out.write(y+”\n”);
out.flush();
raspuns=in.readLine();
System.out.println(“raspuns “+raspuns);
}
} catch (Exception e) {
System.err.println(e);
System.exit(1);
} finally {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
if (clientSocket != null) {
clientSocket.close();
}
}
System.out.println(” A -> finished!”);
}
public static void main(String[] args) throws Exception {
Main m = new Main();
m.Communication();
}
}
Class SHA – la fel ca cea de la TA
Project B
Class Main.java
package B;
import java.math.BigInteger;
import java.util.Random;
import java.io.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class Main extends Thread {
static Random random = new Random();
static SHA sha = new SHA();
static BigInteger ZERO = new BigInteger(“0″);
static BigInteger UNU = new BigInteger(“1″);
static BigInteger DOI = new BigInteger(“2″);
BigInteger verificare = new BigInteger(“0″), Ja, alfa = new BigInteger(“0″), n, b, r = new BigInteger(“0″), y = new BigInteger(“0″);
String Ia, read, alfa2, n2, b2, y2, Ja2;
private static ServerSocket serverSocket = null;
private Socket clientSocket = null;
public static final int PORT = 1988;
int NR_VERIFICARI = 4;
boolean respins = false;
public void run() {
int accepta = 0;
//Executa solicitarea clientului
try {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
out.write(“Hello from B” + “\n”);
out.write(NR_VERIFICARI+”\n”);
out.flush();
read = in.readLine();
System.out.println(read);
while (accepta < NR_VERIFICARI && respins == false) {
Ia = in.readLine();
System.out.println(“Ia: ” + Ia);
n2 = in.readLine();
n = new BigInteger(n2, 10);
System.out.println(“n: ” + n);
b2 = in.readLine();
b = new BigInteger(b2, 10);
System.out.println(“b: ” + b);
alfa2 = in.readLine();
alfa = new BigInteger(alfa2, 10);
System.out.println(“alfa: ” + alfa);
int lungime = n.bitLength();
int random_lungime = 0;
while (r.compareTo(UNU) == -1 || r.compareTo(b) > 0) {
while (random_lungime == 0) {
random_lungime = random.nextInt(lungime);
}
r = BigInteger.probablePrime(random_lungime, random);
}
System.out.println(“r: ” + r);
out.write(r + “\n”);
out.flush();
y2 = in.readLine();
y = new BigInteger(y2, 10);
System.out.println(“y: ” + y);
Ja2 = sha.SHA2(Ia);
System.out.println(“Ja: ” + Ja2);
Ja = sha.SHA1(Ia);
System.out.println(“Ja: ” + Ja);
verificare = Ja.modPow(r, n).multiply(y.modPow(b, n)).mod(n);
System.out.println(“Verificare: ” + verificare);
if (verificare.compareTo(alfa) == 0) {
accepta++;
System.out.println(“Accept !!! “);
out.write(“accept” + “\n”);
out.flush();
} else {
respins = true;
out.write(“respins” + “\n”);
out.flush();
System.out.println(“Reject !!! “);
}
}
if(accepta==NR_VERIFICARI)
{
System.out.println(“IDENTIFICARE REUSITA !!! “);
}
else
{
System.out.println(“IDENTIFICARE ESUATA !!! “);
}
} catch (Exception e) {
System.err.println(e);
} finally {
// Inchid socketul deschis pentru clientul curent
try {
clientSocket.close();
} catch (IOException e) {
System.err.println(“Socketul nu poate fi inchis \n” + e);
}
}
}
public Main() throws IOException {
// while (true) {
serverSocket = new ServerSocket(PORT);
try {
//Asteapta un client
clientSocket = serverSocket.accept();
System.out.println(” B -> initiated!”);
//Executa solicitarea clientului intr-un fir de executie
new Thread(this).start();
} finally {
serverSocket.close();
System.out.println(” B -> finished!”);
}
// }
}
public static void main(String[] args) throws IOException {
Main m = new Main();
}
}
Class SHA - la fel ca la TA