BouncyCastle JCE实践(五)

王朝java/jsp·作者佚名  2006-01-08
宽屏版  字体: |||超大  

对称解密的实现

对称加密/解密算法在电子商务交易过程中存在几个问题:

(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);

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有