Friday, 5 August 2011

Create password protected file in android

package com.cz.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Random;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import android.util.Log;


public class FileEncryptorDecryptor
{

private static String filename;
private static FileInputStream inFile;
private static FileOutputStream outFile;


public void encrypt(FileInputStream filename, FileOutputStream outFile,String password) throws Exception
{
inFile = filename;

// Use PBEKeySpec to create a key based on a password.
// The password is passed as a character array

PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
SecretKeyFactory keyFactory =
SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey passwordKey = keyFactory.generateSecret(keySpec);

// PBE = hashing + symmetric encryption. A 64 bit random
// number (the salt) is added to the password and hashed
// using a Message Digest Algorithm (MD5 in this example.).
// The number of times the password is hashed is determined
// by the interation count. Adding a random number and
// hashing multiple times enlarges the key space.

byte[] salt = new byte[8];
Random rnd = new Random();
rnd.nextBytes(salt);
int iterations = 100;

//Create the parameter spec for this salt and interation count

PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterations);

// Create the cipher and initialize it for encryption.

Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(Cipher.ENCRYPT_MODE, passwordKey, parameterSpec);

// Need to write the salt to the (encrypted) file. The
// salt is needed when reconstructing the key for decryption.

outFile.write(salt);

// Read the file and encrypt its bytes.

byte[] input = new byte[64];
int bytesRead;
while ((bytesRead = inFile.read(input)) != -1)
{
byte[] output = cipher.update(input, 0, bytesRead);
if (output != null) outFile.write(output);
}

byte[] output = cipher.doFinal();
if (output != null) outFile.write(output);

inFile.close();
outFile.flush();
outFile.close();

}

public void decrypt(FileInputStream filename, FileOutputStream outFile,String password) throws Exception
{
inFile = filename;

PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
SecretKeyFactory keyFactory =
SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey passwordKey = keyFactory.generateSecret(keySpec);

// Read in the previouly stored salt and set the iteration count.

byte[] salt = new byte[8];
inFile.read(salt);
int iterations = 100;

PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterations);

// Create the cipher and initialize it for decryption.

Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(Cipher.DECRYPT_MODE, passwordKey, parameterSpec);


byte[] input = new byte[64];
int bytesRead;
while ((bytesRead = inFile.read(input)) != -1)
{
byte[] output = cipher.update(input, 0, bytesRead);
if (output != null)
outFile.write(output);
}

byte[] output = cipher.doFinal();
if (output != null)
outFile.write(output);

inFile.close();
outFile.flush();
outFile.close();
}

}

Tuesday, 19 July 2011

android load on startup service

Most of the time user needs to start a application on boot up, which needs to be auto start. I have searched a lot but didn;t find a good tutorial, but somehow I have managed it and it is working in my My Touch phone. Step to follow

1. Manifest FIle
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.android.events">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.VIBRATE" />
<application>
<receiver android:name=".MyBroadcastreceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".MyService" />
</application>
</manifest>


2. Receiver

package com.android.events;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyBroadcastreceiver extends BroadcastReceiver {
public static final String TAG = "Rajesh";

@Override
public void onReceive(Context context, Intent intent) {

if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, MyService.class);
context.startService(pushIntent);
}
}

}

3. MyService.class could be anything which extends Service

Hope this will help you :-)

Wednesday, 13 July 2011

Check Google PR / Page Ranking For Multiple Domain Name

If you want to use a drop domain catcher script which could register only those domain which have Google Page Rank more than 3, then you might need a script like this example Google Page Rank Checker For Multiple Domain . Please upload only a text file, having domian names, followed by new line.

Sunday, 10 July 2011

Android Chart Library

After googling I have found a good chart engine for android. Really good library to go ahead. I am thinking to donate something for him. First let me create a single project with this library and see if there is any flaw. But at a glance they did well. Please follow this link Android Chart Library

Tuesday, 5 July 2011

Create a xml based family tree using Actionscript3

I haven't got a good flow chart creator tools in actionscript3, so I have started working on this myself and thought to share the code with you guys. So far I have been able to got some time to create a prototype of this and obviously a dirty code. Also here is a screen capture of the same. If you need the source code please drop me a mail at rajeshghosh1978@gmail.com.

How to draw an elastic line having circle at both end

If you are going to create a flow diagram or a white board application, you might need a line which acts like an elastic string, where as user can drag both end. So I have created for one of my project and thought, it might help someone to save some time. Here are the classes

elasticLine.as

package com.collectivezen{
import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.MovieClip;
import com.collectivezen.RoundRectangle;

import flash.filters.*;



public class elasticLine extends MovieClip {
private var rrX:Number;
private var rrY:Number;
private var rrWidth:Number;
private var rrHeight:Number;
private var bgColor:uint;
private var borderColor:uint;
private var borderSize:Number;
private var child:MovieClip = new MovieClip();
private var src:RoundRectangle;
private var dest:RoundRectangle;
private var _this:elasticLine;
private var lineDrawing:MovieClip = new MovieClip();

public function elasticLine(rrX:Number=0,rrY:Number=0,rrWidth:Number=10,rrHeight:Number=10,bgColor:uint=0x000000) {
this.rrX=rrX;
this.rrY=rrY;
this.rrWidth=rrWidth;
this.rrHeight=rrHeight;
this.bgColor=bgColor;
lineDrawing = new MovieClip();
lineDrawing.graphics.lineStyle(1, 0x000000, 1 );
lineDrawing.graphics.beginFill( bgColor, 1 );
lineDrawing.graphics.moveTo(rrX,rrY);
lineDrawing.graphics.lineTo(rrWidth,rrHeight);

///Glow Filter
var glow:GlowFilter = new GlowFilter();
glow.color = bgColor;
glow.alpha = 1;
glow.blurX = 8;
glow.blurY = 8;

var dropShadow:DropShadowFilter = new DropShadowFilter();
dropShadow.color = 0x000000;
dropShadow.blurX = 8;
dropShadow.blurY = 8;
dropShadow.angle = 0;
dropShadow.alpha = 0.5;
dropShadow.distance = 10;

var filtersArray:Array = new Array(glow, dropShadow);
lineDrawing.filters = filtersArray;
addChild(lineDrawing);
}
public function moveLine(src:RoundRectangle,dest:RoundRectangle) {
lineDrawing.graphics.clear();
lineDrawing.graphics.lineStyle(1, 0x000000, 1 );
lineDrawing.graphics.beginFill( this.bgColor, 1 );
lineDrawing.graphics.moveTo(src._CurCenX,src._CurCenY);
lineDrawing.graphics.lineTo(dest._CurCenX,dest._CurCenY);

///Glow Filter
var glow:GlowFilter = new GlowFilter();
glow.color = this.bgColor;
glow.alpha = 1;
glow.blurX = 8;
glow.blurY = 8;

var dropShadow:DropShadowFilter = new DropShadowFilter();
dropShadow.color = 0x000000;
dropShadow.blurX = 8;
dropShadow.blurY = 8;
dropShadow.angle = 0;
dropShadow.alpha = 0.5;
dropShadow.distance = 10;

var filtersArray:Array = new Array(glow, dropShadow);
lineDrawing.filters = filtersArray;
}
public function drawLine(_X:uint,_Y:uint,_eX:uint,_eY:uint,_col:uint) {
lineDrawing.graphics.clear();
lineDrawing.graphics.lineStyle(1, 0x000000, 1 );
lineDrawing.graphics.beginFill( _col, 1 );
lineDrawing.graphics.moveTo(_X,_Y);
lineDrawing.graphics.lineTo(_eX,_eY);

///Glow Filter
var glow:GlowFilter = new GlowFilter();
glow.color = _col;
glow.alpha = 1;
glow.blurX = 8;
glow.blurY = 8;

var dropShadow:DropShadowFilter = new DropShadowFilter();
dropShadow.color = 0x000000;
dropShadow.blurX = 8;
dropShadow.blurY = 8;
dropShadow.angle = 0;
dropShadow.alpha = 0.5;
dropShadow.distance = 10;

var filtersArray:Array = new Array(glow, dropShadow);
lineDrawing.filters = filtersArray;
}

}
}

See my different post to get com.collectivezen.RoundRectangle