下述代码中:PUBLIC_KEY 和 PRIVATE_KEY 可自己生成后替换即可。
1package com.bing.security.util; 2 3 4 5import org.apache.commons.codec.binary.Hex; 6 7import javax.crypto.Cipher; 8import java.io.ByteArrayOutputStream; 9import java.security.*; 10import java.security.spec.PKCS8EncodedKeySpec; 11import java.security.spec.X509EncodedKeySpec; 12import java.util.Base64; 13 14/** 15 * @package:com.bing.security.util 16 * @des: 数据加密解密工具类 17 * @autor :Ryan Wang 18 * @createTime: 2018/2/9-11:29 19 */ 20public class SecurityUtil { 21 22 /** 23 * 对数据加密生成16进制字符串 24 * @param data 要处理的字符串 25 */ 26 public static String encryptDataAsHex(String data) { 27 byte[] bytes = encryptData(data); 28 return Hex.encodeHexString(bytes).toUpperCase(); 29 } 30 31 /** 32 * 对数据加密并进行Base64转码 33 * @param data 要处理的字符串 34 */ 35 public static String encryptDataAsEncode(String data) { 36 byte[] bytes = encryptData(data); 37 return Base64.getEncoder().encodeToString(bytes); 38 } 39 40 /** 41 * 将16进制的字符串进行解密 42 * @param data 43 */ 44 public static String decryptDataFromHex(String data) { 45 if (data == null) { 46 return ""; 47 } 48 byte[] encryptData = hexStringToBytes(data); 49 return new String(decryptData(encryptData)); 50 } 51 52 /** 53 * 将base64编码的字符串进行解密 54 * @param data 55 */ 56 public static String decryptDataFromEncode(String data) { 57 if (data == null) { 58 return ""; 59 } 60 byte[] encryptData = Base64.getDecoder().decode(data); 61 return new String(decryptData(encryptData)); 62 } 63 64 private static byte[] decryptData(byte[] encryptData) { 65 ByteArrayOutputStream outputStream = null; 66 try { 67 Cipher cipher = Cipher.getInstance("RSA"); 68 cipher.init(Cipher.DECRYPT_MODE, getPrivateKey()); 69 int length = encryptData.length; 70 int offset = 0; 71 int i = 0; 72 byte[] cache; 73 outputStream = new ByteArrayOutputStream(); 74 while (length - offset > 0) { 75 /*最大解密字节长度*/ 76 Integer MAX_DECRYPT_BLOCK = 128; 77 if (length - offset > MAX_DECRYPT_BLOCK) { 78 cache = cipher.doFinal(encryptData, offset, MAX_DECRYPT_BLOCK); 79 } else { 80 cache = cipher.doFinal(encryptData, offset, length - offset); 81 } 82 outputStream.write(cache, 0, cache.length); 83 offset = (++i) * MAX_DECRYPT_BLOCK; 84 } 85 outputStream.close(); 86 } catch (Exception e) { 87 e.printStackTrace(); 88 } 89 return outputStream != null ? outputStream.toByteArray() : new byte[0]; 90 } 91 92 private static byte[] encryptData(String data) { 93 if (data == null) { 94 return new byte[0]; 95 } 96 ByteArrayOutputStream outputStream; 97 try { 98 Cipher cipher = Cipher.getInstance("RSA"); 99 cipher.init(Cipher.ENCRYPT_MODE, getPublicKey()); 100 int length = data.getBytes().length; 101 int offset = 0; 102 int i = 0; 103 byte[] cache; 104 outputStream = new ByteArrayOutputStream(); 105 while (length - offset > 0) { 106 /*最大加密字节长度*/ 107 Integer MAX_ENCRYPT_BLOCK = 117; 108 if (length - offset > MAX_ENCRYPT_BLOCK) { 109 cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK); 110 } else { 111 cache = cipher.doFinal(data.getBytes(), offset, length - offset); 112 } 113 outputStream.write(cache, 0, cache.length); 114 offset = (++i) * MAX_ENCRYPT_BLOCK; 115 } 116 outputStream.close(); 117 } catch (Exception e) { 118 return new byte[0]; 119 } 120 return outputStream.toByteArray(); 121 } 122 123 /** 124 * 将16进制的字符串转为byte[] 125 * @param hexString 126 */ 127 private static byte[] hexStringToBytes(String hexString) { 128 if (hexString == null) { 129 return null; 130 } 131 hexString = hexString.toUpperCase(); 132 int length = hexString.length() / 2; 133 char[] hexChars = hexString.toCharArray(); 134 byte[] bytes = new byte[length]; 135 for (int i =0;i<length;i++) { 136 int pos = i * 2; 137 bytes[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1])); 138 } 139 return bytes; 140 } 141 142 private static byte charToByte(char c) { 143 return (byte) "0123456789ABCDEF".indexOf(c); 144 } 145 146 /** 147 * 获取RSA public key 148 */ 149 private static PublicKey getPublicKey() throws Exception { 150 X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(Constant.PUBLIC_KEY_STR)); 151 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 152 return keyFactory.generatePublic(keySpec); 153 } 154 155 /** 156 * 获取RSA private key 157 */ 158 private static PrivateKey getPrivateKey() throws Exception { 159 PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(Constant.PRIVATE_KEY_STR)); 160 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 161 return keyFactory.generatePrivate(keySpec); 162 } 163}