Thursday, January 3, 2008

CoreJava

Encription and decription using Java

import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

public final class EncriptDecript {
/** Encrypt algorithm */
public final static String DES = "DES";
/** Encrytp encoding */
public final static String UTF8 = "UTF8";
/**
* The key format: password-based encrypt with MD5 and DES.
*
* @value
* @see javax.crypto.spec.PBEKeySpec
*/
public static final String PBE_WITH_MD5_AND_DES = "PBEWithMD5AndTripleDES";

private static byte[] salt = { (byte) 0xA9, (byte) 0x9B, (byte) 0xC8,
(byte) 0x32, (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03 };
/** Number of interaction used to prepare the parameter to the ciphers. */
private static final int INTERACTION = 19;
/**
*


    *
  • Create the key

  • *
  • Prepare the parameter to the ciphers

  • *
  • Create the ciphers

  • *
  • Encode the string into bytes using utf-8

  • *
  • Encrypt

  • *
  • Encode bytes to base64 to get a string

  • *

*
* @param secret
* The password to be decrypted.
* @return a plain password.
* @throws Exception
* a generic exception caused during the decrypt process.
*/
public static String encrypt(String plain,String passphrase) throws Exception {

KeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), salt, INTERACTION,1);
SecretKey key = SecretKeyFactory.getInstance(PBE_WITH_MD5_AND_DES)
.generateSecret(keySpec);
Cipher ecipher = Cipher.getInstance(key.getAlgorithm());
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt,
INTERACTION);
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
byte[] utf8 = plain.getBytes(UTF8);
byte[] enc = ecipher.doFinal(utf8);
return new sun.misc.BASE64Encoder().encode(enc);
}

/* @param secret
* The password to be decrypted.
* @return a plain password.
* @throws Exception
* a generic exception caused during the decrypt process.
*/
public static String decrypt(String secret,String passphrase) throws Exception {
KeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), salt,
INTERACTION,1);
SecretKey key = SecretKeyFactory.getInstance(PBE_WITH_MD5_AND_DES)
.generateSecret(keySpec);
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(secret);
Cipher dcipher = Cipher.getInstance(key.getAlgorithm());
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt,
INTERACTION);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
byte[] utf8 = dcipher.doFinal(dec);
return new String(utf8, UTF8);
}

}

Example:To usse EncriptDecript

publilc class TestEncription{
public static final String SALT_PASSWORD = "wDC2mArpqswol8v6ba4leQ==";
public static void main(String [] args)
{
String password="abcd";
String encriptedPassword = ECSPassword.encrypt(password,SALT_PASSWORD);
String decriptedPassword = ECSPassword.decrypt(encriptedPassword ,SALT_PASSWORD);
}

Encription and Decription Using Javascript:


/*
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
* Digest Algorithm, as defined in RFC 1321.
* Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for more info.
*/
/*
* Configurable variables. You may need to tweak these to be compatible with
* the server-side, but the defaults work in most cases.
*/
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
var key = "example" /* key used for encryption */
/*
* These are the functions you'll usually want to call
* They take string arguments and return either hex or base-64 encoded strings
*/
function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));}
function hex_hmac_md5(data) { return binl2hex(core_hmac_md5(key, data)); }
function b64_hmac_md5(data) { return binl2b64(core_hmac_md5(key, data)); }
function str_hmac_md5(data) { return binl2str(core_hmac_md5(key, data)); }
/*
* Perform a simple self-test to see if the VM is working
*/
function md5_vm_test()
{
return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
}
/*
* Calculate the MD5 of an array of little-endian words, and a bit length
*/
function core_md5(x, len)
{
/* append padding */
x[len >> 5] = 0x80 << ((len) % 32); x[(((len + 64) >>> 9) << a =" 1732584193;" b =" -271733879;" c =" -1732584194;" d =" 271733878;" i =" 0;" olda =" a;" oldb =" b;" oldc =" c;" oldd =" d;" a =" md5_ff(a," d =" md5_ff(d," c =" md5_ff(c," b =" md5_ff(b," a =" md5_ff(a," d =" md5_ff(d," c =" md5_ff(c," b =" md5_ff(b," a =" md5_ff(a," d =" md5_ff(d," c =" md5_ff(c," b =" md5_ff(b," a =" md5_ff(a," d =" md5_ff(d," c =" md5_ff(c," b =" md5_ff(b," a =" md5_gg(a," d =" md5_gg(d," c =" md5_gg(c," b =" md5_gg(b," a =" md5_gg(a," d =" md5_gg(d," c =" md5_gg(c," b =" md5_gg(b," a =" md5_gg(a," d =" md5_gg(d," c =" md5_gg(c," b =" md5_gg(b," a =" md5_gg(a," d =" md5_gg(d," c =" md5_gg(c," b =" md5_gg(b," a =" md5_hh(a," d =" md5_hh(d," c =" md5_hh(c," b =" md5_hh(b," a =" md5_hh(a," d =" md5_hh(d," c =" md5_hh(c," b =" md5_hh(b," a =" md5_hh(a," d =" md5_hh(d," c =" md5_hh(c," b =" md5_hh(b," a =" md5_hh(a," d =" md5_hh(d," c =" md5_hh(c," b =" md5_hh(b," a =" md5_ii(a," d =" md5_ii(d," c =" md5_ii(c," b =" md5_ii(b," a =" md5_ii(a," d =" md5_ii(d," c =" md5_ii(c," b =" md5_ii(b," a =" md5_ii(a," d =" md5_ii(d," c =" md5_ii(c," b =" md5_ii(b," a =" md5_ii(a," d =" md5_ii(d," c =" md5_ii(c," b =" md5_ii(b," a =" safe_add(a," b =" safe_add(b," c =" safe_add(c," d =" safe_add(d," bkey =" str2binl(key);"> 16) bkey = core_md5(bkey, key.length * chrsz);
var ipad = Array(16), opad = Array(16);
for(var i = 0; i < hash =" core_md5(ipad.concat(str2binl(data))," lsw =" (x" msw =" (x">> 16) + (y >> 16) + (lsw >> 16);
return (msw <<>>> (32 - cnt));
}
/*
* Convert a string to an array of little-endian words
* If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
*/
function str2binl(str)
{
var bin = Array();
var mask = (1 << i =" 0;">>5] = (str.charCodeAt(i / chrsz) & mask) << (i%32); return bin; } /* * Convert an array of little-endian words to a string */ function binl2str(bin) { var str = ""; var mask = (1 << i =" 0;">>5] >>> (i % 32)) & mask);
return str;
}
/*
* Convert an array of little-endian words to a hex string.
*/
function binl2hex(binarray)
{
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var str = "";
for(var i = 0; i <>>2] >> ((i%4)*8+4)) & 0xF) +
hex_tab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF);
}
return str;
}
/*
* Convert an array of little-endian words to a base-64 string
*/
function binl2b64(binarray)
{
var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var str = "";
for(var i = 0; i < triplet =" (((binarray[i">> 2] >> 8 * ( i %4)) & 0xFF) <<>> 2] >> 8 * ((i+1)%4)) & 0xFF) <<>> 2] >> 8 * ((i+2)%4)) & 0xFF);
for(var j = 0; j <> binarray.length * 32) str += b64pad;
else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
}
}
return str;
}

Example:

call this method in javascript: by passing the string to encript:

hex_hmac_md5(pwd);

No comments: