Skip to main content

Posts

Showing posts from May, 2012

Encrypt Decrypt data using AES in Java

AES stands for Advanced Encryption Standard. AES is an Symmetric Key Algorithm, that means key used for encrypting the data, same key will be used for decryption of the encrypted data. This algorithm supplants DES algorithm. This post shows how to use AES algorithm in Java to encrypt and decrypt data.
Encrypting Data-

import java.security.GeneralSecurityException; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; public class AESTest1 { public static void main(String[] args) throws GeneralSecurityException { KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(128); SecretKey key = keyGenerator.generateKey(); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); byte encryptedData[] = cipher.doFinal(args[0].getBytes()); } } In above example we have used KeyGeneratorclass, which generates key for encrypting data, as we are using AES which is …

Type Erasure in Java

While reading some posts online I came across the following post -
List<integer> list = new ArrayList<integer>(); List&lt;integer&gt; list = new ArrayList&lt;integer&gt;(); list.add(1); list.add(2); List list1 = new ArrayList(); list1.add(&quot;Some String&quot;); list.addAll(list1);

This code compiles and executes as well, this works because of Type Erasure concept in Java. I knew about Type Erasure but while reading more about it on internet I found some posts which were questioning on Type Erasure concept.
Following code makes more confusing about Type Erasure concept, if compiler removes all the information about type parameters and type arguments then how does following code works??

class A<T>{ } class B extends A<String>{ public B() { System.out.println(((ParameterizedType)getClass(). getGenericSuperclass()).getActualTypeArguments()[0]); } } public static void main(String args[]){ new B(); }

Client Side MVC

The current project on which I am working in my company, I have been introduced to a new concept(at least its new for me :) ) Client side MVC by one of my colleague and its actually very good technique.

Why to have client side MVC-
1. It allows you to manage your data on client side.
2. It allows you to customize your views with Modularized JavaScript code.
3. It allows you to create complex views.


In our application we are using Backbone.js to design Client side MVC. Even though I am not an expert in Backbone.js but I liked it, because of the features which are provided by it and how easy it makes to create layouts, view and loads of other things. Backbone.js provides Model, View, Collection and Router(Controller) to create a MVC model.

Backbone has a main dependency - Underscore.js, which is a JavaScript library(Of course!!).
Creating models using Backbone.js is very easy-

var Book = Backbone.Model.extend({
     defaults : {
         name : '',
         author : ''
     …