Friday, February 21, 2014

Sending Java Sealed Objects via Sockets

Since I couldn't find a single example, this is a few examples put together into one working example, thought it'd be useful if I put it on the web

Here's a link to the Gist and below is the code https://gist.github.com/Sarkie/9138396

Server.java

import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
import java.security.spec.*;
import java.security.*;

public class Server {  
 
  
    public static void main(String[] args) throws Exception {
     System.out.println("Server Started");
  
  // Create key
  final char[] password = "secret_password".toCharArray();
  final byte[] salt = "random_salt".getBytes();
  SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
  KeySpec spec = new PBEKeySpec(password, salt, 1024, 128);
  SecretKey tmp = factory.generateSecret(spec);
  SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

  Cipher dcipher = Cipher.getInstance("AES");
  dcipher.init(Cipher.DECRYPT_MODE, secret);   

     ServerSocketChannel ssChannel = ServerSocketChannel.open();
     ssChannel.configureBlocking(true);
     int port = 12345;
     ssChannel.socket().bind(new InetSocketAddress(port));

     while (true) {
   System.out.println("Waiting for a connection...");
      SocketChannel sChannel = ssChannel.accept();

   System.out.println(sChannel.getRemoteAddress().toString() +" connected");
      
   ObjectInputStream ois = new ObjectInputStream(sChannel.socket().getInputStream());

   SealedObject s = (SealedObject)ois.readObject();
   
   SecretObject decryptedSecretObject = (SecretObject) s.getObject(dcipher);     
   
      System.out.println("Server - Packet Data is: '" + decryptedSecretObject.getSecretMessage() + "'");
   
   ois.close();

      System.out.println("Connection ended");
     }
    }
}

Client.java

import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
import java.security.spec.*;
import java.security.*;

public class Client {

    public static void main(String[] args) throws Exception {
 
  String server = "localhost";
  
  if(args.length == 1){
   server = args[0];
  }
 
     System.out.println("Receiver Started");
  
  // Create key
  final char[] password = "secret_password".toCharArray();
  final byte[] salt = "random_salt".getBytes();
  SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
  KeySpec spec = new PBEKeySpec(password, salt, 1024, 128);
  SecretKey tmp = factory.generateSecret(spec);
  SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

  Cipher cipher = Cipher.getInstance("AES");
  cipher.init(Cipher.ENCRYPT_MODE, secret);   

     SocketChannel sChannel = SocketChannel.open();
     sChannel.configureBlocking(true);
  
     if (!sChannel.connect(new InetSocketAddress(server, 12345))) {
   System.out.println("Cannot connect to Server, make sure it is running");
  }
   
  ObjectOutputStream  oos = new ObjectOutputStream(sChannel.socket().getOutputStream());

  SecretObject secretObject = new SecretObjectImpl("007");

  SealedObject so = new SealedObject(secretObject, cipher);

  oos.writeObject(so);
  System.out.println("Sent Sealed Object");

  oos.close();     

     System.out.println("End Receiver");
    }
}

SecretObject.java

import java.io.*;

public interface SecretObject extends Serializable {
   String getSecretMessage();
}

SecretObject.java

import java.io.*;

public class SecretObjectImpl implements SecretObject {
 
 private String _secretMessage = "";
 
 public SecretObjectImpl(String secretMessage) {
  _secretMessage = secretMessage;
 }

 public String getSecretMessage() {
  return _secretMessage;
    }

}

Wednesday, November 13, 2013

“Unable to locate tools.jar” when running ant

You need to set your JAVA_HOME environment variable.

Try this before the ant command

set JAVA_HOME=JDK LOCATION HERE

JDK Location is the root of the JDK installed, such as on mine.

C:\Program Files\Java\jdk1.7.0_45

set JAVA_HOME=C:\Program Files\Java\jdk1.7.0_45

then type SET to check if it worked, and now try your ant command. I also changed my PATH to point to the JDK\bin folder before my JRE\bin folder, but I think Java check JAVA_HOME first.

Thursday, March 21, 2013

XYZ.sln(1, 1): error MSB4025: The project file could not be loaded. Data at the root level is invalid. Line 1, position 1.

If you are getting this error

XYZ.sln(1, 1): error MSB4025: The project file could not be loaded. Data at the root level is invalid. Line 1, position 1.


Look inside the file for invalid Project or EndProject. Usually it is missing an EndProject, because it works in Visual Studio but fails on our Build Server.

Thursday, December 13, 2012

could not load microsoft windows media player firefox plugin in Chrome


I was having this issue.


  1. Check Windows Media Player is installed on your box. Control Panel - Windows Features - Media Features - Windows Media Player.
  2. Install the Firefox Plugin from here http://www.interoperabilitybridges.com/windows-media-player-firefox-plugin-download
  3. Install the Windows Media Player HTML5 Extension for Chrome from here http://www.interoperabilitybridges.com/wmp-extension-for-chrome


Try now

Wednesday, December 12, 2012

Google Play services won't update and can't remote install

I have been having issues for a few months with Google Play and my Nexus One running MIUI. Whenever I went to Google Play Store I'd get the following error.
 But checking the phone showed that I have the latest version, (I even got the apk and installed manually), also I can't remote install any app that I used to. I wiped Google Play, uninstalled updates, also wiped Google Play Services data, still nothing. I had given it up as because I was on a custom rom, but I noticed the other day that my Google Talk app wasn't working, no matter what I did it wouldn't work.

So decided to reinstall the gapps from. Goo.im Downloads This fixed my Google Talk issues and it fixed my Google Play issues, I assume Google sends down messages using Google Talk, which then uses IPC to call Google Play to start off the downloads, or not. Hope this helps someone else.

Monday, December 10, 2012

Least amount of permissions for creating scheduled tasks in Windows 7

I am currently looking at creating Scheduled tasks and I needed to find the permissions needed for creating Schedudled Tasks, usually I would use ProcMon and look for ACCESS_DENIED but it wasn't helping me. Firstly I created a user with zero permissions and ran my tool that would create a Task, it threw an error saying it can't.
runas /user:NoPerms TaskScheduler.exe
I then gave permissions to "%SystemRoot%\Tasks" and it still wouldn't work, after some searching I figured out there is another Tasks folder in "%SystemRoot%\System32\Tasks" That is all that is needed.

Tuesday, November 20, 2012

Disable OpenFeint in Android for Rooted Phones

I really despise OpenFeint integration in any of my games, I noticed on Play store there was an app called CloseFeint which would disable it but the comments aren't great for it.

I therefore looked into it, it is adding a few entries to my hosts file.

127.0.0.1 api.openfeint.com
127.0.0.1 *.openfeint.com
127.0.0.1 *.scoreloop.com

If you know anything about hosts file, you'll know wildcards aren't supported.

So my solution?

Get this amazing ad blocking hosts app.

https://play.google.com/store/apps/details?id=org.adaway

Then if you click Settings/Your Lists you can add your own, so in there add.

openfeint.com
api.openfeint.com
gree.net
api.gree.net
scoreloop.com
api.scoreloop.com

The gree.net stuff is there because gree.net have recently bought OpenFeint and scoreloop seems to be another one.

Once you have added these, you'll have to re-apply the lists, its also good for blocking ad's in games and easy to disable if there are apps that **need** access to OpenFeint in order to work.