
/*
 *  Str Lock; temp implementation of String Encription/Decription
 *
 *
 */

package graphbrowser;

public class StrLock{
    public static int OFFSET=1997;
    /** translate int value Hex string */
    public static String toStr(long value) {
        value+=OFFSET;

        String mode=new String("0000");  //?? temporarily use default value 4
        String str=Long.toString( value, 16 );
        int len1=4-str.length();
        mode=mode.substring(0,len1);
        str=mode.concat(str);
        return str;
    }

    public static long toValue(String charStr) {
        long a=Long.parseLong(charStr,16);
        a-=OFFSET;
        return a;
    }

    public static String encript(String source) {
        String target=new String("");
        int count=0;
        while (count < source.length()) {

          try {
            long temp=(long) source.charAt(count);
            target=target.concat(toStr(temp));
            count++;
          } catch (StringIndexOutOfBoundsException e) {
          }

        }
    return target;
    }

    public static String decript(String source) {

      int head=0,tail=4;
      String sub;
      String target=new String("");
      try {
        while (tail<=source.length() ) {
          sub=source.substring(head,tail);
          long value=toValue(sub);
          //char data[]={(char)value};
          target=target+(char)value;
          //target=target.concat(new String(data));
          head+=4;  tail+=4;
        }
       } catch (StringIndexOutOfBoundsException e) {
      }
      return target;
    }

   /* public static void main (String args[]) {

        System.out.println("liqian");
        System.out.println(StrLock.encript("liqian"));
        System.out.println(StrLock.decript(StrLock.encript("liqian")));
    }
   */
}

