Friday, December 9, 2011

AES Encryption

The program is a total solution for all who needs to implements AES Encryption

The Code:
/************************************************************************/
AESEncrypter.java



import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;

import javax.crypto.Cipher;
import javax.crypto.spec.*;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;

import java.security.spec.AlgorithmParameterSpec;

public class AESEncrypter
{
Cipher ecipher;
Cipher dcipher;

public AESEncrypter(SecretKey key)
{
// Create an 8-byte initialization vector
byte[] iv = new byte[]
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
};

AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
try
{
ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

// CBC requires an initialization vector
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
}
catch (Exception e)
{
e.printStackTrace();
}
}

// Buffer used to transport the bytes from one stream to another
byte[] buf = new byte[1024];

public void encrypt(InputStream in, OutputStream out)
{
try
{
// Bytes written to out will be encrypted
out = new CipherOutputStream(out, ecipher);

// Read in the cleartext bytes and write to out to encrypt
int numRead = 0;
while ((numRead = in.read(buf)) >= 0)
{
out.write(buf, 0, numRead);
}
out.close();
}
catch (java.io.IOException e)
{
}
}

public void decrypt(InputStream in, OutputStream out)
{
try
{
// Bytes read from in will be decrypted
in = new CipherInputStream(in, dcipher);

// Read in the decrypted bytes and write the cleartext to out
int numRead = 0;
while ((numRead = in.read(buf)) >= 0)
{
out.write(buf, 0, numRead);
}
out.close();
}
catch (java.io.IOException e)
{
}
}

public static void main(String args[])
{
try
{
// Generate a temporary key. In practice, you would save this key.
// See also e464 Encrypting with DES Using a Pass Phrase.

KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
SecretKey key =(SecretKey)new SecretKeySpec(new byte[]{'0','1','2','3','4','5','6','7','8','9','0','1','2','3','4','5'},"AES");//kgen.generateKey();

AESEncrypter encrypter = new AESEncrypter(key);
encrypter.encrypt(new FileInputStream("2.pdf"),new FileOutputStream("1.txt"));


encrypter.decrypt(new FileInputStream("1.txt"),new FileOutputStream("100.pdf"));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}

/**********************************************************************/

Accessing Accelerometer In Android

This program can be used to measure the level of x,y,z acc.

The manifest file
<uses-permission android:name="android.permission.LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

The layout file(main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/accText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>

The Activity Class(AccelerometerTest.java)

package com.sarath.accelerometertest;

import android.app.Activity;
import android.os.Bundle;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.TextView;

public class AccelerometerTest extends Activity {

private TextView accText;
private SensorManager myManager;
private List sensors;
private Sensor accSensor;
private float oldX, oldY, oldZ = 0f;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

accText = (TextView)findViewById(R.id.accText);

// Set Sensor + Manager
myManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
sensors = myManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
if(sensors.size() > 0)
{
accSensor = sensors.get(0);
}
}

private void updateTV(float x, float y, float z)
{
/*float thisX = x - oldX * 10;
float thisY = y - oldY * 10;
float thisZ = z - oldZ * 10;
*/
float thisX = (x - oldX) * 10;
float thisY = (y - oldY) * 10;
float thisZ = (z - oldZ) * 10;
accText.setText("x: " + Math.round(thisX) + ";\n y:" + Math.round(thisY) + ";\n z: " + Math.round(thisZ));

oldX = x;
oldY = y;
oldZ = z;
}

private final SensorEventListener mySensorListener = new SensorEventListener()
{
public void onSensorChanged(SensorEvent event)
{
updateTV(event.values[0],
event.values[1],
event.values[2]);
}

public void onAccuracyChanged(Sensor sensor, int accuracy) {}
};

@Override
protected void onResume()
{
super.onResume();
myManager.registerListener(mySensorListener, accSensor, SensorManager.SENSOR_DELAY_GAME);
}

@Override
protected void onStop()
{
myManager.unregisterListener(mySensorListener);
super.onStop();
}
}

Manipulating Android Wallpaper

I searched on how you could manipulate the current wallpaper of the android mobile and here it is the code:

AndroidManifest.xml

<uses-permission name="android.permission.SET_WALLPAPER">

main.xml

<?xml version="1.0" encoding="utf-8"?>
<framelayout android="http://schemas.android.com/apk/res/android" layout_width="wrap_content" layout_height="wrap_content">
<imageview layout_width="wrap_content" layout_height="wrap_content" id="@+id/imageview">
<linearlayout orientation="horizontal" layout_width="wrap_content" layout_height="fill_parent">
<button id="@+id/randomize" layout_width="wrap_content" layout_height="wrap_content" text="@string/randomize" layout_gravity="bottom">
</button><button id="@+id/setwallpaper" layout_width="wrap_content" layout_height="wrap_content" text="@string/set_wallpaper" layout_gravity="bottom">



SetWallpaperActivity.java
package com.kites.sarath.wallpaperdemo;

import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
import android.app.WallpaperManager;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class SetWallpaperActivity extends Activity {
final static private int[] mColors =
{Color.BLUE, Color.GREEN, Color.RED, Color.LTGRAY, Color.MAGENTA, Color.CYAN,
Color.YELLOW, Color.WHITE};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
final ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setDrawingCacheEnabled(true);
imageView.setImageDrawable(wallpaperDrawable);

Button randomize = (Button) findViewById(R.id.randomize);
randomize.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
int mColor = (int) Math.floor(Math.random() * mColors.length);
wallpaperDrawable.setColorFilter(mColors[mColor], PorterDuff.Mode.MULTIPLY);
imageView.setImageDrawable(wallpaperDrawable);
imageView.invalidate();
}
});

Button setWallpaper = (Button) findViewById(R.id.setwallpaper);
setWallpaper.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
try {

wallpaperManager.setBitmap(imageView.getDrawingCache());
finish();
} catch (IOException e) {
e.printStackTrace();
}
}
});

}
}