本文实例讲述了Java实现的RSA加密解密算法。分享给大家供大家参考,具体如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import javax.crypto.Cipher;
public class RSAUtils{
public static String makekeyfile(String pubkeyfile, String prikeyfile) {
String result = "生成公私钥文件失败";
try{
// KeyPairGenerator用于生成公私钥对,基于RSA算法生成对象
KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");
// 初始化密钥对生成器,密钥大小为1024位
gen.initialize(1024);
// //生成强随机数
// SecureRandom random = new SecureRandom();
// gen.initialize(1024,random);
// 生成一个密钥对,保存在pair中
KeyPair pair = gen.generateKeyPair();
// 得到私钥
RSAPrivateKey priKey = (RSAPrivateKey) pair.getPrivate();
// 得到公钥
RSAPublicKey pubKey = (RSAPublicKey) pair.getPublic();
// 生成私钥文件
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(prikeyfile));
os.writeObject(priKey);
os.flush();
os.close();
//生成公钥文件
os = new ObjectOutputStream(new FileOutputStream(pubkeyfile));
os.writeObject(pubKey);
os.flush();
os.close();
result = "生成公钥文件【"+pubkeyfile+"】生成私钥文件【"+prikeyfile+"】";
}catch(Exception e){
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
try{
String pubfile = "F:/images/pub.key";
String prifile = "F:/images/pri.key";
String result = null;
//result = makekeyfile(pubfile, prifile);
result = markPuPra(pubfile, prifile);
System.out.println(result);
}catch(Exception e){
e.printStackTrace();
}
}
public static String markPuPra(String pubfile,String prifile){
String results = "加解密出错";
try{
ObjectInputStream os = new ObjectInputStream(new FileInputStream(pubfile));
RSAPublicKey pubkey = (RSAPublicKey) os.readObject();
os.close();
os = new ObjectInputStream(new FileInputStream(prifile));
RSAPrivateKey prikey = (RSAPrivateKey) os.readObject();
os.close();
String utf = "UTF-8";
String msg = "##中国%%的)人@+_";
// 使用公钥加密私钥解密
System.out.println("原文: " + msg);
byte[] puk = handleData(pubkey, msg.getBytes(utf), 1);
System.out.println("加密后文件数据: " + new String(puk, utf));
byte[] dpuk = handleData(prikey, puk, 0);
System.out.println("解密后文件数据: " + new String(dpuk, utf));
msg = "jd#我0们的¥人+=#新";
// 使用私钥加密公钥解密
System.out.println("原文: " + msg);
byte[] prk = handleData(prikey, msg.getBytes(utf), 1);
System.out.println("加密后文件数据: " + new String(prk, utf));
byte[] dprk = handleData(pubkey, prk, 0);
System.out.println("解密后文件数据: " + new String(dprk, utf));
results = "加解密完成";
}catch(Exception e){
e.printStackTrace();
}
return results;
}
/**
*
* @param k
* @param data
* @param encrypt 1 加密 0解密
* @return
* @throws Exception
*/
public static byte[] handleData(Key key, byte[] data, int type) throws Exception {
if (key != null) {
Cipher ci = Cipher.getInstance("RSA");
if (type == 1) {
ci.init(Cipher.ENCRYPT_MODE, key);
byte[] res = ci.doFinal(data);
return res;
}
if (type == 0) {
ci.init(Cipher.DECRYPT_MODE, key);
byte[] res = ci.doFinal(data);
return res;
}
}
return null;
}
}
|
希望本文所述对大家java程序设计有所帮助。
原文链接:http://blog.csdn.net/cnsd_liuliu/article/details/52474062
相关文章
猜你喜欢
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 18
-
2025-05-29 55
-
2025-05-25 49
-
2025-05-25 97
-
2025-06-05 24
热门评论

