Posts

Showing posts from October, 2012

Calculate Time difference in Java

public static long checkTimeDifference(String time1,String time2){

 //time1 and time 2 must be in HH:mm:ss format

SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date date1;
Date date2;
long difference=0;
try {
date1 = format.parse(time1);
date2 = format.parse(time2);
difference = date2.getTime() - date1.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
return difference;

    }

java.security.InvalidKeyException: Illegal key size or default parameters

java.security.InvalidKeyException: Illegal key size or default parameters
Caused by: java.security.InvalidKeyException: Illegal key size or default parameters
    at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
    at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
    at javax.crypto.Cipher.a(DashoA13*..) ~[na:1.6]
    at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]
    at javax.crypto.Cipher.init(DashoA13*..) ~[na:1.6]

--------------------------------------------------------------------------------------

There are key size restrictions with the default crypto files local_policy.jar and US_export_policy.jar comes with JDK – which limits it to 128.

If the security policy you are using has a key size larger than this limit– then the above exception is thrown.

Please follow these instructions...
You need to patch your JDK with Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files.

For JDK1.5 visit , download the crypto files and copy the two jar files from the extracte…

Double encryption decryption (Blowfish+Base64) in java

Double Encryption and decryption using Blowfish and Base64

--------------------------------------------------------------

import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class BlowfishCipher {

 public static void main(String[] args) throws Exception {

   KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");

    SecretKey secretkey = keygenerator.generateKey();


   Cipher cipher = Cipher.getInstance("Blowfish");

   // get the text to encrypt
   String inputText = "User_ID=Test12&User_Timestamp=20121030040002";

   System.out.println("Double Encryption..................for :"+inputText);
   cipher.init(Cipher.ENCRYPT_MODE, secretkey);
   byte[] firstEncrypt = cipher.doFinal(inputText.getBytes());
   System.out.print…

Blowfish encryption decryption in Java

Blowfish Encryption Decryption using KeyGenerator

----------------------------------------------

import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class BlowfishCipherExample {

 public static void main(String[] args) throws Exception {

   // create a key generator based upon the Blowfish cipher
   KeyGenerator keygenerator = KeyGenerator.getInstance("Blowfish");

   // create a secret key
   SecretKey secretkey = keygenerator.generateKey();


   // create a cipher based upon Blowfish
   Cipher cipher = Cipher.getInstance("Blowfish");

   // initialise cipher to with secret key
     cipher.init(Cipher.ENCRYPT_MODE, secretkey);

   // get the text to encrypt
   String inputText = "User_ID=Test12&User_Timestamp=20121030040002";


   // encrypt me…

Base 64 encryption decryption in java using apache codec

First download jar from apache.. commons-codec-1.7.jar and add this to your lib...
-----------------------------------------------------------------------------------------



import org.apache.commons.codec.binary.Base64;

public class Base64TestUsingApacheCodec {
  public static void main(String[] args) {
    String testStr="Hello ! test this";
    byte[] encodedBytes = Base64.encodeBase64(testStr.getBytes());
    System.out.println("encodedBytes " + new String(encodedBytes));
    byte[] decodedBytes = Base64.decodeBase64(encodedBytes);
    System.out.println("decodedBytes " + new String(decodedBytes));
 }
}


================================================================

Compress images on Linux (ubuntu)

Image resizing/compression for linux

 ImageMagick isn’t included in the default installations of Ubuntu and many other Linux distributions. To install it on Ubuntu, use the following command:

sudo apt-get install imagemagick

-------------------------------------------------------------
Converting Between Formats
convert a.png b.jpg


You can also specify a compression level for JPEG images:

convert a.png -quality 95 b.jpg
---------------------------------------------------------------
Resizing Images

The convert command can also quickly resize an image.
Here you can asks ImageMagick to resize an image to 100 pixels in width and 50 pixels in height:

convert a.jpg -resize 100×50 b.jpg

------------------------------------------------------------
  Reduce size of  all images with same name (No name change after reducing)

 for fileName in *.jpg; do convert $fileName -quality 50 $fileName; done
 -------------------------------------------------------------


 Rotating an Image

ImageMagick can quickly…