Tuesday 6 August 2013

Google's Hacking Incidence

Google's is one of the most popular and high traffic web search engines that happen to be a victim of hacking last year when some mathematician attempted to hack Google's 256-bit SSL encryption key. The mathematican is said to have no malicious intent who instead contacted Google on its weakly encrypted system. Google is said to have admitted about the incidence but didn't notice it until the hacker notified them.

Java Native Interface


A programming interface where java programming language interacts with native programming languages such as C/C++. As a programmer I found it hard to resolve away to communicate with native languages especially C which java derives much of its syntax. The following sample code worked well with C and C++ on windows platform and may also work well on other platforms like UNIX.
I'll give first C sample code which you must have the compilers configured before doing any of these procedures.

Using C language;

First open your editor and save the code below in a java source file (.java extension), that is, "HelloWorld.java".

class HelloWorld {
private native void print();
public static void main(String[] args) {
new HelloWorld().print();
}
static {
System.loadLibrary("HelloWorld"); // loads HelloWorld.dll library file
}
}


then save the code below in a C source file (.c extension), that is, "HelloWorld.c".

#include <jni.h>
#include <stdio.h>
#include "HelloWorld.h"
JNIEXPORT void JNICALL
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
printf("Welcome to JNI using C!\n");
return;
}


Lastly you should have a batch script to execute your commands so save the following contents in "build.bat" file.

javac HelloWorld.java
javah HelloWorld
cl /I"C:\Program Files\Java\jdk1.7.0_01\include" /I"C:\Program Files\Java\jdk1.7.0_01\include\win32" /I"C:\Program Files\Microsoft Visual Studio\VC98\Include" /I"C:\Program Files\Microsoft Visual Studio\VC98\Lib" HelloWorld.c /Fe"HelloWorld.dll" /LD
java HelloWorld


You should have a similar output.



Using C++ language;
Open your text editor and save the following contents in a .java file, that is, "JNIPrintWrapper.java".


// File: JNIPrintWrapper.java
// Allows access to native methods

public class JNIPrintWrapper
{
    // load library JNIPrintLibrary into JVM
    static
    {
        System.loadLibrary( "JNIPrintWrapper" );
    }
    // native C++ method
    public native void printMessage( String message );
 }


save the following in a java source file, that is, "JNIPrintMain.java".

// File: JNIPrintMain.java
// Creates a new instance of the Java wrapper class,
// and calls the native methods.

public class JNIPrintMain{

// uses JNI to print a message
 public static void main( String args[] )
{
 JNIPrintWrapper wrapper = new JNIPrintWrapper();

 // call to native methods through JNIWrapper
 wrapper.printMessage( "Welcome to JNI using C++!\n" );
 }
}


save the following in a C++ source file, that is, "JNIPrintWrapperImpl.cpp".

// File: JNIPrintWrapperImpl.cpp
 // Implements the header created by Java
 // to integrate with JNI.

// C++ core header
 #include <iostream.h>

// header produced by javah
 #include "JNIPrintWrapper.h"

 // prints string provided by Java application
 JNIEXPORT void JNICALL Java_JNIPrintWrapper_printMessage
 ( JNIEnv * env, jobject thisObject, jstring message )
 {
 // boolean to determine if string is copied
 jboolean copied;

 // call JNI method to convert jstring to cstring
 const char* charMessage =
 env->GetStringUTFChars( message, &copied );

 // print message
 cout << charMessage;

// release string to prevent memory leak
env->ReleaseStringUTFChars( message, charMessage );
}



You should have a similar output as shown below.



Java Networking


Java is notably known to have good network capabilities that make the java programming world fun especially when it comes to communication. Here is a sample program code to show Client-Server and Server-Client communications using java networking capabilities.

Here is the Client source code.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner;

/*

 * A simple demonstration of setting up a Java network client.

 */

public class Client
{
    static Scanner scanner = new Scanner(System.in);
//    static ServerSocket server;
    static Socket sock;

    public static void main(String[] argv) {

        String server_name = "localhost";
        String client_msg = null;

        try {

            sock = new Socket(server_name, 14323);
            /* Finally, we can read and write on the socket. */           

            System.out.println(" *** Connecting to " +
                sock.getInetAddress().getHostName() + ":" +
                sock.getLocalPort()  + " ***\n");
            while(sock.isConnected())
            {
//                System.out.println(" *** Connected ***\n");
                System.out.print("Enter message:");
                client_msg = scanner.nextLine();
                //send message to server
                sendMessage(client_msg);
                // wait for server's message
               
                // display new message from server
                receiveMessage();
                // handle connections
                //                handle(sock);
            }
            /* . do the I/O here .. */
            if(!sock.isClosed())
            {
                sock.close( );
                System.out.println("\nSocket closed\n");
            }

        } catch (java.io.IOException e) {

            System.err.println("error connecting to " +

                server_name + ": " + e);

            return;
        }
    }

    protected static void sendMessage(String msg) throws IOException
    {
        if(msg == "" || msg == null)
            return; // exit method
        sock.getOutputStream().write(msg.getBytes());
        System.out.println("\nMessage sent.");
    }

    protected static void receiveMessage() throws IOException
    {
        String recvmsg = "";
        int size = 256;
        byte[] buff = new byte[size];
        sock.getInputStream().read(buff);
        for(int i = 0; i < buff.length; i++)
        {
            if(buff[i] != buff[255])
                recvmsg += (char)buff[i];
        }
        System.out.println("\nServer: " + recvmsg);
    }

}


Here is the Server source code.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**

 * Listen -- make a ServerSocket and wait for connections.

 */

public class Server {

    /** The TCP port for the service. */

    public static final short PORT = 14323;

    protected static ServerSocket sock;
    protected static Socket clientSock;
    protected static Scanner scanner;

    public static void main(String[] args) throws IOException
    {
        scanner = new Scanner(System.in);
        String servermsg;
        try {
            sock = new ServerSocket(PORT);

            System.out.println("\nListening on port " +
                    sock.getLocalPort());
                   
            // receive first message
            clientSock = sock.accept();
            while ((clientSock.isConnected()))
            {
//                System.out.println("Accepting from client " +
//                      clientSock.getInetAddress().getHostName() +
//                      ":" + clientSock.getLocalPort() + "\n");
                // wait for client's message first
               
                // display new client's message
                receiveMessage();
                // get server's message from standard input
                System.out.print("Enter message: ");
                servermsg = scanner.nextLine();
                // send server's message
                sendMessage(servermsg);               
               
            }
        }
        catch (IOException e)
        {
            System.err.println(e);
        }
        finally
        {
            sock.close();
            clientSock.close();
        }
    }  

    static void receiveMessage() throws IOException
    {
        int size = 256;
        byte[] recvbuff = new byte[size];
        clientSock.getInputStream().read(recvbuff);
        System.out.print("Client: ");
        for(int i = 0; i < recvbuff.length; i++)
        {
            if(recvbuff[i] != recvbuff[255])
                System.out.print("" + (char)recvbuff[i]);
        }
        System.out.println();
    }

    static void sendMessage(String msg) throws IOException
    {
        if(msg == "" || msg == null)
            return; // exit method
        int size = msg.getBytes().length;
        byte[] sendbuff = new byte[size];
        sendbuff = msg.getBytes();
        clientSock.getOutputStream().write(sendbuff);
        System.out.println("\nMessage sent.\n");
    }  

}



Here is the impressive output I heard you should have something similar on windows.




Qt 4 Software Development


Qt 4 is a C++ Application Framework that is mostly deployed in making of Graphical User Interface Software Applications e.g., VLC Media PLayer is a good example of Qt Software. First, I would want you to know how to configure windows environment for Qt Applications. Download latest Qt creator which may also be shipped together with Qt SDK but you should download latest Qt SDK and also MinGW compiler. Follow setup and install simply as you do with other software but after you install you must configure the environment before development.
  •  You will need an Internet connection to install MinGW compiler.
  • Before Qt installation you should have MinGW installed and MinGW\bin folder in your PATH.
  • After installation navigate to Qt\<sdk version> folder and look for "configure.exe" file.
  • Open that file and choose Open Source Edition where prompted to, that is, type "o" on the shell.
  • Accept license by typing 'yes' and wait for it to finish executing.
  • Make sure your Qt\<sdk version>\bin is in your PATH.
  • Note that you should download Qt SDK that supports MinGW compiler.
 A Hello World sample program is shown below.

#include <QApplication.h>
#include <QPushButton.h>

int main( int argc, char **argv )
{
QApplication a( argc, argv );
QPushButton hello( "Hello World!");
hello.show();
return a.exec();
}


Save the source code into a file "main.cpp".
Create a new file using your notepad or any text editor program and enter the following commands in it.

qmake -project
qmake
make
cd debug
qt

save it as batch script file, that is, "<file name>.bat" file should have a .bat extension.
Open your command line program or double click on the batch script to execute when on the command line drag the batch script file and drop it to the command line program and then press Enter to execute file. You should have an execution similar as below.



You should have some files created such as qt project file, Makefile file and other folders debug, release, etc in that folder that main.cpp file resides. The output should be similar to that below.




Note: You can navigate to the debug folder where you'll find an executable which when you open should give you a GUI window.
Download Available:  Click here