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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
| package com.kimeng.weipan.utils;
import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.util.encoders.Base64;
import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.Security; import java.security.Signature; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.HashMap; import java.util.Map;
import javax.crypto.Cipher;
public class RSAUtil {
private final static String KEY_RSA = "RSA";
private final static String KEY_RSA_SIGNATURE = "MD5withRSA";
private final static String KEY_RSA_PUBLICKEY = "RSAPublicKey";
private final static String KEY_RSA_PRIVATEKEY = "RSAPrivateKey";
static { Security.addProvider(new BouncyCastleProvider()); }
public static Map<String, Object> init() { Map<String, Object> map = null; try { KeyPairGenerator generator = KeyPairGenerator.getInstance(KEY_RSA); generator.initialize(2048); KeyPair keyPair = generator.generateKeyPair(); RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); map = new HashMap<>(); map.put(KEY_RSA_PUBLICKEY, publicKey); map.put(KEY_RSA_PRIVATEKEY, privateKey); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return map; }
public static byte[] encryptByPublicKey(String data, String key) { byte[] result = null; try { byte[] bytes = decryptBase64(key); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes); KeyFactory factory = KeyFactory.getInstance(KEY_RSA); PublicKey publicKey = factory.generatePublic(keySpec); Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC");
cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] encode = cipher.doFinal(data.getBytes()); result = Base64.encode(encode); } catch (Exception e) { e.printStackTrace(); } return result; }
public static String decryptByPrivateKey(byte[] data, String key) { String result = null; try { byte[] bytes = decryptBase64(key); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes); KeyFactory factory = KeyFactory.getInstance(KEY_RSA); PrivateKey privateKey = factory.generatePrivate(keySpec); Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] decoded = Base64.decode(data); result = new String(cipher.doFinal(decoded)); } catch (Exception e) { e.printStackTrace(); } return result; }
public static String getPublicKey(Map<String, Object> map) { String str = ""; try { Key key = (Key) map.get(KEY_RSA_PUBLICKEY); str = encryptBase64(key.getEncoded()); } catch (Exception e) { e.printStackTrace(); } return str; }
public static String getPrivateKey(Map<String, Object> map) { String str = ""; try { Key key = (Key) map.get(KEY_RSA_PRIVATEKEY); str = encryptBase64(key.getEncoded()); } catch (Exception e) { e.printStackTrace(); } return str; }
public static String sign(byte[] data, String privateKey) { String str = ""; try { byte[] bytes = decryptBase64(privateKey); PKCS8EncodedKeySpec pkcs = new PKCS8EncodedKeySpec(bytes); KeyFactory factory = KeyFactory.getInstance(KEY_RSA); PrivateKey key = factory.generatePrivate(pkcs); Signature signature = Signature.getInstance(KEY_RSA_SIGNATURE); signature.initSign(key); signature.update(data); str = encryptBase64(signature.sign()); } catch (Exception e) { e.printStackTrace(); } return str; }
public static boolean verify(byte[] data, String publicKey, String sign) { boolean flag = false; try { byte[] bytes = decryptBase64(publicKey); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes); KeyFactory factory = KeyFactory.getInstance(KEY_RSA); PublicKey key = factory.generatePublic(keySpec); Signature signature = Signature.getInstance(KEY_RSA_SIGNATURE); signature.initVerify(key); signature.update(data); flag = signature.verify(decryptBase64(sign)); } catch (Exception e) { e.printStackTrace(); } return flag; }
public static byte[] decryptBase64(String key) throws Exception { return Base64.decode(key); }
public static String encryptBase64(byte[] key) throws Exception { return new String(Base64.encode(key)); }
public static void main(String[] args) throws Exception { String publicKey; String privateKey; Map<String, Object> keyMap = RSAUtil.init(); publicKey = RSAUtil.getPublicKey(keyMap); privateKey = RSAUtil.getPrivateKey(keyMap); System.out.println("公钥:\n\r" + publicKey); System.out.println("私钥:\n\r" + privateKey);
System.out.println("公钥加密======私钥解密"); String str = "会跳舞的机器人"; byte[] enStr = RSAUtil.encryptByPublicKey(str, publicKey); String decStr = RSAUtil.decryptByPrivateKey(enStr, privateKey); System.out.println("加密前:" + str + "\n\r解密后:" + decStr);
System.out.println("\n\r"); System.out.println("私钥签名======公钥验证"); String sign = RSAUtil.sign(str.getBytes(), privateKey); System.out.println("签名:\n\r" + sign); boolean flag = RSAUtil.verify(str.getBytes(), publicKey, sign); System.out.println("验签结果:\n\r" + flag); } }
|