Skip to content

Instantly share code, notes, and snippets.

@codelance
Created December 2, 2012 01:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codelance/4186463 to your computer and use it in GitHub Desktop.
Save codelance/4186463 to your computer and use it in GitHub Desktop.
Quantum Key Simulator
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Vector;
enum Photon { ZERO, ONE, UNDEFINED; };
class KeyBit {
KeyBit(Photon setBit, String setPol){bit = setBit; polarization = setPol;}
public Photon bit;
public String polarization;
public String toString()
{
return "{"+(bit == Photon.ZERO ? "0" : "1" )+":"+polarization+"}";
}
}
public class QuantumKeySimulator {
String[] polarization_filters_bit_one = new String[]{"\\", "-"};
String[] polarization_filters_bit_zero = new String[]{"/", "|"};
String[] basis = new String[]{"+", "x"};
static final int KEYSIZE = 20;
public class Alice extends Thread{
protected Vector<KeyBit> messages = new Vector<KeyBit>();
protected Vector<KeyBit> archive = new Vector<KeyBit>();
private Vector<String> alicebasisSequence = new Vector<String>();
public void run()
{
try{
for(int i = 0; i < KEYSIZE; i++)
{
sendPhoton();
sleep(1500);
}
}
catch( InterruptedException e)
{
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
private synchronized void sendPhoton() throws InterruptedException, NoSuchAlgorithmException
{
SecureRandom random = SecureRandom.getInstance ("SHA1PRNG");
String polarization = "";
if( random.nextBoolean() )
{
//sending Bit 1
polarization = polarization_filters_bit_one[ random.nextInt(polarization_filters_bit_one.length)];
messages.add(new KeyBit(Photon.ONE, polarization));
}
else
{
//Sending Bit 0
polarization = polarization_filters_bit_zero[ random.nextInt(polarization_filters_bit_zero.length)];
messages.add(new KeyBit(Photon.ZERO, polarization));
}
if( polarization.equals("-") || polarization.equals("|") )
alicebasisSequence.add("+");
else if( polarization.equals("\\") || polarization.equals("/"))
alicebasisSequence.add("x");
//System.out.println("[Alice Sent Photon "+alicebasisSequence.size()+"]: ("+ messages.lastElement() +") Basis="+alicebasisSequence.lastElement());
notify();
}
public synchronized KeyBit receivePhoton() throws InterruptedException {
notify();
while ( messages.size() == 0 )
wait();
KeyBit photon = messages.remove(0);
archive.add(photon);
return photon;
}
public synchronized Vector<String> checkSequence(Vector<String> bobsequence)
{
for(int i = 0; i < bobsequence.size(); i++ )
{
String alice = alicebasisSequence.elementAt(i);
String bob = bobsequence.elementAt(i);
if( !alice.equals( bob ) )
{
alicebasisSequence.setElementAt(null, i);
bobsequence.setElementAt(null, i);
}
}
for(int i = 0; i < alicebasisSequence.size(); i++ )
{
if( alicebasisSequence.get(i) == null )
archive.setElementAt(null, i);
}
while(alicebasisSequence.remove(null));
while(archive.remove(null));
return bobsequence;
}
public synchronized void printMessage()
{
System.out.println("Alice Final Key: "+ archive);
}
}
public class Bob extends Thread{
Alice alice_instance;
private Vector<KeyBit> messages = new Vector<KeyBit>();
private Vector<String> bobbasisSequence = new Vector<String>();
Bob( Alice instance ){ alice_instance = instance; }
public void run(){
if( alice_instance == null)
throw new IllegalArgumentException();
try{
SecureRandom random = SecureRandom.getInstance ("SHA1PRNG");
while( messages.size() != KEYSIZE )
{
KeyBit photon = alice_instance.receivePhoton();
String base = basis[ random.nextInt(basis.length)];
bobbasisSequence.add( base );
if( base.equals("+") && ( photon.polarization.equals("-") || photon.polarization.equals("|")))
messages.add(photon);
else if( base.equals("x") && ( photon.polarization.equals("/") || photon.polarization.equals("\\")))
messages.add(photon);
else if( base.equals("+") )
{
//rectilinear basis
String new_polarization = random.nextBoolean() ? "|" : "-";
Photon new_photon = Photon.ZERO;
messages.add(new KeyBit(new_photon, new_polarization));
}
else
{
//diagonal basis
String new_polarization = random.nextBoolean() ? "/" : "\\";
Photon new_photon = Photon.ZERO ;
messages.add(new KeyBit(new_photon, new_polarization));
}
sleep( 800 );
}
}
catch( InterruptedException e){}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public synchronized void printMessage()
{
System.out.println("Bob Detect Filter: "+bobbasisSequence);
System.out.println("Bob Final Key: "+messages);
}
public synchronized void validateSequence()
{
bobbasisSequence = alice_instance.checkSequence(bobbasisSequence);
for(int i = 0; i < bobbasisSequence.size(); i++ )
{
if( bobbasisSequence.elementAt(i) == null )
messages.set(i, null);
}
while( bobbasisSequence.remove(null) );
while( messages.remove(null)) ;
System.out.println("Bob Reconciled: "+ messages);
}
}
class Eve extends Alice
{
Alice alice_instance;
private Vector<String> evebasisSequence = new Vector<String>();
Eve( Alice instance ){ alice_instance = instance; }
@Override
public void run()
{
try{
while( archive.size() != 20 )
{
MITM();
sleep(500);
}
}
catch(InterruptedException e)
{
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
private synchronized void MITM() throws NoSuchAlgorithmException, InterruptedException
{
SecureRandom random = SecureRandom.getInstance ("SHA1PRNG");
KeyBit photon = alice_instance.receivePhoton();
String base = basis[ random.nextInt(basis.length)];
evebasisSequence.add( base );
if( base.equals("+") && ( photon.polarization.equals("-") || photon.polarization.equals("|")))
{
String new_polarization = random.nextBoolean() ? "|" : "-";
Photon new_photon = photon.bit;
messages.add(new KeyBit(new_photon, new_polarization));
}
else if( base.equals("x") && ( photon.polarization.equals("/") || photon.polarization.equals("\\")))
{
String new_polarization = random.nextBoolean() ? "/" : "\\";
Photon new_photon = photon.bit;
messages.add(new KeyBit(new_photon, new_polarization));
}
else if( base.equals("+") )
{
//rectilinear basis
String new_polarization = random.nextBoolean() ? "|" : "-";
Photon new_photon = Photon.ZERO ;
messages.add(new KeyBit(new_photon, new_polarization));
}
else
{
//diagonal basis
String new_polarization = random.nextBoolean() ? "/" : "\\";
Photon new_photon = Photon.ZERO ;
messages.add(new KeyBit(new_photon, new_polarization));
}
notify();
}
@Override
public synchronized Vector<String> checkSequence(Vector<String> bobsequence)
{
return alice_instance.checkSequence(bobsequence);
}
@Override
public void printMessage()
{
System.out.println("Eve Detect Filter: "+evebasisSequence);
System.out.println("Eve Final Key: "+ archive);
}
}
public void runSimulation()
{
System.out.println("Running Simulation with just Alice and Bob");
Alice alice = new Alice();
alice.start();
Bob bob = new Bob( alice );
bob.start();
while( alice.isAlive() || bob.isAlive());
alice.printMessage();
bob.printMessage();
bob.validateSequence();
System.out.println("Running Simulation with Alice, Bob, and eavesdropper Eve");
Alice alice2 = new Alice();
alice2.start();
Eve eve = new Eve(alice2);
eve.start();
Bob bob2 = new Bob( eve );
bob2.start();
while( alice2.isAlive() || eve.isAlive() || bob2.isAlive());
alice2.printMessage();
eve.printMessage();
bob2.printMessage();
bob2.validateSequence();
}
public static void main(String[] args) {
new QuantumKeySimulator().runSimulation();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment