Plaćanje e-novcem
Teorija Original SSL Echo SSL e-cash Budućnost Certifikati
Download source i klase
Klijent
Trgovac
Banka
Lista
"datoteke"
Verzija trgovca uz SSL

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

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocketFactory;


class baza {
public int racun;
public int kljuc;
public String KID;

public baza () {
KID=""; racun=kljuc=0;
}
}
public class Trg {
int serverPort = 9822; // PORT
static String ImeServer = new String();
public static void main(String args[]) {

if ( args.length!=1 ) {
System.out.println("? Upotreba: java Trg Ime_Servera_Banke");
System.exit(1);
}
Trg.ImeServer = args[0];
// Kreira objekt servera i pokrece ga
Trg server = new Trg();
server.run();
}

public Trg() {
super();
}

public void run() {

try {

// Kreira serverov socket na specificiranom portu
// ServerSocket server = new ServerSocket(serverPort);
SSLServerSocketFactory sslserversocketfactory =
(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();

SSLServerSocket sslserversocket = (SSLServerSocket)sslserversocketfactory.createServerSocket(serverPort);


do {
// Vrti i ceka nadolazece konekcije
//Socket client = server.accept();
SSLSocket client = (SSLSocket)sslserversocket.accept();

// Kreira novu DRETVU za svaku konekciju (uspostavljenu vezu)
(new ServerThread(client)).start();

} while ( true );

} catch ( IOException ex ) {

System.exit(0);

}
}
}

class ServerThread extends Thread {

SSLSocket client;
final String imeTrg = "inet";

// Pohrani referencu na socket na koji je klijent prikljucen
public ServerThread(SSLSocket client) {
this.client = client;
}

// Ulazak u dretvu (Thread's entry point)
public void run() {

try {

// Kreira tokove za komunikaciju sa klijentom
ServiceOutputStream outStream = new ServiceOutputStream(new BufferedOutputStream(client.getOutputStream()));
ServiceInputStream inStream = new ServiceInputStream(client.getInputStream());

// Citanje klijentovog zahtjeva
ServiceRequest request = inStream.getRequest();

// obrada zahtjeva
while ( processRequest(outStream,inStream.strbla) ) {
};
outStream.close();
} catch ( IOException ex ) {

System.exit(0);

}

try {

client.close();

} catch ( IOException ex ) {

System.exit(0);

}

}

private boolean provjera (String strNovac) {
// Kontrolna je jednaka zbroju SVIH prethodnih

int intChk1, intChk2, intChk3, Sum=0;
boolean boolUspio = true;
intChk1=intChk2=intChk3=0;
// Kontrolne znamenke

intChk1=strNovac.charAt(7)-48;
intChk2=strNovac.charAt(14)-48;
intChk3=strNovac.charAt(18)-48;

// Petlja provjere
for ( short i=1;i<strNovac.length();i++ ) {

if ( (i==7)&&(Sum%8!=intChk1) ) boolUspio=false;
if ( (i==14)&&(Sum%8!=intChk2) ) boolUspio=false;
if ( (i==18)&&(Sum%8!=intChk3) ) boolUspio=false;
Sum+=strNovac.charAt(i)-48;

}

return boolUspio;


}

public synchronized boolean processRequest(ServiceOutputStream outStream, String podatak) {
System.out.println("====================\nObrada kupovine...");
//ako je novcanica i prosla je provjeru
if ( podatak.charAt(0)=='!'&&(provjera(podatak)==true) ) {
komunikacija kom = new komunikacija(Trg.ImeServer,9811);
kom.zahtjev("t"+podatak+"|"+imeTrg);
try {

if ( kom.prihvat==true ) {
System.out.println("Roba poslana!!!!");
outStream.println("Roba poslana!!!");
}
if ( kom.prihvat==false ) {
System.out.println("Novcanica nije prosla !");
outStream.println("Transakcija NIJE uspijela !");
}

} catch ( IOException IOE ) {
System.out.println("!!! Banka je srusena !!!\n"+IOE);
}


} else {
try {
System.out.print("LOPOV!!!!!!\n");
outStream.println("LOPOVE!!!");
} catch ( IOException IOE ) {
System.out.println(IOE);
}

}

return false;


}

}

////////////////////////////////////////////////////////////////////////////////////
// KLASA komunikacije //
////////////////////////////////////////////////////////////////////////////////////

class komunikacija {

private InetAddress host;
private String strZahtjev;
private int intPort;
public String ime,IP_adr;
public String Stanje;
public String strNovcanica;
public boolean prihvat;


public komunikacija(String strURL,int Prt) {
Stanje= new String();
strNovcanica = new String ();
intPort=Prt; prihvat=false;
try {
host = InetAddress.getByName(strURL);
ime = host.getHostName();
IP_adr = host.getHostAddress();
} catch ( UnknownHostException ex ) {
ime="? Unknown host";
IP_adr="xxx.xxx.xxx.xxx";
}
}

public void getPage(SSLSocket clientSocket) {

try {
// Pribavlja ulazno/izlazne tokovove
DataOutputStream outbound = new DataOutputStream(
clientSocket.getOutputStream() );
DataInputStream inbound = new DataInputStream(
clientSocket.getInputStream() );

// Salje zahtjev BANCI - KORAK 4
outbound.writeBytes(strZahtjev+"\r\n");
/////////////////////////////////////////////////////////////////////////////////

// ceka odgovor od BANKE - KORAK 5
String responseLine = new String();
responseLine ="";
while ( (responseLine = inbound.readLine()) != null ) {

if ( responseLine.startsWith("BOK1") ) {
prihvat=true;
} else if ( responseLine.startsWith("BNO0") ) {
prihvat=false;

}
}
outbound.close();
inbound.close();
clientSocket.close();
///////////////////////////////////////////////

} catch ( IOException ioe ) {
System.out.println("IOException: " + ioe);
}

}


public void zahtjev(String sto) {
String novac = new String();
try {
// Otvara klijentov socket za vezu
// SSLSocket clientSocket1 = new Socket(IP_adr, intPort);
SSLSocketFactory sslclient = (SSLSocketFactory)SSLSocketFactory.getDefault();
SSLSocket clientSocket1 = (SSLSocket)sslclient.createSocket(IP_adr,intPort);

System.out.print(clientSocket1+"\n");
strZahtjev=sto;

getPage(clientSocket1);
} catch ( UnknownHostException uhe ) {
System.out.print("UnknownHostException: " + uhe +"\n");
} catch ( IOException ioe ) {
System.out.print("IOException: " + ioe+"\n");
}


}

}


///////////////////////////////////////////////////////////////////////////////////

// Input stream filter

class ServiceInputStream extends FilterInputStream {
public String strbla;
public ServiceInputStream(InputStream in) {
super(in);
}

public String readLine() throws IOException {

StringBuffer result=new StringBuffer();
boolean finished = false;
boolean cr = false;

do {
int ch = -1;
ch = read();
if ( ch==-1 ) return result.toString();
result.append((char) ch);

if ( cr && ch==10 ) {
result.setLength(result.length()-2);
return result.toString();
}

if ( ch==13 ) cr = true;
else cr=false;

} while ( !finished );


return result.toString();

}


// Metoda za citanje klijentovog zahtjeva iz ulaznog toka
public ServiceRequest getRequest() throws IOException {
ServiceRequest request = new ServiceRequest();
strbla=readLine();
return request;

}

}

// Output stream filter

class ServiceOutputStream extends FilterOutputStream {

public ServiceOutputStream(OutputStream out) {
super(out);
}


public void println() throws IOException {
write(13);
write(10);

}

public void println(String s) throws IOException {
for ( int i=0;i<s.length();++i ) write(s.charAt(i));
println();

}


}


// Klasa implementacije klijentovih zahtjeva
class ServiceRequest {
Vector lines = new Vector();
public String lnm;

public ServiceRequest() {
}

public void addLine(String line) {
lines.addElement(line);
}

public boolean WhichReq() {
lnm=(String) lines.elementAt(0);
return true;
}

}

 

Operacijski sustavi 2 - sigurnost, plaćanje e-novcem