21 October 2014

Java RSA Encryption and Decryption Example

This is the sample code

package sajith.foru.rsaencryption;

import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

public class RsaEncryption {

/**
 * Generate RSA private and public key and Encrypt/Decrypt
 *
 * @param args
 * @throws NoSuchPaddingException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 * @throws BadPaddingException
 * @throws IllegalBlockSizeException
 */
public static void main(String[] args) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException,
IllegalBlockSizeException, BadPaddingException {
/**
 * Generate KeyPair
 */
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048); // key size can be change to 512,1024
KeyPair kp = keyGen.genKeyPair();
/**
 * Generate private and public key
 */
PublicKey publicKey = kp.getPublic();
PrivateKey privateKey = kp.getPrivate();
/**
 * String want to be encrypted
 */
String text = "Your text Message";
Cipher cipher = Cipher.getInstance("RSA");
/**
 * Enable Encrypt mode
 */
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedData = cipher.doFinal(text.getBytes());
String encryptedText = new String(encryptedData);
System.out.println("Text Encrypted : " + encryptedText);
/**
 * Enable Decrypt mode
 */
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptDataValue = cipher.doFinal(encryptedData); // decrypted
// the
// encrypted
// data
String decryptedText = new String(decryptDataValue);
System.out.println("Text Decryted : " + decryptedText);

}



}

Replace sentence or word in file linux

For replace world in file

sed -i 's/OLd_text/new_text/g' fileName

ex sed -i 's/my_world/hello_world/g' hello.txt

output :-


For replace Sentence

14 October 2014

Android TimePicker Dialog

Android TimePicker Dialog Example

If you want to add TimePicker dialog when your OnClick events you can use this code line.

            Calendar mcurrentTime = Calendar.getInstance();
            int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
            int minute = mcurrentTime.get(Calendar.MINUTE);
            TimePickerDialog mTimePicker;
            mTimePicker = new TimePickerDialog(AddReminder.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
                    eReminderTime.setText( selectedHour + ":" + selectedMinute);
                }
            }, hour, minute, true);//Yes 24 hour time
            mTimePicker.setTitle("Select Time");
            mTimePicker.show();

24 September 2014

Encode large size image to Base64 String in android

Encode large size  image to Base64 String in android



In here i have described how to encode large size of image to base64 String.For example when you want to upload images to server using android application most of the times image size is (2MB-5MB).So Using normal byte inputstream it is difficult to upload image.It gives out of memory error when encoding image.So that is why we encode image to String using base64 and decode string to Image in server side.

Encode Image


public String BitMapToString(Bitmap bitmap){

        ByteArrayOutputStream baos=new  ByteArrayOutputStream();

        bitmap.compress(Bitmap.CompressFormat.JPEG,100, baos);

        byte [] b=baos.toByteArray();

        String temp=null;

        try{

            System.gc();

            temp= Base64.encodeToString(b, Base64.DEFAULT);

        }catch(Exception e){

            e.printStackTrace();

        }catch(OutOfMemoryError e){

            baos=new  ByteArrayOutputStream();

            bitmap.compress(Bitmap.CompressFormat.JPEG,50, baos);
            b=baos.toByteArray();

            temp=Base64.encodeToString(b, Base64.DEFAULT);

            Log.e("EWN", "Out of memory error catched");

        }

        return temp;

    }

Happy Coding.

28 August 2014

chart.js

In here show how to use chart.js, Chart js is open source chart platform for web based applications.So it is very easy to use chart.js in your web site.First you have to download (https://github.com/nnnick/Chart.js) chart.js and chart.min.js files.Then in your html file you can add this code for bar chart.

Bar chart





<script>
var barChartData = {
labels : ["Sunday","Mondey","Tuesday","Wednesday","Thursday","Friday","Saturday"],
datasets : [
{
fillColor : "rgba(220,220,220,0.5)",
strokeColor : "rgba(220,220,220,0.8)",
highlightFill: "rgba(220,220,220,0.75)",
highlightStroke: "rgba(220,220,220,1)",
data : [85,65,35,55,80,20,90]
},
{
fillColor : "rgba(151,187,205,0.5)",
strokeColor : "rgba(151,187,205,0.8)",
highlightFill : "rgba(151,187,205,0.75)",
highlightStroke : "rgba(151,187,205,1)",
data : [55,100,30,70,30,65,45]
}
]
}
window.onload = function(){
var ctx = document.getElementById("canvas").getContext("2d");
window.myBar = new Chart(ctx).Bar(barChartData, {
responsive : true
});
}
</script>


Then inside canvas you can draw the bar chart.


<div style="width: 50%">
   <canvas id="canvas" height="450" width="600"></canvas>
</div>
Inside the Script you can change colors of charts.

Pie chart



<script>

  var pieData = [

    {

     value: 300,

     color:"#F7464A",

     highlight: "#FF5A5E",

     label: "Food"

    },

    {

     value: 50,

     color: "#46BFBD",

     highlight: "#5AD3D1",

     label: "Transport"

    },

    {

     value: 100,

     color: "#FDB45C",

     highlight: "#FFC870",

     label: "Drinks"

    },

    {

     value: 40,

     color: "#949FB1",

     highlight: "#A8B3C5",

     label: "class"

    },

    {

     value: 120,

     color: "#4D5360",

     highlight: "#616774",

     label: "Others"

    }



   ];



   window.onload = function(){

    var ctx = document.getElementById("chart-area").getContext("2d");

    window.myPie = new Chart(ctx).Pie(pieData);

   };



 </script>

22 August 2014

Install juju in local lxc contanier

Install JUJU in ubuntu

In this thread i have described how to install juju in ubuntu 14.04.First You have to install juju in to your machine.
   
   sudo apt-add-repository ppa:juju/stable
   sudo apt-get update
And then install juju-core in to your machine.It might takes more times based on your internet connection.
sudo apt-get install juju-local
Then for the configuration 
juju generate-config
juju switch local
Then You need to bootstrap the local environment.So Then enter this command
juju bootstrap --show-log
This might be take more than 20 minutes it is based on your internet connection.After installing juju you can check the status of the juju environment.
juju status
Then for the Testing you can install juju GUI.This will help you to understand what charm is running and what are the relationships using graphical view.
juju deploy juju-gui
juju expose juju-gui

How to create juju charm

First you have to install juju and then you have install charm tools for juju.
sudo add-apt-repository ppa:juju/stable
sudo apt-get update;
sudo apt-get install charm-tools;