Tuesday, October 7, 2014

Java : File Encryption and Decryption using Blowfish

The Java security APIs span a wide range of areas, including cryptography, public key infrastructure, secure communication, authentication, and access control. Java security technology provides the developer with a comprehensive security framework for writing applications, and also provides the user or administrator with a set of tools to securely manage applications.
Source : http://www.oracle.com/technetwork/java/javase/tech/index-jsp-136007.html

javax.crypto.Cipher
This class provides the functionality of a cryptographic cipher for encryption and decryption. It forms the core of the Java Cryptographic Extension (JCE) framework.
Cipher API : http://docs.oracle.com/javase/6/docs/api/javax/crypto/Cipher.html

Cipher: initialized with keys, these used for encrypting/decrypting data. There are various types of algorithms: symmetric bulk encryption (e.g. AES, DES, DESede, Blowfish, IDEA)
http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html

Blowfish  
Blowfish has a 64-bit block size and a variable key length from 1 bit up to 448 bits
Blowfish Wiki : http://en.wikipedia.org/wiki/Blowfish_%28cipher%29

Here are the general steps to encrypt/decrypt a file in Java -

  • Create a Key from a given byte array for a given algorithm.
  • Get an instance of Cipher class for a given algorithm transformation.
  • Initialize the Cipher with an appropriate mode (encrypt or decrypt) and the given Key.
  • Invoke doFinal(input_bytes) method of the Cipher class to perform encryption or decryption on the input_bytes, which returns an encrypted or decrypted byte array.
  • Read an input file to a byte array and write the encrypted/decrypted byte array to an output file accordingly.


Please see the below java code

Wednesday, September 24, 2014

Javascript code for 2048 number puzzle

Other day I was playing this game on my android mobile and I was so amazed with it that I thought why not try to write a JavaScript code for this game. So here I am with it.

new game

Score :

HOW TO PLAY: Use your arrow keys to move the tiles.

Please see the complete source code -


Output -

Sunday, August 31, 2014

Five Common Questions from JavaScript Beginners

What is JavaScript?


JavaScript is an easy-to-use version of programming language (a constructed language that communicates instructions to a computer).  JavaScript is a scripting language (this means it is a programming language that supports scripts, which can interpret and automate the execution of tasks). Developed by Brendan Eich, while working for Netscape Communications Corporation, this program code was designed to appeal to nonprofessional programmers.  JavaScript runs inside an Internet browser. JavaScript is not a stand-alone programming language.  JavaScript is designed to embed information  in a web browser.  JavaScript includes the following features: dynamic typing and has first-class functions.  

Is Java the same as JavaScript?

Though both Java and JavaScript are both programming languages they are also immensely different.  Java is a very complex programming language while JavaScript is an easy-to-use version that is great for those beginners learning about program languages.  JavaScript and Java have the same expression syntax, naming conventions, and basic control-flow constructs.  JavaScript was previously named LiveScript; the name change has caused much confusion between Java and JavaScript.  Java is a programming language that can be used as a ‘stand-alone’ whereas JavaScript must rely on the environment it is operating in.

How Does JavaScript Work?

JavaScript works by placing code within a Web page.  When a browser loads the page, the built in interpreter reads the JavaScript code it finds within the page and runs it.

What is JavaScript Used For?

JavaScript is mostly used to allow ‘client-side’ scripts to interact with the user.  It is also used to control a Web browser, alter content that is displayed in a document, game development, as well as desktop and mobile applications.  Real life applications where this can be used (outside of web pages) include PDF documents, site-specific browsers, and desktop widgets.

What are the Various Data types used in JavaScript?

Number: Numbers can be written with or without decimals and may use scientific (exponential) notation if they are large enough. Does not allow the use of non-numbers such as NaN.
String: Strings are written with quotes.  Either double or single. 
Boolean: Can only have two values: true or false.
Array: Arrays use square brackets ‘[]’ while separating elements with comas.
Object: Objects are written with curly brackets ‘{}’. Comas separate each pair while the colon ‘:’ separates the name from its value.
Null: An empty value.  Variables can be emptied by setting the value to null.
Undefined: The value of a variable with no value.

Author Bio: Right brain, meet left brain. We're the whole brain. Agency Fusion in Salt Lake City, Utah is a web development and design firm with both creative and technological expertise. Learn more about Agency Fusion.

Tuesday, July 29, 2014

JAXB marshalling and unmarshalling of Hashtable

It is not possible to Marshall or Unmarshall a Hashtable directly however you can achieve it using a Holder class (wrapper class). With the help of Holder class you can get the default XML format as shown below -

Default XML format -
<mapHolder>
    <htmlColorCode>
        <entry>
            <key>blue</key>
            <value>0000FF</value>
        </entry>
        <entry>
            <key>green</key>
            <value>00FF00</value>
        </entry>
        <entry>
            <key>red</key>
            <value>FF0000</value>
        </entry>
    </htmlColorCode>
</mapHolder>

However if you need to customize the XML format like the one shown below you may need to use XmlAdapter.

<colorMap>
    <colors>
        <item code="0000FF" color="blue"/>
        <item code="00FF00" color="green"/>
        <item code="FF0000" color="red"/>
    </colors>
</colorMap>

Method 1 - Using Wrapper class

Output -
--Marshalling--
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mapHolder>
    <htmlColorCode>
        <entry>
            <key>blue</key>
            <value>0000FF</value>
        </entry>
        <entry>
            <key>green</key>
            <value>00FF00</value>
        </entry>
        <entry>
            <key>red</key>
            <value>FF0000</value>
        </entry>
    </htmlColorCode>
</mapHolder>

--Unmarshalling--
{blue=0000FF, green=00FF00, red=FF0000}


Java - JAX-WS Jar files for SOAP web services

If you are writing a SOAP based webservice you may need to add below JAR files in your applications classpath

Error: java.lang.ClassNotFoundException: com.sun.xml.ws.transport.http.servlet.WSServletContextListener
Add: jaxws-rt.jar

Error: java.lang.NoClassDefFoundError: com/sun/istack/localization/Localizable
Add: jaxb-core.jar

Error: java.lang.NoClassDefFoundError: com/sun/xml/stream/buffer/XMLStreamBuffer
Add: streambuffer.jar

Error: java.lang.NoClassDefFoundError: com/sun/xml/bind/api/JAXBRIContext
Add: jaxb-impl.jar

Error: java.lang.NoClassDefFoundError: com/sun/xml/ws/policy/PolicyException
Add: policy.jar

Error: java.lang.NoClassDefFoundError: org/jvnet/staxex/XMLStreamReaderEx
Add: stax-ex.jar

Error: java.lang.NoClassDefFoundError: org/glassfish/gmbal/ManagedObjectManager
Add: gmbal-api-only.jar

Error: java.lang.NoClassDefFoundError: org/glassfish/external/amx/AMXGlassfish
Add: management-api.jar

Error: java.lang.NoClassDefFoundError: org/glassfish/ha/store/api/BackingStoreException
Add: ha-api.jar

You can download latest version of JAR file (E.g. jaxws-ri-2.2.8.zip ) from https://jax-ws.java.net/2.2.8/. For more information please see the link https://jax-ws.java.net/.