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

????? }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值