package mentalpoker;

import java.io.IOException;
import java.security.InvalidKeyException;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;


/**
 * The Class Poker.
 */
public class Poker {
	
	/** The rsa service. */
	RSAService rsaService;
	public static String myUsername;
	
	/**
	 * Instantiates a new game.
	 *
	 * @param ip the ip to connect to
	 * @throws Exception the exception
	 */
	public Poker(String ip) throws Exception {
		rsaService = new RSAService();
		EncryptedDeck encDeck = null;

		if (ip == null){
			System.out.println("No IP specified. Waiting for connections...\n");
			encDeck = createDeck();
		}
		
		//test of commutative RSA (encrypting and decrypting in a different order)
		RSAService tmprsaService = new RSAService(rsaService.getP(), rsaService.getQ());
		
		//encrypt the already encrypted card again with another rsaservice (different key)
		EncryptedCard deCard =  tmprsaService.encryptEncCard(encDeck.encCards[0]);
		
		//decrypt with the first rsaService
		EncryptedCard ueCard = rsaService.decryptEncCard(deCard);
		
		//then decrypt with the second
		Card c = tmprsaService.decryptCard(ueCard);
		
		//if this prints jiberish then something has gone wrong else all is sweet =)
		System.out.println(String.valueOf(c.cardType) + " of " + c.suit);
		
	}
	
	/**
	 * Creates the deck.
	 *
	 * @return the encrypted deck
	 * @throws BadPaddingException 
	 * @throws InvalidKeyException the invalid key exception
	 * @throws IllegalBlockSizeException the illegal block size exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	private EncryptedDeck createDeck() throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException{
		Deck deck = new Deck(rsaService);
		EncryptedDeck encDeck = new EncryptedDeck();
		for (int i = 0; i < Deck.NUM_CARDS; i++){
			encDeck.encCards[i] = deck.getEncryptedCard(i);
		}
		return encDeck;
	}
	
	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) {
		try {
			String ip = null;
			if (args.length == 1){
				ip = args[0];
			}
			new Poker(ip);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

@@ -175,17 +175,16 @@
 ption;%0A%0A
-%0A
 /**%0A * T
@@ -819,24 +819,137 @@
 getQ());%0A%09%09%0A
+%09%09System.out.println(%22Data after first encryption step: %22 + new String(encDeck.encCards%5B0%5D.cardData) + %22%5Cn%22);%0A%09%09%0A
 %09%09//encrypt 
@@ -1098,16 +1098,114 @@
 ds%5B0%5D);%0A
+%09%09System.out.println(%22Data after second encryption step: %22 + new String(deCard.cardData) + %22%5Cn%22);%0A
 %09%09%0A%09%09//d
@@ -1293,24 +1293,157 @@
 rd(deCard);%0A
+%09%09System.out.println(%22Data after decryption with first key (used in first encryption step): %22 + new String(ueCard.cardData) + %22%5Cn%22);%0A
 %09%09%0A%09%09//then 
@@ -1594,16 +1594,16 @@
 weet =)%0A
-
 %09%09System
@@ -1615,16 +1615,93 @@
 println(
+%22Data after decryption with second key (used in second encryption step): %22 + 
 String.v

Read previous description.