现在在做一个jsp页面,数据库用的是oracle,想在注册用户页面的时候把密码保存在user表中的password字段里,但想在此字段中显示的密码进行加密,请问这个应该怎么实现

解决方案 »

  1.   

    就用md5(password)好了,
    比效用md5後的字串比較
      

  2.   


    import java.security.*;
    public class MD5_ok { public final static String MD5(String s) {
      char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
        'a', 'b', 'c', 'd', 'e', 'f' };
      try {
       byte[] strTemp = s.getBytes();
       MessageDigest mdTemp = MessageDigest.getInstance("MD5");
       mdTemp.update(strTemp);
       byte[] md = mdTemp.digest();
       int j = md.length;
       char str[] = new char[j * 2];
       int k = 0;
       for (int i = 0; i < j; i++) {
        byte byte0 = md[i];
        str[k++] = hexDigits[byte0 >>> 4 & 0xf];
        str[k++] = hexDigits[byte0 & 0xf];
       }
       return new String(str);
      } catch (Exception e) {
       return null;
      }
     } public static void main(String[] args) {
      // MD5_Test aa = new MD5_Test();  System.out.print(MD5_ok.MD5("gg"));
     }
    }
      

  3.   

    给你一个加密类,我们自己写的:
    package *.*.util.encrypt;public class DesEncrypt {
      public DesEncrypt() {
      }
      final static boolean ENCRYPT = false;   //加密参数
      final static boolean DECRYPT = true;    //解密参数  private static byte SubKey[][][] = new byte[2][16][48]; // 16圈子密钥
      private static boolean Is3DES; // 3次DES标志
      private static byte Tmp[] = new byte[256];
      private static byte deskey[] = new byte[16];  // initial permutation IP
      final static byte IP_Table[] = {
          58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
          62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
          57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
          61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7
      };
      // final permutation IP^-1
      final static byte IPR_Table[] = {
          40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31,
          38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29,
          36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27,
          34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25
      };
      // expansion operation matrix
      final static byte E_Table[] = {
          32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9,
          8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,
          16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,
          24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1
      };
      // 32-bit permutation function P used on the output of the S-boxes
      final static byte P_Table[] = {
          16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
          2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
      };
      // permuted choice table (key)
      final static byte PC1_Table[] = {
          57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18,
          10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
          63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22,
          14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4
      };
      // permuted choice key (table)
      final static byte PC2_Table[] = {
          14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10,
          23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2,
          41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
          44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
      };
      // number left rotations of pc1
      final static byte LOOP_Table[] = {
          1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
      };
      // The (in)famous S-boxes
      final static byte S_Box[][][] = {
          // S1
          {{14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},
           {0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8},
           {4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0},
           {15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}},
          // S2
          {{15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10},
           {3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5},
           {0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15},
           {13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9}},
          // S3
          {{10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8},
           {13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1},
           {13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7},
           {1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12}},
          // S4
          {{7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15},
           {13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9},
           {10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4},
           {3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14}},
          // S5
          {{2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9},
           {14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6},
           {4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14},
           {11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3}},
          // S6
          {{12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11},
           {10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8},
           {9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6},
           {4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13}},
          // S7
          {{4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1},
           {13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6},
           {1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2},
           {6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12}},
          // S8
          {{13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7},
           {1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2},
           {7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8},
           {2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}}
      };
      private static byte K[] = new byte[64];
      private static byte M[] = new byte[64];
      private static byte MR[] = new byte[48];
      private static byte tmp[] = new byte[32];  /**
       * Type—ENCRYPT:加密,DECRYPT:解密
       * 输出缓冲区(Out)的长度 >= ((datalen+7)/8)*8,即比datalen大的且是8的倍数的最小正整数
       * Out: 加密后的数据的缓冲区
       * In: 加密前的数据的缓冲区
       * datalen: 待加密的数据长度
       * Key: 加密的密钥
       * keylen: 密钥的长度
       * type: ENCRYPT=加密; DECRYPT=解密
       */
      public static int Des_Go(byte Out[], byte In[], long datalen, byte Key[], int keylen, boolean Type) {
        if (!(Out != null && In != null && Key != null))
          return 0;
        datalen = (datalen + 7) & 0xfffffff8;
        if (datalen == 0)
          return 0;
        SetKey(Key, keylen);
        if (!Is3DES) { // 1次DES
          int p = 0;
          for (long i = 0, j = datalen >> 3; i < j; ++i, p += 8) {
            byte ob[] = new byte[8];
            byte ib[] = new byte[8];
            for (int k = 0; k < 8; k++) {
              ob[k] = Out[k + p];
              ib[k] = In[k + p];
            }
            DES(ob, ib, SubKey[0], Type);
            for (int k = 0; k < 8; k++) {
              Out[k + p] = ob[k];
            }
          }
        } else { // 3次DES 加密:加(key0)-解(key1)-加(key0) 解密::解(key0)-加(key1)-解(key0)
          int p = 0;
          for (long i = 0, j = datalen >> 3; i < j; ++i, p += 8) {
            byte ob[] = new byte[8];
            byte ib[] = new byte[8];
            for (int k = 0; k < 8; k++) {
              ob[k] = Out[k + p];
              ib[k] = In[k + p];
            }
            DES(ob, ib, SubKey[0], Type);
            for (int k = 0; k < 8; k++) {
              Out[k + p] = ob[k];
            }
            for (int k = 0; k < 8; k++) {
              ob[k] = Out[k + p];
            }
            DES(ob, ob, SubKey[1], !Type);
            for (int k = 0; k < 8; k++) {
              Out[k + p] = ob[k];
            }
            for (int k = 0; k < 8; k++) {
              ob[k] = Out[k + p];
            }
            DES(ob, ob, SubKey[0], Type);
            for (int k = 0; k < 8; k++) {
              Out[k + p] = ob[k];
            }
          }
        }
        return (int)datalen;
      }  private static void SetKey(byte Key[], int len) {
        for (int i = 0; i < 16; i++) {
          deskey[i] = 0x0;
        }
        for (int i = 0; i < (len > 16 ? 16 : len); i++) {
          deskey[i] = Key[i];
        }
        SetSubKey(SubKey[0], deskey);
        if (len > 8) {
          Is3DES = true;
          byte b[] = new byte[8];
          for (int i = 0; i < 8; i++)
            b[i] = deskey[8 + i];
          SetSubKey(SubKey[1], b);
        } else {
          Is3DES = false;
        }
      }太长,不让一次发,待续
      

  4.   

    接上:
      private static void DES(byte Out[], byte In[], byte pSubKey[][], boolean Type) {
        ByteToBit(M, In, 64);
        Transform(M, M, IP_Table, 64);
        if (Type == ENCRYPT) {
          for (int i = 0; i < 16; ++i) {
            byte Ri[] = new byte[32];
            byte Li[] = new byte[32];
            for (int j = 0; j < 32; j++) {
              Ri[j] = tmp[j] = M[32 + j];
              Li[j] = M[j];
            }
            F_func(Ri, pSubKey[i]);
            Xor(Ri, Li, 32);
            for (int j = 0; j < 32; j++) {
              Li[j] = tmp[j];
            }
            for (int j = 0; j < 32; j++) {
              M[32 + j] = Ri[j];
              M[j] = Li[j];
            }
          }
        } else {
          for (int i = 15; i >= 0; --i) {
            byte Ri[] = new byte[32];
            byte Li[] = new byte[32];
            for (int j = 0; j < 32; j++) {
              Li[j] = tmp[j] = M[j];
              Ri[j] = M[j + 32];
            }
            F_func(Li, pSubKey[i]);
            Xor(Li, Ri, 32);
            for (int j = 0; j < 32; j++) {
              Ri[j] = tmp[j];
            }
            for (int j = 0; j < 32; j++) {
              M[32 + j] = Ri[j];
              M[j] = Li[j];
            }
          }
        }
        Transform(M, M, IPR_Table, 64);
        BitToByte(Out, M, 64);
      }  private static void SetSubKey(byte pSubKey[][], byte Key[]) {
        ByteToBit(K, Key, 64);
        Transform(K, K, PC1_Table, 56);
        for (int i = 0; i < 16; ++i) {
          RotateL(K, 28, LOOP_Table[i]);
          byte b[] = new byte[36];
          for (int j = 0; j < 36; j++) {
            b[j] = K[28 + j];
          }
          RotateL(b, 28, LOOP_Table[i]);
          for (int j = 0; j < 36; j++) {
            K[28 + j] = b[j];
          }
          Transform(pSubKey[i], K, PC2_Table, 48);
        }
      }  private static void F_func(byte In[], byte Ki[]) {
        Transform(MR, In, E_Table, 48);
        Xor(MR, Ki, 48);
        S_func(In, MR);
        Transform(In, In, P_Table, 32);
      }  private static void S_func(byte Out[], byte In[]) {
        int in = 0, out = 0;
        for (int i = 0, j, k; i < 8; ++i, in += 6, out += 4) {
          j = (In[in + 0] << 1) + In[in + 5];
          k = (In[in + 1] << 3) + (In[in + 2] << 2) + (In[in + 3] << 1) + In[in + 4];
          byte b1[] = new byte[4];
          byte b2[] = new byte[1];
          b2[0] = S_Box[i][j][k];
          ByteToBit(b1, b2, 4);
          for (int m = 0; m < 4; m++) {
            Out[m + out] = b1[m];
          }
        }
      }  private static void Transform(byte Out[], byte In[], byte Table[], int len) {
        for (int i = 0; i < len; ++i) {
          Tmp[i] = In[Table[i] - 1];
        }
        for (int i = 0; i < len; i++) {
          Out[i] = Tmp[i];
        }
      }  private static void Xor(byte InA[], byte InB[], int len) {
        for (int i = 0; i < len; ++i) {
          InA[i] ^= InB[i];
        }
      }  private static void RotateL(byte In[], int len, int loop) {
        for (int i = 0; i < loop; i++) {
          Tmp[i] = In[i];
        }
        for (int i = 0; i < len - loop; i++) {
          In[i] = In[i + loop];
        }
        for (int i = 0; i < loop; i++) {
          In[i + len - loop] = Tmp[i];
        }
      }  private static void ByteToBit(byte Out[], byte In[], int bits) {
        for (int i = 0; i < bits; ++i) {
          int out = (In[i >> 3] >> (i & 7)) & 1;
          Out[i] = (byte) out;
        }
      }  private static void BitToByte(byte Out[], byte In[], int bits) {
        for (int i = 0; i < (bits >> 3); i++) {
          Out[i] = 0x00;
        }
        for (int i = 0; i < bits; ++i) {
          Out[i >> 3] |= In[i] << (i & 7);
        }
      }
      /**
       * 把加密后的数据转换成字符串,所生成的字符串只包含了从0--9和从A--F16个字符。
       * Out: 转换后的输出缓冲区, 其大小是len*2;
       * In: 待转换的数据缓冲区
       * len: 待转换的数据的长度
       */
      public static void ConvertCode(byte Out[], byte In[], int len) {
        for (int i = 0; i < len; i++) {
          Out[2*i] = ((In[i]>>4)&15) < 10 ? (byte)(0x30+((In[i]>>4)&15)):(byte)(0x37+((In[i]>>4)&15));
          Out[2*i+1] = (In[i]&15) < 10 ? (byte)(0x30+(In[i]&15)):(byte)(0x37+(In[i]&15));
        }
      }
      /**
       * 加密并转换字符
       * @param Out: 加密转换后的输出缓冲区
       * @param In: 待加密的字符缓冲区
       * @param len: 待加密的字符缓冲区的长度
       *
       * @return: 加密转换后的输出缓冲区的长度
       */
      public static int ImEncrypt(byte Out[], byte In[], int len) {
        byte key[] = {0, 2, 0, 0, 9, 3, 5, 1, 9, 8, 0, 0, 9, 1, 7, 9};
        byte buf[] = new byte[255];    for (int i=0; i<buf.length; i++) buf[i] = 0x00; //append 0x00 to tail of source    if (len == 0) {
          len = 8;
          for (int i=0; i<len; i++) buf[i] = (byte) (0x31 + i);
        } else {
          for (int i=0; i<len; i++) buf[i] = In[i];
        }
        int ret = Des_Go(buf, buf, len, key, key.length, ENCRYPT);
        ConvertCode(Out, buf, ret);
        return 2*ret;
      }  public static String ImEncrypt(String plaint) {
        String cypher = "";
        byte bPlaint[] = plaint.getBytes();
        byte bCypher[] = new byte[32];    int ret = ImEncrypt(bCypher, bPlaint, plaint.length());    cypher = new String(bCypher, 0, ret);
        return cypher;
      }  /////////////////////////////////////////////////////////////////////////////
      public static void main(String[] args) {
        byte key[] = {0, 2, 0, 0, 9, 3, 5, 1, 9, 8, 0, 0, 9, 1, 7, 9};
        byte buf[] = new byte[255];
        byte str[] = "webmaster".getBytes();    for (int i = 0; i < buf.length; i++) {
          buf[i] = 0x00;
        }
        int ret = ImEncrypt(buf, str, str.length);
        System.out.println(new String(buf));    /*for (int i = 0; i < str.length; i++) {
          buf[i] = str[i];
        }
        System.out.println("Before encrypting");
        System.out.println(new String(buf));    int len = DesEncrypt.Des_Go(buf, buf, str.length, key, key.length, ENCRYPT);
        byte out[] = new byte[len*2];
        DesEncrypt.ConvertCode(out, buf, len);
        System.out.println(new String(out));    System.out.println("After encrypting");
        System.out.println(new String(buf));    DesEncrypt.Des_Go(buf, buf, str.length, key, key.length, DECRYPT);
        System.out.println("After decrypting");
        System.out.println(new String(buf));*/
      }----------------------------
    调用加密方法即可:
    String encrypt = DesEncrypt.ImEncrypt(Password.trim());//
      

  5.   

    xp1204(深圳不是个好地方,我却还是要待下去) ( ) 信誉:100    Blog  2007-3-23 16:23:15  得分: 0  
     
     
       
    md5是不能解密的
    自己写的就可以了  
     
    需要解密干吗?只要比较用户的密码和数据库里面是否一致,那就都加密了比较不是一样,