释放双眼,带上耳机,听听看~!
非对称加密算法和对称加密算法的主要差别在于非对称加密算法用于加密和解密的密匙不同,一个公开称为公钥,一个保密称为私钥;这个算法解决了对称加密的算法的密匙分配的问题,提高了算法的安全性。
常见的非对称加密算法:RSA算法
1.RSA消息传递模型
2 RSA算法实现
3.使用Java提供的API实现
如下测试了用私钥加密,公钥解密的过程。反之也可以测试。
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 1import javax.crypto.Cipher;
2import java.security.*;
3import java.security.interfaces.RSAPrivateKey;
4import java.security.interfaces.RSAPublicKey;
5import java.security.spec.PKCS8EncodedKeySpec;
6import java.security.spec.X509EncodedKeySpec;
7import java.util.HashMap;
8import java.util.Map;
9
10public class RSATest {
11
12 private static final String KEY_ALGORITHM="RSA";
13 private static final String PUBLIC_KEY="RSAPublicKey";
14 private static final String PRIVATE_KEY="RSAPrivateKey";
15 /**
16 * RSA密匙长度,默认是1024位,密匙长度必须是在64的倍数
17 * 范围是512--65536之间
18 *
19 */
20 private static final int KEY_SIZE = 512;
21
22 public static void main(String[] args) throws Exception {
23
24 String str ="hello vison";
25 Map<String, Object> map = initKey();
26 byte[] pulicKey = getPulicKey(map);
27 byte[] privateKey = getPrivateKey(map);
28
29 byte[] dataAfterEncrypt = encryptByPrivateKey(str.getBytes(), privateKey);
30 System.out.println("encrypt by privateKey: " + Base64.encodeMD5Hex(new String(dataAfterEncrypt)));
31 byte[] dataAfterDecrpty = decryptByPublicKey(dataAfterEncrypt, pulicKey);
32 System.out.println("decrpt by publicKey :" + new String(dataAfterDecrpty));
33
34 }
35 /**
36 * 公钥解密
37 * @param data
38 * @param key
39 * @return
40 * @throws Exception
41 */
42 public static byte[] decryptByPublicKey(byte[] data,byte[] key)throws Exception{
43 //获取公钥
44 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(key);
45 KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
46 PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
47 //对数据解密
48 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
49 cipher.init(Cipher.DECRYPT_MODE,publicKey);
50 return cipher.doFinal(data);
51 }
52
53 /**
54 * 公钥加密
55 * @param data
56 * @param key
57 * @return
58 * @throws Exception
59 */
60 public static byte[] encryptByPublicKey(byte[] data,byte[] key)throws Exception{
61 //获取公钥
62 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(key);
63 KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
64 PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
65 //对数据加密
66 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
67 cipher.init(Cipher.ENCRYPT_MODE,publicKey);
68 return cipher.doFinal(data);
69 }
70
71 /**
72 * 私钥解密
73 * @param data
74 * @param key 私钥
75 * @return byte[] 返回解密后的数据
76 * @throws Exception
77 */
78 public static byte[] decryptByPrivateKey(byte[] data,byte[] key)throws Exception{
79 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(key);
80 KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
81 PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
82
83 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
84 cipher.init(Cipher.DECRYPT_MODE,privateKey);
85 return cipher.doFinal(data);
86
87 }
88 /**
89 * 私钥加密
90 * @param data 待加密数据
91 * @param key 私钥
92 * @return byte[] 加密数据
93 * @throws Exception
94 */
95 public static byte[] encryptByPrivateKey(byte[] data,byte[] key) throws Exception {
96 PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(key);
97 KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
98 PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
99
100 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
101 cipher.init(Cipher.ENCRYPT_MODE,privateKey);
102 return cipher.doFinal(data);
103 }
104
105
106 /**
107 * 获取私钥
108 * @param keyMap
109 * @return
110 */
111 public static byte[] getPrivateKey(Map<String,Object> keyMap){
112 Key key = (Key) keyMap.get(PRIVATE_KEY);
113 return key.getEncoded();
114 }
115 /**
116 * 获取公钥
117 * @param keyMap
118 * @return
119 */
120 public static byte[] getPulicKey(Map<String,Object> keyMap){
121 Key key = (Key) keyMap.get(PUBLIC_KEY);
122 return key.getEncoded();
123
124 }
125 /**
126 * 初始化密匙对
127 * @return Map 密钥map
128 * @throws Exception
129 */
130 public static Map<String,Object> initKey() throws Exception {
131 //实例化密钥对生成器
132 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM);
133 //初始化
134 keyPairGenerator.initialize(KEY_SIZE);
135 //生成密匙对
136 KeyPair keyPair = keyPairGenerator.genKeyPair();
137 //私钥
138 RSAPrivateKey privateKey = (RSAPrivateKey)keyPair.getPrivate();
139 //公钥
140 RSAPublicKey publicKey = (RSAPublicKey)keyPair.getPublic();
141 //封装密钥
142 HashMap<String, Object> map = new HashMap<>();
143 map.put(PUBLIC_KEY,publicKey);
144 map.put(PRIVATE_KEY,privateKey);
145 return map;
146 }
147}
148
149
150