站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题BouncyCastle JCE实践(五)

BouncyCastle JCE实践(五)

摘要: BouncyCastle JCE实践(五)

对称解密的实现 对称加密/解密算法在电子商务交易过程中存在几个问题:
(1) 要求提供一条安全的渠道使通讯双方在首次通讯时协商一个共同的密钥。直接的面对面协商可能是不现实而且难于实施的,所以双方可能需要借助于邮件和电话等其它相对不够安全的手段来进行协商;
(2) 密钥的数目难于管理。因为对于每一个合作者都需要使用不同的密钥,很难适应开放社会中大量的信息交流;
(3) 对称加密算法一般不能提供信息完整性的鉴别。它无法验证发送者和接受者的身份;
对称密钥的管理和分发工作是一件具有潜在危险的和烦琐的过程。对称加密是基于共同保守秘密来实现的,采用对称加密技术的贸易双方必须保证采用的是相同的密钥,保证彼此密钥的交换是安全可靠的,同时还要设定防止密钥泄密和更改密钥的程序。
对称解密的代码实现如下:
//从密钥文件中读密钥
SecretKey key=null;
try
{ObjectInputStream keyFile=new ObjectInputStream(
new FileInputStream("c:\\安全文件\\"+misClass.username+"\\对称\\对称密钥\\yhb.des"));
key=(SecretKey)keyFile.readObject();
keyFile.close(); }
catch(FileNotFoundException ey1) {
System.out.println("Error when read keyFile");
System.exit(0); }
catch(Exception ey2) {
System.out.println("error when read the keyFile");
System.exit(0); }
//用key产生Cipher
Cipher cipher=null;
try {
//设置算法,应该与加密时的设置一样
cipher=Cipher.getInstance("DES");
//设置解密模式
cipher.init(Cipher.DECRYPT_MODE,key);
}catch(Exception ey3) {
System.out.println("Error when create the cipher");
System.exit(0); }
//从对话框中取得要解密的文件并解密
File file=new File(dirstring,string1);
String filename=file.getName();
try {
//输出流,请注意文件名称的获取
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(
"c:\\安全文件\\文件\\"+filename.substring(0,filename.length()-4)));
//输入流
CipherInputStream in=new CipherInputStream(new BufferedInputStream(
new FileInputStream(file)),cipher);
int thebyte=0;
while((thebyte=in.read())!=-1) {
out.write(thebyte); }
in.close();
out.close(); }
catch(Exception ey5) {
System.out.println("Error when encrypt the file");
System.exit(0); }
↑返回目录
前一篇: BS开发技巧之一:利用Textarea实现长文本域的输入
后一篇: BouncyCastle JCE实践(四)