密文不能用string 来保存,必须用char[]来保存string 保存---之前用string来保存,同事说不对
  //public string EnPasswd(string passwd)
        ////参数1:In明文;参数2:Out密文
        //{
        //    char ch;
        //    char[] w0 = new char[8 + 1];
        //    char[] w1 = new char[8 + 1];
        //    int i, s;        //    for (i = 0; i < 8; i++)
        //    {
        //        w0[i] = '\0';
        //        w1[i] = '\0';
        //    }        //    for (i = 0; i < passwd.Length; i++)
        //    {
        //        w0[i] += Convert.ToChar(passwd.Substring(i, 1));
        //    }        //    for (i = 0, ch = '\0'; i < 8; i++)
        //    {
        //        ch += w0[i];        //        ch = Convert.ToChar(ch ^ 0xda);
        //        w0[i] = ch;
        //    }        //    for (i = 0; i < 8; i++)
        //    {
        //        ch = w0[i];
        //        for (s = 0; s < 8; s++)
        //        {
        //            w1[(i + s) % 8] |= Convert.ToChar(((Convert.ToInt32(ch) & 1) << i));
        //            ch >>= 1;
        //        }
        //    }        //    StringBuilder st = new StringBuilder();
        //    st = st.Append(w1);
        //    string res = st.ToString();
        //    return res;
        //}
char[] 保存:
换成char[]来保存与下载的也不相符
//public char[]EnPasswd(string passwd)
        ////参数1:In明文;参数2:Out密文
        //{
        //    char ch;
        //    char[] w0 = new char[8 + 1];
        //    char[] w1 = new char[8 + 1];
        //    int i, s;        //    for (i = 0; i < 8; i++)
        //    {
        //        w0[i] = '\0';
        //        w1[i] = '\0';
        //    }        //    for (i = 0; i < passwd.Length; i++)
        //    {
        //        w0[i] += Convert.ToChar(passwd.Substring(i, 1));
        //    }        //    for (i = 0, ch = '\0'; i < 8; i++)
        //    {
        //        ch += w0[i];        //        ch = Convert.ToChar(ch ^ 0xda);
        //        w0[i] = ch;
        //    }        //    for (i = 0; i < 8; i++)
        //    {
        //        ch = w0[i];
        //        for (s = 0; s < 8; s++)
        //        {
        //            w1[(i + s) % 8] |= Convert.ToChar(((Convert.ToInt32(ch) & 1) << i));
        //            ch >>= 1;
        //        }
        //    }         //    return w1;
        //}用char[]来保存,加密后的密码与从数据库下载的密码(密码字段类型为:binary,保存在数据库时已经加密,下载到本地文件时候 密码已经转为 char[])一样 也不对啊,哪位大神帮忙看看

解决方案 »

  1.   

    涉及到位运算,要用char,但是char的范围是 -128~127,如果超出这个范围就运算不准确了,所以确定是否用char还是byte,以及涉及到两方的编码是否一致
      

  2.   

       public static char[] EnPasswd(string passwd)
            //参数1:In明文;参数2:Out密文
            {
                char ch;
                char[] w0 = new char[8 + 1];
                char[] w1 = new char[8 + 1];
                int i, s;            for (i = 0; i < 8; i++)
                {
                    w0[i] = '\0';
                    w1[i] = '\0';
                }            for (i = 0; i < passwd.Length; i++)
                {
                    w0[i] += Convert.ToChar(passwd.Substring(i, 1));
                }            for (i = 0, ch = '\0'; i < 8; i++)
                {
                    ch += w0[i];                ch = Convert.ToChar(ch ^ 0xda);
                    w0[i] = ch;
                }            for (i = 0; i < 8; i++)
                {
                    ch = w0[i];
                    for (s = 0; s < 8; s++)
                    {
                        w1[(i + s) % 8] |= Convert.ToChar(((Convert.ToInt32(ch) & 1) << i));
                        ch >>= 1;
                    }
                }    
                return w1;
            }大哥们指点下啊,小弟还是没搞明白