谁知道3des中pkcs5填充方式怎么弄?和pkcs7填充有什么不同?
解决方案 »
- 求教,有directdraw操作显存数据经验的高手进
- vc在线程序更新 如何更新更新程序update.exe
- 请各位高人指点一下 先行谢过了
- VC2005 里怎么使用SkinMagic
- 哪位老大有:WINDOWS网络编程技术的配套码呀?小弟跪求!
- 我在看王艳平编著的《windows程序设计》(人民邮电出版社出的那本书)时遇到一个关于模块句柄的问题,请指教
- DoDataExchange没有执行,有谁碰到过吗????
- IDispatch的功能是作什么呢?
- 有关服务器同时与几个客户端通信的问题
- 如何在对话框中实现打印的功能(分数不多,尽请笑纳)
- 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!