码迷,mamicode.com
首页 > 编程语言 > 详细

Java 非对称加密

时间:2015-06-10 22:13:13      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

 1 package test;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.ObjectInputStream;
 6 import java.io.ObjectOutputStream;
 7 import java.security.Key;
 8 import java.security.KeyPair;
 9 import java.security.KeyPairGenerator;
10 
11 import javax.crypto.Cipher;
12 
13 public class testcase {
14 
15     public static void main(String[] args) throws Exception {
16         PublicEnrypt();
17         privateDecrypt();
18 
19     }
20     //加密
21     public static void PublicEnrypt() throws Exception{
22         // 实例化加密工具,使用RSA算法
23         Cipher cipher=Cipher.getInstance("RSA");
24         // 实例化Key
25         KeyPairGenerator keypairGenerator=KeyPairGenerator.getInstance("RSA");
26         // 获得一对钥匙
27         KeyPair keyPair=keypairGenerator.generateKeyPair();
28         // 获取公钥
29         Key publicKey=keyPair.getPublic();
30         // 获取私钥
31         Key privateKey=keyPair.getPrivate();
32         // 用公钥加密
33         cipher.init(Cipher.ENCRYPT_MODE, publicKey);
34         byte []result =cipher.doFinal("数据内容".getBytes("UTF-8"));
35         // 将私钥写入文件
36         saveKey(privateKey,"zxx_private.key");
37         // 加密后的数据存储
38         saveData(result,"public_encryt.dat");        
39         System.out.println(new String(result,"UTF-8"));
40     }
41     //解密
42     public static void privateDecrypt() throws Exception{
43         //实例化加密工具
44         Cipher cipher=Cipher.getInstance("RSA");
45         // 获取私钥
46         Key privateKey=readKey("zxx_private.key");
47         // 获取加密后的信息
48         byte[] src=readData("public_encryt.dat");
49         // 用私钥解密
50         cipher.init(Cipher.DECRYPT_MODE, privateKey);
51         byte[] result=cipher.doFinal(src);
52         System.out.println(new String(result,"UTF-8"));
53     }
54     // 保存数据
55     public static void saveData(byte[] result,String fileName) throws Exception{
56         FileOutputStream fosData=new FileOutputStream(fileName);
57         fosData.write(result);
58         fosData.close();
59     }
60     // 保存密钥
61     public static void saveKey(Key key,String fileName) throws Exception{
62         FileOutputStream fosKey=new FileOutputStream(fileName);
63         ObjectOutputStream oosSecretKey=new ObjectOutputStream(fosKey);
64         oosSecretKey.writeObject(key);
65         oosSecretKey.close();
66         fosKey.close();    
67     }
68       //读取密钥
69     public static Key readKey(String fileName) throws Exception{
70         FileInputStream fisKey=new FileInputStream(fileName);
71         ObjectInputStream oisKey=new ObjectInputStream(fisKey);
72         Key key=(Key)oisKey.readObject();
73         oisKey.close();
74         fisKey.close();
75         return key;
76     }
77        // 读取数据
78     public static byte[] readData(String fileName) throws Exception{
79         FileInputStream fisDat=new FileInputStream(fileName);
80         byte[] src=new byte[fisDat.available()];
81         int len=fisDat.read(src);
82         int total=0;
83         while(total<src.length){
84             total+=len;
85             len=fisDat.read(src, total, src.length-total);
86         }
87         fisDat.close();
88          return src;
89     }
90     
91 }

 

Java 非对称加密

标签:

原文地址:http://www.cnblogs.com/rain-tl/p/4567255.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!