谁知道3des中pkcs5填充方式怎么弄?和pkcs7填充有什么不同?

解决方案 »

  1.   

    什么是填充?
      如前所述,如果使用分组密码而消息长度不是块长度的整数倍,那么,必须用字节填充最后一个块以凑成完整的块大小。有许多方法填充块,譬如全用零或一。在本教程中,我们将对私钥加密使用PKCS5 填充,而对公钥加密使用PKCS1。
      使用PKCS5 时,由一个其值表示剩余字节数的重复字节来填充短块。我们不会在本教程中更深入地讨论填充算法,但您需要了解的信息是,JDK 1.4 支持下列填充技术:
      无填充
      ·PKCS5
      ·OAEP
      ·SSL3
      BouncyCastle 库(请参阅第三方库充实了安全性和参考资料)支持其它填充技术。
      

  2.   

    import java.security.*;
      import javax.crypto.*;
      //
      // encrypt and decrypt using the DES private key algorithm
      public class PrivateExample {
       public static void main (String[] args) throws Exception {
        //
        // check args and get plaintext
        if (args.length !=1) {
         System.err.println("Usage: java PrivateExample text");
         System.exit(1);
        }
        byte[] plainText = args[0].getBytes("UTF8");
        //
        // get a DES private key
        System.out.println( "\nStart generating DES key" );
        KeyGenerator keyGen = KeyGenerator.getInstance("DES");
        keyGen.init(56);
        Key key = keyGen.generateKey();
        System.out.println( "Finish generating DES key" );
        //
        // get a DES cipher object and print the provider
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        System.out.println( "\n" + cipher.getProvider().getInfo() );
        //
        // encrypt using the key and the plaintext
        System.out.println( "\nStart encryption" );
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] cipherText = cipher.doFinal(plainText);
        System.out.println( "Finish encryption: " );
        System.out.println( new String(cipherText, "UTF8") );
        //
        // decrypt the ciphertext using the same key
        System.out.println( "\nStart decryption" );
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] newPlainText = cipher.doFinal(cipherText);
        System.out.println( "Finish decryption: " );
        System.out.println( new String(newPlainText, "UTF8") );
       }
      }  ■私钥密码术样本执行
      D:\IBM>java PrivateExample "This is a test!"  Start generating DES key
      Finish generating DES key  SunJCE Provider (implements DES, Triple DES, Blowfish, PBE, Diffie-Hellman,
      HMAC-MD5, HMAC-SHA1)  Start encryption
      Finish encryption:
      Kdkj4338*3n1#kxkgtixo4  Start decryption
      Finish decryption:
      This is a test!