BouncyCastle JCE实践(三)

来源:互联网 发布:淘宝买枪零件犯法吗 编辑:程序博客网 时间:2024/05/21 08:49
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
 

密钥的产生

对称密钥的产生

    对称加密采用了对称密码编码技术,它的特点是文件加密和解密使用相同的密钥,即加密密钥也可以用作解密密钥。这种方法在密码学中叫做对称加密算法,对称加密算法使用起来简单快捷,密钥较短,且破译困难,除了数据加密标准(DES),另一个对称密钥加密系统系统是国际数据加密算法(IDEA),它比DES的加密性好,而且对计算机功能要求也没有那么高。IDEA加密标准由PGP(Pretty Good Privacy)系统使用。

//首先要import javax.crypto.*;

SecretKey key=null;

     try

{

//指定算法,这里为DES;如果想用Blowfish算法,则用 getInstance("Blowfish")

//BouncyCastle基本上支持所有通用标准算法

KeyGenerator keygen=KeyGenerator.getInstance("DES");

//指定密钥长度,长度越高,加密强度越大

keygen.init(56);

//产生密钥

key=keygen.generateKey();

//构造输出文件,这里的目录是动态的,根据用户名称来构造目录

     ObjectOutputStream keyFile=new ObjectOutputStream(new FileOutputStream

             ("c:/安全文件/"+misClass.username+"/对称/对称密钥/yhb.des"));

     keyFile.writeObject(key);

     keyFile.close();

     }

     catch(NoSuchAlgorithmException e5)

{

// generateKey()抛出的异常

     System.out.print("no such algorithm");

     System.exit(0);

     }

     catch(IOException e4)

     {

     System.out.print("error when generate the des key");

     System.exit(0);

}

非对称密钥的产生

1976年,美国学者Dime和Henman为解决信息公开传送和密钥管理问题,提出一种新的密钥交换协议,允许在不安全的媒体上的通讯双方交换信息,安全地达成一致的密钥,这就是“公开密钥系统”。相对于“对称加密算法”这种方法也叫做“非对称加密算法”。

与对称加密算法不同,非对称加密算法需要两个密钥:公开密钥(publickey)和私有密钥(privatekey)。公开密钥与私有密钥是一对,如果用公开密钥对数据进行加密,只有用对应的私有密钥才能解密;如果用私有密钥对数据进行加密,那么只有用对应的公开密钥才能解密。因为加密和解密使用的是两个不同的密钥,所以这种算法叫作非对称加密算法。

//密钥对

    KeyPair keys=null;

    try

{

//指定算法

KeyPairGenerator kpg=KeyPairGenerator.getInstance("RSA");

//指定长度

    kpg.initialize(1024);

keys=kpg.genKeyPair();

//公钥

byte[] key1=keys.getPublic().getEncoded();

//私钥

    byte[] key2=keys.getPrivate().getEncoded();

 

    //构造公钥文件并写入公钥

FileOutputStream keyFile1=new FileOutputStream

       ("c:/安全文件/"+misClass.username+"/非对称/本人公私钥/yhb.public");

     keyFile1.write(key1);

     keyFile1.close();

    //构造私钥文件并写入私钥

    keyFile1=new FileOutputStream

     ("c:/安全文件/"+misClass.username+"/非对称/本人公私钥/yhb.private");

    keyFile1.write(key2);

    keyFile1.close();

    }

    catch(NoSuchAlgorithmException e8)

{

//算法异常

     System.out.print("no such algorithm");

     System.exit(0);

     }

     catch(IOException e9)

     {

     System.out.print("error when generate the rsa key");

     System.exit(0);

     }

 

作者又名HongSoft,研究领域:1)基于工作流的BPM系统研究2)基于JAVA的信息安全技术.欢迎和大家讨论JAVA相关各方面问题 hongbosoftware@163.com

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击