Knapsack.java
/** main method to test and utilize all methods defined in this
	*   document.
	*   @param args arguments.
	*/
main method to test and utilize all methods defined in this
document.
@param args arguments.
main method to test and utilize all methods defined in this document . @param args arguments .
public static void main(String[] args) {
		BigInteger[] privateS = {new BigInteger("2"),new BigInteger("4"),
		new BigInteger("8"),new BigInteger("16"),new BigInteger("32"),
		new BigInteger("64"), new BigInteger("128"), new BigInteger("256")};
		BigInteger modulus = new BigInteger("1000");
		BigInteger multiplier = new BigInteger("333");
		BigInteger inverse = new BigInteger("997");
		byte[][] message = split("message.txt",1);
		BigInteger[] publicS = new BigInteger[privateS.length];
		publicS = publicSequence(privateS, modulus, multiplier);
		BigInteger[] encrypted = new BigInteger[message.length];
		ExecutorService service1 = Executors.newFixedThreadPool(message.length);
		List<Future> allFutures1 = new ArrayList<>();
		for(int i = 0; i<message.length; i++) {
			Future<BigInteger> future = service1.submit(new EncryptThread(message[i], publicS));
			allFutures1.add(future);
		}
		boolean alive = true;
		System.out.println("Encryption Started");
		while(alive){
			alive = false;
			for(int k = 0; k < allFutures1.size() && alive == false; k++) {
				if(!allFutures1.get(k).isDone()) {
					alive = true;
				}
			}
		}
		System.out.println("Encryption Finished");
		for(int i = 0; i<encrypted.length; i++) {
			try {
				encrypted[i] = (BigInteger) (allFutures1.get(i)).get();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
		}
		service1.shutdown();
		try {
			FileOutputStream fw = new FileOutputStream("encrypted.txt");
			for(int i = 0; i< encrypted.length; i++) {
					fw.write(encrypted[i].intValue());
			}
			fw.close();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
		int [][] decrypted = new int[message.length][];
		ExecutorService service2 = Executors.newFixedThreadPool(encrypted.length);
		List<Future> allFutures2 = new ArrayList<>();
		for(int i = 0; i<encrypted.length; i++) {
			Future<int []> future = service2.submit(new DecryptThread(encrypted[i],privateS, inverse,modulus));
			allFutures2.add(future);
		}
		alive = true;
		System.out.println("Decryption Started");
		while(alive){
			alive = false;
			for(int k = 0; k < allFutures2.size() && alive == false; k++) {
				if(!allFutures2.get(k).isDone()) {
					alive = true;
				}
			}
		}
		System.out.println("Decryption Finished");
		for(int i = 0; i<encrypted.length; i++) {
			try {
				decrypted[i] = (int[]) (allFutures2.get(i)).get();
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
		}
		service2.shutdown();
		try {
			FileOutputStream fw = new FileOutputStream("output.txt");
			for(int i = 0; i< decrypted.length; i++) {
				for(int j = 0; j<decrypted[i].length; j++) {
					fw.write(decrypted[i][j]);
				}
			}
			fw.close();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
	}
public static void main ( String [ ] args ) { BigInteger [ ] privateS = { new BigInteger ( "2" ) , new BigInteger ( "4" ) , new BigInteger ( "8" ) , new BigInteger ( "16" ) , new BigInteger ( "32" ) , new BigInteger ( "64" ) , new BigInteger ( "128" ) , new BigInteger ( "256" ) } ; BigInteger modulus = new BigInteger ( "1000" ) ; BigInteger multiplier = new BigInteger ( "333" ) ; BigInteger inverse = new BigInteger ( "997" ) ; byte [ ] [ ] message = split ( "message.txt" , 1 ) ; BigInteger [ ] publicS = new BigInteger [ privateS . length ] ; publicS = publicSequence ( privateS , modulus , multiplier ) ; BigInteger [ ] encrypted = new BigInteger [ message . length ] ; ExecutorService service1 = Executors . newFixedThreadPool ( message . length ) ; List < Future > allFutures1 = new ArrayList < > ( ) ; for ( int i = 0 ; i < message . length ; i ++ ) { Future < BigInteger > future = service1 . submit ( new EncryptThread ( message [ i ] , publicS ) ) ; allFutures1 . add ( future ) ; } boolean alive = true ; System . out . println ( "Encryption Started" ) ; while ( alive ) { alive = false ; for ( int k = 0 ; k < allFutures1 . size ( ) && alive == false ; k ++ ) { if ( ! allFutures1 . get ( k ) . isDone ( ) ) { alive = true ; } } } System . out . println ( "Encryption Finished" ) ; for ( int i = 0 ; i < encrypted . length ; i ++ ) { try { encrypted [ i ] = ( BigInteger ) ( allFutures1 . get ( i ) ) . get ( ) ; } catch ( InterruptedException e ) { e . printStackTrace ( ) ; } catch ( ExecutionException e ) { e . printStackTrace ( ) ; } } service1 . shutdown ( ) ; try { FileOutputStream fw = new FileOutputStream ( "encrypted.txt" ) ; for ( int i = 0 ; i < encrypted . length ; i ++ ) { fw . write ( encrypted [ i ] . intValue ( ) ) ; } fw . close ( ) ; } catch ( Exception e ) { System . err . println ( e . getMessage ( ) ) ; } int [ ] [ ] decrypted = new int [ message . length ] [ ] ; ExecutorService service2 = Executors . newFixedThreadPool ( encrypted . length ) ; List < Future > allFutures2 = new ArrayList < > ( ) ; for ( int i = 0 ; i < encrypted . length ; i ++ ) { Future < int [ ] > future = service2 . submit ( new DecryptThread ( encrypted [ i ] , privateS , inverse , modulus ) ) ; allFutures2 . add ( future ) ; } alive = true ; System . out . println ( "Decryption Started" ) ; while ( alive ) { alive = false ; for ( int k = 0 ; k < allFutures2 . size ( ) && alive == false ; k ++ ) { if ( ! allFutures2 . get ( k ) . isDone ( ) ) { alive = true ; } } } System . out . println ( "Decryption Finished" ) ; for ( int i = 0 ; i < encrypted . length ; i ++ ) { try { decrypted [ i ] = ( int [ ] ) ( allFutures2 . get ( i ) ) . get ( ) ; } catch ( InterruptedException e ) { e . printStackTrace ( ) ; } catch ( ExecutionException e ) { e . printStackTrace ( ) ; } } service2 . shutdown ( ) ; try { FileOutputStream fw = new FileOutputStream ( "output.txt" ) ; for ( int i = 0 ; i < decrypted . length ; i ++ ) { for ( int j = 0 ; j < decrypted [ i ] . length ; j ++ ) { fw . write ( decrypted [ i ] [ j ] ) ; } } fw . close ( ) ; } catch ( Exception e ) { System . err . println ( e . getMessage ( ) ) ; } }
main method to test and utilize all methods defined in this
   document.
main method to test and utilize all methods defined in this document .
// split the message up into blocks of appropriate size
 // create the public sequence based on the parameters above
 // create the encrypted array
 // go through the message, and encrypt each block
 // parallelized completed
 //start of encryption threading
 //Makes sure all the threads get done before continuing, could possibly remove because of the .get() method that futures support
 //which might make the program wait until it has something to actually get. But alive is a good safety net. 
 // TODO Auto-generated catch block
 // TODO Auto-generated catch block
 // create the decrypted array, and go through the
 // encrypted sequence and decrypt each block of the
 // message, parallelized completed
 //Start of decryption threading
 // TODO Auto-generated catch block
 // TODO Auto-generated catch block
 // try to write the decrypted message to a file
