1package com.locator.encryption; 2 3import java.io.ByteArrayOutputStream; 4import java.security.Key; 5import java.security.KeyFactory; 6import java.security.KeyPair; 7import java.security.KeyPairGenerator; 8import java.security.PrivateKey; 9import java.security.PublicKey; 10import java.security.Signature; 11import java.security.interfaces.RSAPrivateKey; 12import java.security.interfaces.RSAPublicKey; 13import java.security.spec.PKCS8EncodedKeySpec; 14import java.security.spec.X509EncodedKeySpec; 15import java.util.HashMap; 16import java.util.Map; 17 18import javax.crypto.Cipher; 19 20/** 21 * <p> 22 * RSA公钥/私钥/签名工具包 23 * <p> 24 * 字符串格式的密钥在未在特殊说明情况下都为BASE64编码格式<br/> 25 * 由于非对称加密速度极其缓慢,一般文件不使用它来加密而是使用对称加密,<br/> 26 * 非对称加密算法可以用来对对称加密的密钥加密,这样保证密钥的安全也就保证了数据的安全 27 * </p> 28 * 29 */ 30public class RSAUtils{ 31 32 /** 33 * 加密算法RSA 34 */ 35 public static final String KEY_ALGORITHM = "RSA"; 36 37 /** 38 * 签名算法 39 */ 40 public static final String SIGNATURE_ALGORITHM = "MD5withRSA"; 41 42 /** 43 * 获取公钥的key 44 */ 45 private static final String PUBLIC_KEY = "LocatorPublicKey"; 46 47 /** 48 * 获取私钥的key 49 */ 50 private static final String PRIVATE_KEY = "LocatorPrivateKey"; 51 52 /** 53 * RSA最大加密明文大小 54 */ 55 private static final int MAX_ENCRYPT_BLOCK = 117; 56 57 /** 58 * RSA最大解密密文大小 59 */ 60 private static final int MAX_DECRYPT_BLOCK = 128; 61 62 /** 63 * <p> 64 * 生成密钥对(公钥和私钥) 65 * </p> 66 * 67 * @return 68 * @throws Exception 69 */ 70 public static Map<String, Object> genKeyPair() throws Exception { 71 KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); 72 keyPairGen.initialize(1024); 73 KeyPair keyPair = keyPairGen.generateKeyPair(); 74 RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); 75 RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); 76 Map<String, Object> keyMap = new HashMap<String, Object>(2); 77 keyMap.put(PUBLIC_KEY, publicKey); 78 keyMap.put(PRIVATE_KEY, privateKey); 79 return keyMap; 80 } 81 82 /** 83 * <p> 84 * 用私钥对信息生成数字签名 85 * </p> 86 * 87 * @param data 已加密数据 88 * @param privateKey 私钥(BASE64编码) 89 * 90 * @return 91 * @throws Exception 92 */ 93 public static String sign(byte[] data, String privateKey) throws Exception { 94 byte[] keyBytes = Base64Utils.decode(privateKey); 95 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); 96 KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); 97 PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec); 98 Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); 99 signature.initSign(privateK); 100 signature.update(data); 101 return Base64Utils.encode(signature.sign()); 102 } 103 104 /** 105 * <p> 106 * 校验数字签名 107 * </p> 108 * 109 * @param data 已加密数据 110 * @param publicKey 公钥(BASE64编码) 111 * @param sign 数字签名 112 * 113 * @return 114 * @throws Exception 115 * 116 */ 117 public static boolean verify(byte[] data, String publicKey, String sign) 118 throws Exception { 119 byte[] keyBytes = Base64Utils.decode(publicKey); 120 X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); 121 KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); 122 PublicKey publicK = keyFactory.generatePublic(keySpec); 123 Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); 124 signature.initVerify(publicK); 125 signature.update(data); 126 return signature.verify(Base64Utils.decode(sign)); 127 } 128 129 /** 130 * <P> 131 * 私钥解密 132 * </p> 133 * 134 * @param encryptedData 已加密数据 135 * @param privateKey 私钥(BASE64编码) 136 * @return 137 * @throws Exception 138 */ 139 public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) 140 throws Exception { 141 byte[] keyBytes = Base64Utils.decode(privateKey); 142 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); 143 KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); 144 Key privateK = keyFactory.generatePrivate(pkcs8KeySpec); 145 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); 146 cipher.init(Cipher.DECRYPT_MODE, privateK); 147 int inputLen = encryptedData.length; 148 ByteArrayOutputStream out = new ByteArrayOutputStream(); 149 int offSet = 0; 150 byte[] cache; 151 int i = 0; 152 // 对数据分段解密 153 while (inputLen - offSet > 0) { 154 if (inputLen - offSet > MAX_DECRYPT_BLOCK) { 155 cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); 156 } else { 157 cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet); 158 } 159 out.write(cache, 0, cache.length); 160 i++; 161 offSet = i * MAX_DECRYPT_BLOCK; 162 } 163 byte[] decryptedData = out.toByteArray(); 164 out.close(); 165 return decryptedData; 166 } 167 168 /** 169 * <p> 170 * 公钥解密 171 * </p> 172 * 173 * @param encryptedData 已加密数据 174 * @param publicKey 公钥(BASE64编码) 175 * @return 176 * @throws Exception 177 */ 178 public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) 179 throws Exception { 180 byte[] keyBytes = Base64Utils.decode(publicKey); 181 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); 182 KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); 183 Key publicK = keyFactory.generatePublic(x509KeySpec); 184 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); 185 cipher.init(Cipher.DECRYPT_MODE, publicK); 186 int inputLen = encryptedData.length; 187 ByteArrayOutputStream out = new ByteArrayOutputStream(); 188 int offSet = 0; 189 byte[] cache; 190 int i = 0; 191 // 对数据分段解密 192 while (inputLen - offSet > 0) { 193 if (inputLen - offSet > MAX_DECRYPT_BLOCK) { 194 cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); 195 } else { 196 cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet); 197 } 198 out.write(cache, 0, cache.length); 199 i++; 200 offSet = i * MAX_DECRYPT_BLOCK; 201 } 202 byte[] decryptedData = out.toByteArray(); 203 out.close(); 204 return decryptedData; 205 } 206 207 /** 208 * <p> 209 * 公钥加密 210 * </p> 211 * 212 * @param data 源数据 213 * @param publicKey 公钥(BASE64编码) 214 * @return 215 * @throws Exception 216 */ 217 public static byte[] encryptByPublicKey(byte[] data, String publicKey) 218 throws Exception { 219 byte[] keyBytes = Base64Utils.decode(publicKey); 220 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); 221 KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); 222 Key publicK = keyFactory.generatePublic(x509KeySpec); 223 // 对数据加密 224 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); 225 cipher.init(Cipher.ENCRYPT_MODE, publicK); 226 int inputLen = data.length; 227 ByteArrayOutputStream out = new ByteArrayOutputStream(); 228 int offSet = 0; 229 byte[] cache; 230 int i = 0; 231 // 对数据分段加密 232 while (inputLen - offSet > 0) { 233 if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { 234 cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK); 235 } else { 236 cache = cipher.doFinal(data, offSet, inputLen - offSet); 237 } 238 out.write(cache, 0, cache.length); 239 i++; 240 offSet = i * MAX_ENCRYPT_BLOCK; 241 } 242 byte[] encryptedData = out.toByteArray(); 243 out.close(); 244 return encryptedData; 245 } 246 247 /** 248 * <p> 249 * 私钥加密 250 * </p> 251 * 252 * @param data 源数据 253 * @param privateKey 私钥(BASE64编码) 254 * @return 255 * @throws Exception 256 */ 257 public static byte[] encryptByPrivateKey(byte[] data, String privateKey) 258 throws Exception { 259 byte[] keyBytes = Base64Utils.decode(privateKey); 260 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); 261 KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); 262 Key privateK = keyFactory.generatePrivate(pkcs8KeySpec); 263 Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); 264 cipher.init(Cipher.ENCRYPT_MODE, privateK); 265 int inputLen = data.length; 266 ByteArrayOutputStream out = new ByteArrayOutputStream(); 267 int offSet = 0; 268 byte[] cache; 269 int i = 0; 270 // 对数据分段加密 271 while (inputLen - offSet > 0) { 272 if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { 273 cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK); 274 } else { 275 cache = cipher.doFinal(data, offSet, inputLen - offSet); 276 } 277 out.write(cache, 0, cache.length); 278 i++; 279 offSet = i * MAX_ENCRYPT_BLOCK; 280 } 281 byte[] encryptedData = out.toByteArray(); 282 out.close(); 283 return encryptedData; 284 } 285 286 /** 287 * <p> 288 * 获取私钥 289 * </p> 290 * 291 * @param keyMap 密钥对 292 * @return 293 * @throws Exception 294 */ 295 public static String getPrivateKey(Map<String, Object> keyMap) 296 throws Exception { 297 Key key = (Key) keyMap.get(PRIVATE_KEY); 298 return Base64Utils.encode(key.getEncoded()); 299 } 300 301 /** 302 * <p> 303 * 获取公钥 304 * </p> 305 * 306 * @param keyMap 密钥对 307 * @return 308 * @throws Exception 309 */ 310 public static String getPublicKey(Map<String, Object> keyMap) 311 throws Exception { 312 Key key = (Key) keyMap.get(PUBLIC_KEY); 313 return Base64Utils.encode(key.getEncoded()); 314 } 315 316}
Java公钥私钥签名工具包
Wesley13
2021-10-11
1870 0 0
点赞
收藏
评论区
加载中...