对称解密的实现
对称加密/解密算法在电子商务交易过程中存在几个问题:
(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);
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10794571/viewspace-974792/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10794571/viewspace-974792/
本文介绍了使用BouncyCastle JCE进行对称解密的具体步骤,包括从密钥文件读取密钥、创建并初始化Cipher对象,以及通过CipherInputStream对文件进行解密的过程。
601

被折叠的 条评论
为什么被折叠?



