Tuesday 7 February 2012

Updating the Cookie for specific time interval


If you want to update the cookies after some time interval using the time then call the below function.
Timer should run with the interval of less than 11 minutes.

updateCookieValue  :  function () {
    var cookies = document.cookie.split(";");
if (cookies.length > 0) {
var nTime = new Date();
//add 11 mins to current time so that the recursive call to update cookie
             (with interval 10 mins)
//gets executed before the cookie gets expired ( 11 * 1000 * 60)
             // time added in millisecond
nTime.setTime(nTime.getTime()+660000);
for (var i = 0; i < cookies.length; i++){
var name = cookies[i].split("=")[0];
name = name.trim();
var value = cookies[i].split("=")[1];
if((name == " cookie_0") || (name == " cookie_1")){
document.cookie = name + "=" +escape( value ) +";expires="
                      + nTime.toGMTString()+";path=/;domain=";
}
        }
}
}

Sunday 5 February 2012

Encryption and Decryption Technique


import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class EncryptDecryptUtil {

    private static final String UNICODE_FORMAT = "UTF8";
    public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
    private KeySpec keySpec;
    private SecretKeyFactory secretKeyFactory;
    private Cipher cipher;
    byte[] keyAsBytes;
    private String encryptionKey;
    private String encryptionScheme;
    private SecretKey secretKey;

    public EncryptDecryptUtil() throws Exception
    {
        encryptionKey = "EncryptionKeyForEncryption";
        encryptionScheme = DESEDE_ENCRYPTION_SCHEME;
        keyAsBytes = encryptionKey.getBytes(UNICODE_FORMAT);
        keySpec = new DESedeKeySpec(keyAsBytes);
        secretKeyFactory = SecretKeyFactory.getInstance(encryptionScheme);
        cipher = Cipher.getInstance(encryptionScheme);
        secretKey = secretKeyFactory.generateSecret(keySpec);
    }

    public String encrypt(String unencryptedString) {
        String encryptedString = null;
        try {
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] plainText = unencryptedString.getBytes(UNICODE_FORMAT);
            byte[] encryptedText = cipher.doFinal(plainText);
            BASE64Encoder base64encoder = new BASE64Encoder();
            encryptedString = base64encoder.encode(encryptedText);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptedString;
    }
 
    public String decrypt(String encryptedString) {
        String decryptedText=null;
        try {
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            BASE64Decoder base64decoder = new BASE64Decoder();
            byte[] encryptedText = base64decoder.decodeBuffer(encryptedString);
            byte[] plainText = cipher.doFinal(encryptedText);
            decryptedText= bytes2String(plainText);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return decryptedText;
    }
 
    private static String bytes2String(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            stringBuffer.append((char) bytes[i]);
        }
        return stringBuffer.toString();
    }
 
     /**
     * Testing The Encryption And Decryption Technique
     */
    public static void main(String args []) throws Exception
    {
    EncryptDecryptUtil myEncryptor = new EncryptDecryptUtil();
    String stringToEncrypt= "theherald";
        String encrypted=myEncryptor.encrypt(stringToEncrypt);
        String decrypted=myEncryptor.decrypt(encrypted);

        System.out.println("The String To be Encrypted : "+stringToEncrypt);
        System.out.println("Encrypted Value of the String : " + encrypted);
        System.out.println("Decrypted Value of the String : "+decrypted);

    }
}  


The output is ::::::

The String To be Encrypted : theherald
Encrypted Value of the String :b6WaWLvUO7XznWLKxDzGXw==
Decrypted Value of the String :theherald