public String escapeLDAPSearchFilter(String filter){
  StringBuilder sb=new StringBuilder();
  for (int i=0; i < filter.length(); i++) {
    char curChar=filter.charAt(i);
switch (curChar) {
case '\\':
      sb.append("\\5c");
    break;
case '*':
  sb.append("\\2a");
break;
case '(':
sb.append("\\28");
break;
case ')':
sb.append("\\29");
break;
case '\u0000':
sb.append("\\00");
break;
default :
sb.append(curChar);
}
}
return sb.toString();
}

public String escapeLDAPSearchFilter ( String filter ) { StringBuilder sb = new StringBuilder ( ) ; for ( int i = 0 ; i < filter . length ( ) ; i ++ ) { char curChar = filter . charAt ( i ) ; switch ( curChar ) { case '\\' : sb . append ( "\\5c" ) ; break ; case '*' : sb . append ( "\\2a" ) ; break ; case '(' : sb . append ( "\\28" ) ; break ; case ')' : sb . append ( "\\29" ) ; break ; case '' : sb . append ( "\\00" ) ; break ; default : sb . append ( curChar ) ; } } return sb . toString ( ) ; }
escapes the given string for use within an ldap search filter . this implementation is provided courtesy of owasp : https : / / www . owasp . org / index . php / preventing_ldap_injection_in_java
