public class Test2 {  public static String encryptStr(String decryptpasswd) {
      char  key1 = '+', key2 = '@';
      int   i = 0, j = 0, decryptpasswdlength = 0;
      char[] tmpstr = new char[128];
      String encryptpasswd = null;
      if ((decryptpasswd == null) || (decryptpasswdlength = decryptpasswd.length()) == 0){
         return(encryptpasswd);
      }
      i = 0;
      while (decryptpasswd.charAt(i) == ' ') i++;
      if (i >= decryptpasswdlength){
        return(encryptpasswd);
      }
      j = decryptpasswdlength-1;
      for (i = 0; i < decryptpasswdlength; i+=2){
        tmpstr[j] = (char)(decryptpasswd.charAt(i) << ((j+1)%7));
        tmpstr[j] = (char)(tmpstr[j] | key1);
        tmpstr[j] = (char)(tmpstr[j] & 0x00FF);
        j--;
      }
      for (i = 1; i < decryptpasswdlength; i+=2){
        tmpstr[j] = (char)(decryptpasswd.charAt(i) << ((j+2)%7));
        tmpstr[j] = (char)(tmpstr[j] | key2);
        tmpstr[j] = (char)(tmpstr[j] & 0x00FF);
        j--;
      }
      for (i = 0; i < decryptpasswdlength; i++){
        if (tmpstr[i] == ' ')
          tmpstr[i] = '^';
      }
      encryptpasswd = String.copyValueOf(tmpstr,0,decryptpasswdlength);
      return(encryptpasswd);
    }

 public static void main(String[] args) {
Test2 test2 = new Test2();
String str1 = Test2.encryptStr("11111");
System.out.println(str1);//???;+
  String str2 = "???;+";
  System.out.println(str1.equals(str2));
 
  //以上现象导致的结果是 sql语句打印出来:select * from app_employee where userid='test1'and password='???;+'
  //再plsql 中能执行出来结果,但是select * from app_employee where userid='test1'and password='???;+',数据库中有数据单查询不出结果
  //原因:上述两个字符串为什么不相同,
}
}///////////////////////////////////////现在很苦恼  查询不出结果就取不出用户信息,各位大侠帮帮我一下吧!
谢谢

解决方案 »

  1.   

    我用charAt,发现时编码问题 return(new String(encryptpasswd.getBytes()));    打印出来就是true了。两个字符串就相等了。楼主给分吧。
      

  2.   

    String的构造函数String(byte[] bytes),通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。这里我的平台默认编码是utf-8的。为了保证以后程序移植的正确性。 最好在返回时return(new String(encryptpasswd.getBytes(),"utf-8")),再处理下异常。
      

  3.   

    能不能更好的解释一下呢?为什么是UTF-8呢?
    能不能给我说一下原理呢?感激不尽
      

  4.   

    上面有点错误,
    关键在于String的方法
    1 getBytes() 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
    2getBytes(String charsetName) 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。你用encryptpasswd.getBytes("GBK")吧。因为我的平台是gbk的。所以encryptpasswd.getBytes(),和encryptpasswd.getBytes("GBK")效果一样。也就是说你上面最后返回的return(encryptpasswd);中的encryptpasswd是字符串"???;+"经过GBK编码后得到的,所以要用getBytes("GBK")转化成字符串"???;+"
      

  5.   

    所以你最后return(new String(encryptpasswd.getBytes("GBK")));就ok了。
      

  6.   

    所以你最后return(new String(encryptpasswd.getBytes("GBK")));就ok了。没效果啊!
      

  7.   

    return(encryptpasswd);
    说实话 我工作一年了 头会看到 return语句这么写的
      

  8.   

    你只需要在返回时:return (new String(encryptpasswd.getBytes("UTF-8")));
    编码就能工作了.
    这个是一个密码加密程序???也太简单了点吧!