IT Certificaions

Google Cloud Platform

Wednesday, October 31, 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;
   
    }

Tuesday, October 30, 2012

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 extracted jce directory (local_policy.jar and US_export_policy.jar) to $JAVA_HOME/jre/lib/security.

For  JDK1.6 visit , download these jars and see the installation instructions.

Or
Just you can copy and paste these jars into jdk1.6/jars/lib/security and rebuild your application.

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.println("blowfish encryption...firstEncrypted::"+new String(firstEncrypt));
   
   byte[] encodedBytes = Base64.encodeBase64(firstEncrypt);
   System.out.println("Base64 decoded encryption: encodedBytes::"+new String(encodedBytes));
   
   System.out.println("Double decryption starts now..................");
   byte[] decodedBytes = Base64.decodeBase64(encodedBytes);
   System.out.println("Base64 decryptions........decodedBytes:: " +new String(decodedBytes));
   
   cipher = Cipher.getInstance("Blowfish");
   cipher.init(Cipher.DECRYPT_MODE, secretkey);   
   byte[] decryptedFinal = cipher.doFinal(decodedBytes);
   System.out.println("Finally...blowfish decryption....decryptedFinal::"+new String(decryptedFinal));
 
   System.exit(0);
 }
 
}

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 message
    byte[] encrypted = cipher.doFinal(inputText.getBytes());

   // re-initialise the cipher to be in decrypt mode
   cipher.init(Cipher.DECRYPT_MODE, secretkey);

   // decrypt message
   byte[] decrypted = cipher.doFinal(encrypted);

   // and display the results
   System.out.println("encrypted text: " + new String(encrypted) + "\n" + "decrypted text: " + new String(decrypted));

  
 }
 
}
--------------------------------------------------------------------

Blowfish Encryption using existing secret key

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 {

String keyStr="CEM8hHSuL4nuXDydDn8Ey5jd"; // Let's assume you have this key

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

    // create a secret key
   SecretKeySpec secretkey = new SecretKeySpec(keyStr.getBytes(), "Blowfish");


    // 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 message
    byte[] encrypted = cipher.doFinal(inputText.getBytes());

    // re-initialise the cipher to be in decrypt mode
   cipher.init(Cipher.DECRYPT_MODE, secretkey);

    // decrypt message
   byte[] decrypted = cipher.doFinal(encrypted);

    // and display the results
   System.out.println("encrypted text: " + new String(encrypted) + "\n" + "decrypted text: " + new String(decrypted));

  
  }
 
}

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));
 }
}


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

Friday, October 5, 2012

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 rotate an image. The following command takes an image named howtogeek.jpg, rotates it by 90 degrees and saves the rotated image as howtogeek-rotated.jpg:

 convert a.jpg -rotate 90 b.jpg


 ------------------------------------------------------------------------------
If you failed to install it like below...
...
...

  Unable to fetch some archives, maybe run apt-get update or try with --fix-missing
Then go for this...

sudo apt-get update

sudo apt-get install imagemagick --fix-missing 

For more Info please go: https://help.ubuntu.com/community/ImageMagick