public static byte[] PBEncryption(String rStr,String rPassword)
  {
    try
    {
      PBEKeySpec pbeKeySpec;
      PBEParameterSpec pbeParamSpec;
      SecretKeyFactory keyFac;
      // Salt
      byte[] salt = {
          (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
          (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
      };
      // Iteration count
      int count = 20;
      // Create PBE parameter set
      pbeParamSpec = new PBEParameterSpec(salt, count);
      // Prompt user for encryption password.
      // Collect user password as char array (using the
      // "readPasswd" method from above), and convert
      // it into a SecretKey object, using a PBE key
      // factory.
      pbeKeySpec = new PBEKeySpec(rPassword.toCharArray());
      keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
      SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);      // Create PBE Cipher
      Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");      // Initialize PBE Cipher with key and parameters
      pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);      // Our cleartext
      byte[] cleartext = rStr.getBytes();      // Encrypt the cleartext
      byte[] ciphertext = pbeCipher.doFinal(cleartext);
      return ciphertext;
    }catch(Exception e)
    {
      e.printStackTrace();
      return new byte[0];
    }
  }

解决方案 »

  1.   

    public static byte[] PBDecryption(byte[] rByte,String rPassword)
      {
        try
        {
          PBEKeySpec pbeKeySpec;
          PBEParameterSpec pbeParamSpec;
          SecretKeyFactory keyFac;
          // Salt
          byte[] salt = {
              (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
              (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
          };
          // Iteration count
          int count = 20;
          // Create PBE parameter set
          pbeParamSpec = new PBEParameterSpec(salt, count);
          // Prompt user for encryption password.
          // Collect user password as char array (using the
          // "readPasswd" method from above), and convert
          // it into a SecretKey object, using a PBE key
          // factory.
          pbeKeySpec = new PBEKeySpec(rPassword.toCharArray());
          keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
          SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);      // Create PBE Cipher
          Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");      // Initialize PBE Cipher with key and parameters
          pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);      // Decrypt the cleartext
          byte[] ciphertext = pbeCipher.doFinal(rByte);
          return ciphertext;
        }catch(Exception e)
        {
          e.printStackTrace();
          return new byte[0];
        }
      }
      

  2.   

    谢谢!
    请问上面两个例子里的rStr 参数或 rByte参数是起什么作用的?
    回答了这个另外10分几一起给啦
      

  3.   

    rStr是用户口令,password 是你的加密密码。
    rByte是加密后的用户口令,password是你的加密密码。
      

  4.   


    在public static byte[] PBEncryption(String rStr,String rPassword)和public static byte[] PBDecryption(byte[] rByte,String rPassword)函数里面,String rStr和byte[] rByte的作用难道是不同的?
    这两个函数的返回值应该是加密以后的用户口令,参数String password是用户口令,我对您上面说rStr/rByte 是用户口令这句话不太理解,能否再给我详细点的解释?谢谢!
      

  5.   

    public static byte[] PBEncryption(用户口令,你的加密密码) // 返回用户口令的密文。
    public static byte[] PBDecryption(用户口令的密文,你的加密密码)// 返回用户口令的明文