谁知道3des中pkcs5填充方式怎么弄?和pkcs7填充有什么不同? 谁知道3des中pkcs5填充方式怎么弄?和pkcs7填充有什么不同? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 什么是填充? 如前所述,如果使用分组密码而消息长度不是块长度的整数倍,那么,必须用字节填充最后一个块以凑成完整的块大小。有许多方法填充块,譬如全用零或一。在本教程中,我们将对私钥加密使用PKCS5 填充,而对公钥加密使用PKCS1。 使用PKCS5 时,由一个其值表示剩余字节数的重复字节来填充短块。我们不会在本教程中更深入地讨论填充算法,但您需要了解的信息是,JDK 1.4 支持下列填充技术: 无填充 ·PKCS5 ·OAEP ·SSL3 BouncyCastle 库(请参阅第三方库充实了安全性和参考资料)支持其它填充技术。 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! MFC-请问如何在分割好的窗口中添加文本??? 在另外一个线程访问对话框中的控件 在bat文件中,如何copy的同时创建目录?? 初学WIN32 SDK ,创建窗口报错,请大虾指教 在MFC中 TRACE0(""); 是做什么用的? 急!!!急!!!在线等待 关于文本编辑器的问题? 为什么我不能改标题? 请问,当我重载CToolBar 的OnPaint函数时???? BHO 在页面中添加js和一个按钮 connect()出错!在线急求!回复便给分! 请问快速合并文件的方法
如前所述,如果使用分组密码而消息长度不是块长度的整数倍,那么,必须用字节填充最后一个块以凑成完整的块大小。有许多方法填充块,譬如全用零或一。在本教程中,我们将对私钥加密使用PKCS5 填充,而对公钥加密使用PKCS1。
使用PKCS5 时,由一个其值表示剩余字节数的重复字节来填充短块。我们不会在本教程中更深入地讨论填充算法,但您需要了解的信息是,JDK 1.4 支持下列填充技术:
无填充
·PKCS5
·OAEP
·SSL3
BouncyCastle 库(请参阅第三方库充实了安全性和参考资料)支持其它填充技术。
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!