1 package com.github.wxiaoqi.security.auth.common.util.jwt;
2
3 import sun.misc.BASE64Decoder;
4 import sun.misc.BASE64Encoder;
5
6 import java.io.DataInputStream;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.security.*;
11 import java.security.spec.PKCS8EncodedKeySpec;
12 import java.security.spec.X509EncodedKeySpec;
13 import java.util.HashMap;
14 import java.util.Map;
15
16 /**
17 * Created by ace on 2017/9/10.
18 */
19 public class RsaKeyHelper {
20 /**
21 * 获取公钥Base64
22 *
23 * @param filename
24 * @return
25 * @throws Exception
26 */
27 public PublicKey getPublicKey(String filename) throws Exception {
28 InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(filename);
29 DataInputStream dis = new DataInputStream(resourceAsStream);
30 byte[] keyBytes = new byte[resourceAsStream.available()];
31 dis.readFully(keyBytes);
32 dis.close();
33 X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
34 KeyFactory kf = KeyFactory.getInstance("RSA");
35 return kf.generatePublic(spec);
36 }
37
38 /**
39 * 获取密钥
40 *
41 * @param filename
42 * @return
43 * @throws Exception
44 */
45 public PrivateKey getPrivateKey(String filename) throws Exception {
46 InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(filename);
47 DataInputStream dis = new DataInputStream(resourceAsStream);
48 byte[] keyBytes = new byte[resourceAsStream.available()];
49 dis.readFully(keyBytes);
50 dis.close();
51 PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyBytes);
52 KeyFactory kf = KeyFactory.getInstance("RSA");
53 return kf.generatePrivate(spec);
54 }
55
56 /**
57 * 获取公钥
58 *
59 * @param publicKey
60 * @return
61 * @throws Exception
62 */
63 public PublicKey getPublicKey(byte[] publicKey) throws Exception {
64 X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKey);
65 KeyFactory kf = KeyFactory.getInstance("RSA");
66 return kf.generatePublic(spec);
67 }
68
69 /**
70 * 获取密钥
71 *
72 * @param privateKey
73 * @return
74 * @throws Exception
75 */
76 public PrivateKey getPrivateKey(byte[] privateKey) throws Exception {
77 PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKey);
78 KeyFactory kf = KeyFactory.getInstance("RSA");
79 return kf.generatePrivate(spec);
80 }
81
82 /**
83 * 生存rsa公钥和密钥
84 *
85 * @param publicKeyFilename
86 * @param privateKeyFilename
87 * @param password
88 * @throws IOException
89 * @throws NoSuchAlgorithmException
90 */
91 public void generateKey(String publicKeyFilename, String privateKeyFilename, String password) throws IOException, NoSuchAlgorithmException {
92 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
93 SecureRandom secureRandom = new SecureRandom(password.getBytes());
94 keyPairGenerator.initialize(1024, secureRandom);
95 KeyPair keyPair = keyPairGenerator.genKeyPair();
96 byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
97 FileOutputStream fos = new FileOutputStream(publicKeyFilename);
98 fos.write(publicKeyBytes);
99 fos.close();
100 byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();
101 fos = new FileOutputStream(privateKeyFilename);
102 fos.write(privateKeyBytes);
103 fos.close();
104 }
105
106 /**
107 * 生存rsa公钥
108 *
109 * @param password
110 * @throws IOException
111 * @throws NoSuchAlgorithmException
112 */
113 public static byte[] generatePublicKey(String password) throws IOException, NoSuchAlgorithmException {
114 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
115 SecureRandom secureRandom = new SecureRandom(password.getBytes());
116 keyPairGenerator.initialize(1024, secureRandom);
117 KeyPair keyPair = keyPairGenerator.genKeyPair();
118 return keyPair.getPublic().getEncoded();
119 }
120
121 /**
122 * 生存rsa公钥
123 *
124 * @param password
125 * @throws IOException
126 * @throws NoSuchAlgorithmException
127 */
128 public static byte[] generatePrivateKey(String password) throws IOException, NoSuchAlgorithmException {
129 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
130 SecureRandom secureRandom = new SecureRandom(password.getBytes());
131 keyPairGenerator.initialize(1024, secureRandom);
132 KeyPair keyPair = keyPairGenerator.genKeyPair();
133 return keyPair.getPrivate().getEncoded();
134 }
135
136 public static Map<String, byte[]> generateKey(String password) throws IOException, NoSuchAlgorithmException {
137 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
138 SecureRandom secureRandom = new SecureRandom(password.getBytes());
139 keyPairGenerator.initialize(1024, secureRandom);
140 KeyPair keyPair = keyPairGenerator.genKeyPair();
141 byte[] publicKeyBytes = keyPair.getPublic().getEncoded();
142 byte[] privateKeyBytes = keyPair.getPrivate().getEncoded();
143 Map<String, byte[]> map = new HashMap<String, byte[]>();
144 map.put("pub", publicKeyBytes);
145 map.put("pri", privateKeyBytes);
146 return map;
147 }
148
149 public static String toHexString(byte[] b) {
150 return (new BASE64Encoder()).encodeBuffer(b);
151 }
152
153 public static final byte[] toBytes(String s) throws IOException {
154 return (new BASE64Decoder()).decodeBuffer(s);
155 }
156
157 public static void main(String[] args) throws NoSuchAlgorithmException {
158 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
159 SecureRandom secureRandom = new SecureRandom("123".getBytes());
160 keyPairGenerator.initialize(1024, secureRandom);
161 KeyPair keyPair = keyPairGenerator.genKeyPair();
162 System.out.println(keyPair.getPublic().getEncoded());
163 }
164
165 }