Friday, April 04, 2008

StringTokenizer separater issue with special charecters

public class Tokenizer {

public static void main(String[] args) {

String longStr = "33068|~|Disa+bled|~|False==34296|~|Value|~|Yes";

String separator = "+";

StringTokenizer token = new StringTokenizer(longStr, separator);

String[] strList = longStr.split(separator);

for(int i=0; i < strList.length ; ++i)

{

System.out.println(strList[i]);

}

}

Hi ,

The above program is throwing an exeception because I am using '+' as delimeter . if we put '\\+' then it is working fine.

Problem is the separator is configurable and I don't know which data will come into this. Some times it may be one character and sometimes it may be

String which contains these meta characters. In those case I can't identify those and put \\ right… tell me is there any method to do this.



Solution :

"\\Q"+separator+"\\E";

If we use \\Q and \\E it will remove all the metacharacters in the string and it will take as it is…

$ in replacement text of string.

If you want to replace a part of the string with other string which consists a character $ in it, with normal replace/replaceAll will give error. To resolve it see following code.

String str = "asdf$fgh";
String str2 = "hghf-j-kjfi";
//Matcher m
str = str.replaceAll("\\$", "\\\\\\$");
str = str2.replaceAll("-j-", str);
System.out.println(str);

Monday, July 24, 2006

Password Based Encryption With MD5 and DES algorithms


package com.pavan.text.encryption;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
*
* @author pgupta
*
*/
public class PasswordTest {
//Predefined key for encryption and decryption.
public static final String key = "PaVaNkUmArGuPtA";

public static void main(String[] args) {
PasswordTest pt = new PasswordTest();
String encryptedString = pt.Encrypt("pgupta");
System.out.println("encryptedString: "+encryptedString);

String decryptedString = pt.Decrypt(encryptedString);
System.out.println("decryptedString: "+decryptedString);
}

/**
* Encrypts the given string using Password Based Encryption With MD5 and DES algorithm.
* @param password
* @return
*/
public String Encrypt(String password){
PBEKeySpec pbeKeySpec;
PBEParameterSpec pbeParamSpec;
SecretKeyFactory keyFac;
// Salt
byte[] salt = {
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
};
// Iteration count
int count = 20;
try {
// Create PBE parameter set
pbeParamSpec = new PBEParameterSpec(salt, count);
pbeKeySpec = new PBEKeySpec(PasswordTest.key.toCharArray());
keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
// Create PBE Cipher
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
// Initialize PBE Cipher with key and parameters
pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
// Our cleartext
byte[] cleartext = password.getBytes();
// Encrypt the cleartext
byte[] encryptedPassword = pbeCipher.doFinal(cleartext);
String hash = (new BASE64Encoder()).encode(encryptedPassword);
return hash;

} catch (Exception e) {

}
return null;
}

/**
* Decrypts the given string using Password Based Decryption With MD5 and DES algorithm.
* @param encryptedPassword
* @return
*/
public String Decrypt(String encryptedPassword){
try {
PBEKeySpec pbeKeySpec;
PBEParameterSpec pbeParamSpec;
SecretKeyFactory keyFac;
// Salt
byte[] salt = {
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
};
// Iteration count
int count = 20;
// Create PBE parameter set
pbeParamSpec = new PBEParameterSpec(salt, count);
pbeKeySpec = new PBEKeySpec(PasswordTest.key.toCharArray());
keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
// Create PBE Cipher
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
// Initialize PBE Cipher with key and parameters
pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

//decrypt
pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
byte hash[] = (new BASE64Decoder()).decodeBuffer(encryptedPassword);

byte[] decryptPassword = pbeCipher.doFinal(hash);
return new String(decryptPassword);
} catch (Exception e) {

}
return null;
}


}

Thursday, June 08, 2006

regular expression for file name comparision

"[0-9]+.*|.*[!,*,&,@,#,$,%,^,*, \\s].*"

Saturday, March 04, 2006

How to access more than one resultset from jdbc storedprocedures

 
public List executeCallableStatement(CallableStatement colStmt) {
ResultSet res = null;
List result = null;
List multiResult = new ArrayList();
int count = 1;
try {
colStmt.execute();
while (true) {
res = colStmt.getResultSet();
if (res != null){
result = generateResultList(res);
count ++;
}
if(count > 1){
multiResult.add(result);
}
// Advance and quit if done
if ((colStmt.getMoreResults() == false)
&& (colStmt.getUpdateCount() == -1))
{
if (count == 2)
{
multiResult = result;
}
break;
}
}
} catch (SQLException e) {
e.printStackTrace();
}

return multiResult;
}

/**
* Genarates ArrayList for given result set.
* @param res
* @return
*/
private List generateResultList(ResultSet res){
Hashtable hashTab = null;
List resultList = new ArrayList();
try {
if (res != null) {
ResultSetMetaData rsm = res.getMetaData();
int columnCount = rsm.getColumnCount();
while (res.next()) {
hashTab = new Hashtable();
for (int i = 1; i <= columnCount; i++) {
String columnName = rsm.getColumnName(i);
if (rsm.getColumnType(i) == Types.VARCHAR)
hashTab.put(columnName, res.getString(columnName));
else if (rsm.getColumnType(i) == Types.INTEGER)
hashTab.put(columnName, new Integer(res
.getInt(columnName)));
else if (rsm.getColumnType(i) == Types.TIMESTAMP)
hashTab.put(columnName, res
.getTimestamp(columnName));
else if (rsm.getColumnType(i) == Types.DECIMAL)
hashTab.put(columnName, new Float(res
.getFloat(columnName)));
}
resultList.add(hashTab);
}
}else{
return null;
}
} catch (SQLException e) {
e.printStackTrace();
}

return resultList;
}