Saturday, September 1, 2012

Send a facebook message using php

Facebook API does not provide any method for sending a message to the inbox of a user. Fortunately, messages can also be sent using XMPP, which is used by facebook chat, but can also be used for sending a message to a user that is offline (i.e., the message will appear in the user's inbox).

In this link you can find a php class that utilizes facebook's XMPP functionality and sends a message to a facebook user.

In order to use this class you need to obtain an API key, by registering your application here. In order to use this class you should provide, your API key, your user id, the current authorization token, and the user id of the user to which you wish to send a message.

In order to get your user id, you can user the facebook php sdk and use the getUser() method of the Facebook class. In order to get the authorization token you can invoke the getAccessToken() of the Facebook class

Wednesday, August 1, 2012

A proxy re-encryption implementation

Proxy re-encryption is scheme that allows a proxy to re-encrypt a ciphertext, encrypted with the public key of a user A, into a ciphertext that can be decrypted with a private key of a user B, without having access to the private key of A or B, as well as to the plaintext.

Green and Ateniese describe an Identity-based proxy re-encryption scheme in their paper and prove its security. An implementation of their solution can be found in my github repository. This is a python implementation using the Charm Crypto tool

Sunday, July 1, 2012

Export excel diagrams to pdf and use them in latex

If you are creating your diagrams using Excel 2007, there is an easy way to export them in .pdf and then use them as figures in your latex documents.

 Open your excel document, select your diagram and then press the office logo and select save as->pdf or exps. Choose a file name and select save as file type tou be PDF (*.pdf). This will result in a pdf file with the diagram and a lot of blank space. In order to remove the blank space use pdfcrop. This a utility is included both in MikTex (latex for windows) as well as in texlive-extra-utils ubuntu package. Alternatevily you can download it from here

Friday, June 1, 2012

Running an IIS7 site from a network drive

Running a web site, located in a network share, in IIS7 can be really tricky, as it usually ends up with IIS7 complaining about permission problem. This usually happens because IIS processes run as a different user, who is not allowed to access network shares. In this blog post it is shown how a web site can be run in the context of a user that is eligible to access a network share.

 But before we start an important note: Running a web site in the context of a privileged user may possibly entail security risks.

In this blog post the following setup is considered: a network drive with IP 192.168.2.6 and share called fotiou, which is password-protected. The share has been mapped, by user User_NAME, to a network drive (Z:), windows have been configured to connect to that drive on start up, and our web site is located in Z:\wordpress.

 From the IIS7 manager console add a new web site. In our case the site is named wordpress. In the Physical path textbox insert the full URI to the website and not the mapped drive (in our case this would be \\192.168.2.6\fotiou\wordpress), and press OK.

Now navigate in the Application Pools located above the Sites option (see the picture below)

From the Application Pools list select the newly created site (named wordpress in our example, as depicted below)

Right click and select Advanced Settings. The in the Process Model tab, edit the Identity option, by selecting the Custom account option, and by setting it to the current user, as in the picture below


Now navigate back to the Sites tree, select your web site and double click the Authentication option


Double click on the Anonymous Authentication option and select Application pool identity


Now your web site can be run without permission problems.

Tuesday, May 1, 2012

Handle Geek characters in an Access database with Java

When reading Greek characters from an access database special action needs to be taken in order to properly display them. The following code prints two string fields named name and surname, stored in an access database:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String filename = "THE_MDB_FILE";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database += filename.trim() + ";DriverID=22;READONLY=true;useUnicode=true}";
dbConnection = DriverManager.getConnection(database);
Statement s = dbConnection.createStatement();
String query = "Select * from Users";
ResultSet rs = s.executeQuery(query);
String name = new String(rs.getBytes("Name"),"ISO8859_7");
String surname = new String(rs.getBytes("SurName"),"ISO8859_7");
System.out.println("Name:" + name + " Surname:" + surname);

Sunday, April 1, 2012

Fast file searching in C#

This code shows how can you search for a file in C# using windows index and OleDB. The code below is tested in windows Vista where file indexing is enable by default. The following function, when called, outpust the fullpath of the file in request. It uses the operating system's file index therefore is very fast!


using System;
using System.Data.OleDb;

namespace MyBrowser
{
    class Browser
    {
        public void findFile(string fileName)
        {
            
            try
            {
                string url = "provider=Search.CollatorDSO.1;" +
                    "EXTENDED PROPERTIES=\"Application=Windows\"";
                OleDbConnection connection = new OleDbConnection(url);
                string queryString = "Select System.ItemPathDisplay from "+
                    "SystemIndex where System.FileName ='" + fileName + "' ";
                Console.WriteLine(queryString);
                OleDbCommand command = new OleDbCommand(queryString, connection);
                connection.Open();
                OleDbDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    Console.WriteLine(reader[0].ToString());
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            
        }
    }
}

Thursday, March 1, 2012

C++ map random value

This code shows how can you get a random value out of a C++ map structure


map<string, int> myMap;
 /*
  * Put code here that fills the map
  */
map<string, int>::iterator iter = myMap.begin();
map<string, int>::size_type myMapSize;
myMapSize = myMap.size();
int random_integer =  intrand(myMapSize);
advance(iter,random_integer);
cout <<iter->first;

Wednesday, February 1, 2012

Create a low layer network protocol with java

Creating a protocol running at layer 3, may be a hard task. But there are times where it needs to be done quick and easy, i.e for testing protocol before implementing it directly to the operating system's kernel. Jpcap is a  open-source, multi platform Java libray that allows protocol designers to implement their protocol amazingly fast.

Before start using Jpcap, WinPcap and Java SDK must be installed. Jpcap wan be downloaded from here. The protocol that is going to be implemented is very simple. It runs over Ethernet and it uses as addresses real names. The protocol is named MyProtocol. The following topology is going to be used:

      PC1:                                   PC2:
mac address:00:03:0d:2e:79:68 -|switch|-mac address:00:90:96:1d:62:f8
MyProtocol address:Alice                MyProtocol address:Bob

A MyProtocol packet consists of a header and a body. The header has an 1 byte field for the local address length, an 1 byte field for the remote address legth, the local address and the remote address. The body just contains some data. In this example PC1 (Alice) will send "HELLO" to PC2 (Bob). The code for the client is quite straightforward. Initially a the network interface to be used is located, then the ethernet packet that will carry the MyProtocol packet is constructed and finally the MyProtocol packed is constructed. The code for the client follows


import java.util.Arrays;
import jpcap.*;
import jpcap.packet.*;

public class MyProtocol {
    public static void main (String args[]){
        //The local mac address
        final byte[] localMacAddress ={0x00,0x03,0x0d,0x2e,0x79,0x68};
        //The remote mac address
        final byte[] remoteMacAddress={0x00,(byte)0x90,(byte)0x96,0x1d,0x62,(byte)0xf8};
        //The index in the network interfaces list that holds the local device
        int localDeviceIndex =-1;
        //Obtain the list of network interfaces
        NetworkInterface[] devices = JpcapCaptor.getDeviceList();
        //Find the local device to use
        for (int i = 0; i < devices.length; i++){
            if (Arrays.equals(devices[i].mac_address,localMacAddress))
               localDeviceIndex = i; 
        }
        //Check wheather an interface with mac address same as the local mac 
        //address has been found
        if (localDeviceIndex ==-1){ //it has not been found, break execution
            System.out.println("A network interface with address equal " +
                                "to localMacAddress has not been found");
            System.exit(-1);
        }
        //Create the data link packet
        EthernetPacket ether=new EthernetPacket();
        //Set the source address
        ether.src_mac = localMacAddress;
        //Set the destination address
        ether.dst_mac = remoteMacAddress;
        //Set the type code to a not common used value
        ether.frametype = 0x0890;
        //Create our protocol packet
        /* Create a header. The header is of the form
         * 
         * local address legth|remote address legth|local address|remote address
         *     1 byte         |    1 byte          |   n bytes    |      n bytes
         * 
         * Suppose that the local address is Alice and the remote is Bob
         */
        final byte[] myProtLcAddr = "Alice".getBytes();
        final byte[] myProtRmAddr = "Bob".getBytes();
        byte[] header = new byte[myProtLcAddr.length + myProtRmAddr.length + 2];
        header[0] = (byte)myProtLcAddr.length;
        header[1] = (byte)myProtRmAddr.length;
        System.arraycopy(myProtLcAddr, 0, header, 2, myProtLcAddr.length);
        System.arraycopy(myProtRmAddr, 0, header, 2 + myProtLcAddr.length, myProtRmAddr.length);
        //Create the body. The body is just a hello message
        byte[] body ="HELLO".getBytes();
        //Build the packet
        Packet packet = new Packet();
        //Add the header and the body tou the packet
        byte[] data = new byte[header.length + body.length];
        System.arraycopy(header, 0, data, 0, header.length);
        System.arraycopy(body, 0, data, header.length, body.length);
        packet.data = data;
        //Add the data link layer
        packet.datalink = ether;
        //Send the packet
        try{
            JpcapCaptor captor=JpcapCaptor.openDevice(devices[localDeviceIndex],1514,false,50);
            JpcapSender sender = captor.getJpcapSenderInstance();
            sender.sendPacket(packet);
            System.out.println("Packet send");
        }catch(Exception e){
            System.out.println("Exception occured " + e.toString());
        }
        
    }
}

The server part is even simpler, the network interface is located and it is set up to listen for specific ethernet type packets. The code follows


import java.util.Arrays;
import jpcap.*;
import jpcap.packet.*;

public class MyProtocolServer {
    public static void main (String args[]){
        //The local mac address
        final byte[] localMacAddress ={0x00,(byte)0x90,(byte)0x96,0x1d,0x62,(byte)0xf8};
        //The index in the network interfaces list that holds the local device
        int localDeviceIndex =-1;
        //Obtain the list of network interfaces
        NetworkInterface[] devices = JpcapCaptor.getDeviceList();
        //Find the local device to use
        for (int i = 0; i < devices.length; i++){
            if (Arrays.equals(devices[i].mac_address,localMacAddress))
               localDeviceIndex = i; 
        }
        //Check wheather an interface with mac address same as the local mac 
        //address has been found
        if (localDeviceIndex ==-1){ //it has not been found, break execution
            System.out.println("A network interface with address equal " +
                                "to localMacAddress has not been found");
            System.exit(-1);
        }
        
        //Open the device and loop until a packet of type 0x0890 arrives
        try{
            JpcapCaptor captor=JpcapCaptor.openDevice(devices[localDeviceIndex],1514,false,50);
            captor.setFilter("ether proto 0x0890", true);
            boolean continueCapture = true;
            while (continueCapture){
                Packet packet = captor.getPacket();
                if (packet !=null ){ // a packet has been captured
                    //retreive the sender address. The firt byte stores the length
                    //and the address starts from the 3rd byte
                    String remoteAddress = new String(packet.data,2,packet.data[0]);
                    System.out.println("Hello received from " + remoteAddress);                
                    continueCapture = false;
                }
            }
            
            
        }catch(Exception e){
            System.out.println("Exception occured " + e.toString());
        }
        
    }

}

Wednesday, January 4, 2012

Adding an applet to an XHTML page

The traditional way to add a java applet to a web page used to be the <applet> tag but this method has been deprecated.Instead the <object> is now used.

The xhtml code to add an applet is quite tricky now. Here is an example of it:

<!--[if !IE]>-->
<object classid="java:vr.class"  
   type="application/x-java-applet" 
   archive="YOUR_JAR.jar" height="HEIGHT" width="WIDTH" > 
<!--[endif]-->
<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"  
   height="HEIGHT" width="WIDTH">  
   <param name="code" value="YOUR_CLASS" />
   <param name="archive" value="YOUR_JAR.jar" />
</object>

More examples and a full explanation of what clsid is can be found here