16 | * BASE64编码解码工具包 17 | *
18 | *19 | * 依赖javabase64-1.3.1.jar 20 | *
21 | * 22 | * @author IceWee 23 | * @date 2012-5-19 24 | * @version 1.0 25 | */ 26 | public class Base64Utils { 27 | 28 | /** 29 | * 文件读取缓冲区大小 30 | */ 31 | private static final int CACHE_SIZE = 1024; 32 | 33 | /** 34 | *35 | * BASE64字符串解码为二进制数据 36 | *
37 | * 38 | * @param base64 39 | * @return 40 | * @throws Exception 41 | */ 42 | public static byte[] decode(String base64) throws Exception { 43 | return Base64.decode(base64.getBytes()); 44 | } 45 | 46 | /** 47 | *48 | * 二进制数据编码为BASE64字符串 49 | *
50 | * 51 | * @param bytes 52 | * @return 53 | * @throws Exception 54 | */ 55 | public static String encode(byte[] bytes) throws Exception { 56 | return new String(Base64.encode(bytes)); 57 | } 58 | 59 | /** 60 | *61 | * 将文件编码为BASE64字符串 62 | *
63 | *64 | * 大文件慎用,可能会导致内存溢出 65 | *
66 | * 67 | * @param filePath 68 | * 文件绝对路径 69 | * @return 70 | * @throws Exception 71 | */ 72 | public static String encodeFile(String filePath) throws Exception { 73 | byte[] bytes = fileToByte(filePath); 74 | return encode(bytes); 75 | } 76 | 77 | /** 78 | *79 | * BASE64字符串转回文件 80 | *
81 | * 82 | * @param filePath 83 | * 文件绝对路径 84 | * @param base64 85 | * 编码字符串 86 | * @throws Exception 87 | */ 88 | public static void decodeToFile(String filePath, String base64) throws Exception { 89 | byte[] bytes = decode(base64); 90 | byteArrayToFile(bytes, filePath); 91 | } 92 | 93 | /** 94 | *95 | * 文件转换为二进制数组 96 | *
97 | * 98 | * @param filePath 99 | * 文件路径 100 | * @return 101 | * @throws Exception 102 | */ 103 | public static byte[] fileToByte(String filePath) throws Exception { 104 | byte[] data = new byte[0]; 105 | File file = new File(filePath); 106 | if (file.exists()) { 107 | FileInputStream in = new FileInputStream(file); 108 | ByteArrayOutputStream out = new ByteArrayOutputStream(2048); 109 | byte[] cache = new byte[CACHE_SIZE]; 110 | int nRead = 0; 111 | while ((nRead = in.read(cache)) != -1) { 112 | out.write(cache, 0, nRead); 113 | out.flush(); 114 | } 115 | out.close(); 116 | in.close(); 117 | data = out.toByteArray(); 118 | } 119 | return data; 120 | } 121 | 122 | /** 123 | *124 | * 二进制数据写文件 125 | *
126 | * 127 | * @param bytes 128 | * 二进制数据 129 | * @param filePath 130 | * 文件生成目录 131 | */ 132 | public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception { 133 | InputStream in = new ByteArrayInputStream(bytes); 134 | File destFile = new File(filePath); 135 | if (!destFile.getParentFile().exists()) { 136 | destFile.getParentFile().mkdirs(); 137 | } 138 | destFile.createNewFile(); 139 | OutputStream out = new FileOutputStream(destFile); 140 | byte[] cache = new byte[CACHE_SIZE]; 141 | int nRead = 0; 142 | while ((nRead = in.read(cache)) != -1) { 143 | out.write(cache, 0, nRead); 144 | out.flush(); 145 | } 146 | out.close(); 147 | in.close(); 148 | } 149 | 150 | } -------------------------------------------------------------------------------- /src/utils/DowloadFileUtil.java: -------------------------------------------------------------------------------- 1 | package utils; 2 | 3 | import java.io.BufferedInputStream; 4 | import java.io.File; 5 | import java.io.FileInputStream; 6 | import java.io.FileOutputStream; 7 | import java.io.OutputStream; 8 | import java.net.URL; 9 | import java.net.URLConnection; 10 | 11 | import javax.servlet.http.HttpServletRequest; 12 | import javax.servlet.http.HttpServletResponse; 13 | 14 | public class DowloadFileUtil { 15 | 16 | public static void _downLoad(String filePath, HttpServletRequest request, HttpServletResponse response, boolean isOnLine) throws Exception { 17 | BufferedInputStream br = null; 18 | OutputStream out = null; 19 | try { 20 | File f = new File(filePath); 21 | System.out.println("filePath: " + filePath); 22 | if (!f.exists()) { 23 | System.out.println("File not found!"); 24 | response.sendError(404, "File not found!"); 25 | return; 26 | } 27 | br = new BufferedInputStream(new FileInputStream(f)); 28 | byte[] buf = new byte[1024]; 29 | int len = 0; 30 | 31 | String userAgent = request.getHeader("User-Agent"); 32 | String fileName = null; 33 | 34 | // 针对IE或者以IE为内核的浏览器: 35 | if (userAgent.contains("MSIE") || userAgent.contains("Trident")) { 36 | fileName = java.net.URLEncoder.encode(f.getName(), "UTF-8"); 37 | } else { 38 | // 非IE浏览器的处理: 39 | fileName = new String(f.getName().getBytes("UTF-8"), "ISO-8859-1"); 40 | } 41 | 42 | response.reset(); // 非常重要 43 | if (isOnLine) { // 在线打开方式 44 | URL u = new URL("file:///" + filePath); 45 | response.setContentType(u.openConnection().getContentType()); 46 | response.setHeader("Content-Disposition", "inline; filename=" + fileName); 47 | // 文件名应该编码成UTF-8 48 | } else { // 纯下载方式 49 | response.setContentType("application/x-msdownload"); 50 | response.setHeader("Content-Disposition", "attachment; filename=" + fileName); 51 | } 52 | out = response.getOutputStream(); 53 | while ((len = br.read(buf)) > 0) 54 | out.write(buf, 0, len); 55 | }catch(Exception e) { 56 | e.printStackTrace(); 57 | }finally { 58 | if(br != null) { 59 | br.close(); 60 | } 61 | if(out != null) { 62 | out.close(); 63 | } 64 | } 65 | 66 | } 67 | 68 | public static void downloadNet(HttpServletRequest request, HttpServletResponse response, String fileUrl) throws Exception { 69 | // 下载网络文件 70 | int bytesum = 0; 71 | int byteread = 0; 72 | BufferedInputStream br = null; 73 | OutputStream out = null; 74 | 75 | try { 76 | String fName = fileUrl.substring(fileUrl.lastIndexOf("/")+1); 77 | String userAgent = request.getHeader("User-Agent"); 78 | String fileName = null; 79 | // 针对IE或者以IE为内核的浏览器: 80 | if (userAgent.contains("MSIE") || userAgent.contains("Trident")) { 81 | fileName = java.net.URLEncoder.encode(fName, "UTF-8"); 82 | } else { 83 | // 非IE浏览器的处理: 84 | fileName = new String(fName.getBytes("UTF-8"), "ISO-8859-1"); 85 | } 86 | 87 | response.reset(); // 非常重要 88 | response.setContentType("application/x-msdownload"); 89 | response.setHeader("Content-Disposition", "attachment; filename=" + fileName); 90 | 91 | URL url = new URL(fileUrl); 92 | URLConnection conn = url.openConnection(); 93 | br = new BufferedInputStream(conn.getInputStream()); 94 | out = response.getOutputStream(); 95 | 96 | byte[] buffer = new byte[1204]; 97 | while ((byteread = br.read(buffer)) != -1) { 98 | bytesum += byteread; 99 | System.out.println(bytesum); 100 | out.write(buffer, 0, byteread); 101 | } 102 | } catch (Exception e) { 103 | e.printStackTrace(); 104 | } finally { 105 | if(br != null) { 106 | br.close(); 107 | } 108 | if(out != null) { 109 | out.close(); 110 | } 111 | } 112 | } 113 | 114 | public static void downLoad(String filePath) throws Exception { 115 | BufferedInputStream br = null; 116 | OutputStream out = null; 117 | try { 118 | File f = new File(filePath); 119 | if (!f.exists()) { 120 | System.out.println("File not found!"); 121 | return; 122 | } 123 | br = new BufferedInputStream(new FileInputStream(f)); 124 | byte[] buf = new byte[1024]; 125 | int len = 0; 126 | int start = filePath.lastIndexOf("/"); 127 | String fileName = filePath.substring(start); 128 | System.out.println("--------"+fileName); 129 | File FILE_FOR_WRITE = new File("D:/RSE/publickey/"+fileName); 130 | out = new FileOutputStream(FILE_FOR_WRITE); 131 | while ((len = br.read(buf)) > 0) 132 | out.write(buf, 0, len); 133 | }catch(Exception e) { 134 | e.printStackTrace(); 135 | }finally { 136 | if(br != null) { 137 | br.close(); 138 | } 139 | if(out != null) { 140 | out.close(); 141 | } 142 | System.out.println("文件下载成功"); 143 | } 144 | 145 | } 146 | 147 | } 148 | -------------------------------------------------------------------------------- /src/utils/MD5Util.java: -------------------------------------------------------------------------------- 1 | package utils; 2 | 3 | import java.security.MessageDigest; 4 | 5 | public class MD5Util { 6 | 7 | public static void main(String[] args) { 8 | System.out.println(MD5Encode("aaa","UTF-8")); 9 | } 10 | 11 | private static String byteArrayToHexString(byte b[]) { 12 | StringBuffer resultSb = new StringBuffer(); 13 | for (int i = 0; i < b.length; i++) 14 | resultSb.append(byteToHexString(b[i])); 15 | 16 | return resultSb.toString(); 17 | } 18 | 19 | private static String byteToHexString(byte b) { 20 | int n = b; 21 | if (n < 0) 22 | n += 256; 23 | int d1 = n / 16; 24 | int d2 = n % 16; 25 | return hexDigits[d1] + hexDigits[d2]; 26 | } 27 | 28 | public static String MD5Encode(String origin, String charsetname) { 29 | String resultString = null; 30 | try { 31 | resultString = new String(origin); 32 | MessageDigest md = MessageDigest.getInstance("MD5"); 33 | if (charsetname == null || "".equals(charsetname)) 34 | resultString = byteArrayToHexString(md.digest(resultString 35 | .getBytes())); 36 | else 37 | resultString = byteArrayToHexString(md.digest(resultString 38 | .getBytes(charsetname))); 39 | } catch (Exception exception) { 40 | } 41 | return resultString; 42 | } 43 | 44 | private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5", 45 | "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; 46 | } 47 | -------------------------------------------------------------------------------- /src/utils/MoveFile.java: -------------------------------------------------------------------------------- 1 | package utils; 2 | 3 | import java.io.File; 4 | 5 | public class MoveFile { 6 | public static boolean removeFile(String fileName,String destinationFloderUrl,String destFileName) 7 | { 8 | File file = new File(fileName); 9 | File destFloder = new File(destinationFloderUrl); 10 | //检查目标路径是否合法 11 | if(destFloder.exists()) 12 | { 13 | if(destFloder.isFile()) 14 | { 15 | System.out.println("目标路径是个文件,请检查目标路径!"); 16 | return false; 17 | } 18 | }else 19 | { 20 | if(!destFloder.mkdirs()) 21 | { 22 | System.out.println("目标文件夹不存在,创建失败!"); 23 | return false; 24 | } 25 | } 26 | //检查源文件是否合法 27 | if(file.isFile() &&file.exists()) 28 | { 29 | // long now=System.currentTimeMillis(); 30 | // String time = Utils.sdf(now); 31 | String destinationFile = destinationFloderUrl+destFileName; 32 | if(!file.renameTo(new File(destinationFile))) 33 | { 34 | System.out.println("移动文件失败!"); 35 | return false; 36 | } 37 | }else 38 | { 39 | System.out.println("要备份的文件路径不正确,移动失败!"); 40 | return false; 41 | } 42 | System.out.println("已成功移动文件"+file.getName()+"到"+destinationFloderUrl); 43 | return true; 44 | } 45 | 46 | public static void main(String[] argv) { 47 | String basePath = "/home/user0308/Tmp/tmp/"; 48 | String destPath = basePath + "contract/"; 49 | String sourceFilePath = basePath + "/orgA/" + "temContract"; 50 | 51 | removeFile(sourceFilePath,destPath,"new_file"); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/utils/MySessionContext.java: -------------------------------------------------------------------------------- 1 | package utils; 2 | 3 | import java.util.HashMap; 4 | 5 | import javax.servlet.http.HttpSession; 6 | 7 | public class MySessionContext { 8 | private static HashMap mymap = new HashMap(); 9 | 10 | public static synchronized void AddSession(HttpSession session) { 11 | if (session != null) { 12 | mymap.put(session.getId(), session); 13 | } 14 | } 15 | 16 | public static synchronized void DelSession(HttpSession session) { 17 | if (session != null) { 18 | mymap.remove(session.getId()); 19 | } 20 | } 21 | 22 | public static synchronized HttpSession getSession(String session_id) { 23 | if (session_id == null) 24 | return null; 25 | return (HttpSession) mymap.get(session_id); 26 | } 27 | } -------------------------------------------------------------------------------- /src/utils/MySessionListener.java: -------------------------------------------------------------------------------- 1 | package utils; 2 | 3 | import javax.servlet.http.HttpSession; 4 | import javax.servlet.http.HttpSessionEvent; 5 | import javax.servlet.http.HttpSessionListener; 6 | 7 | public class MySessionListener implements HttpSessionListener { 8 | public void sessionCreated(HttpSessionEvent httpSessionEvent) { 9 | MySessionContext.AddSession(httpSessionEvent.getSession()); 10 | } 11 | 12 | public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { 13 | HttpSession session = httpSessionEvent.getSession(); 14 | MySessionContext.DelSession(session); 15 | } 16 | 17 | } -------------------------------------------------------------------------------- /src/utils/PropertiesUtil.java: -------------------------------------------------------------------------------- 1 | package utils; 2 | 3 | import java.io.FileNotFoundException; 4 | import java.io.FileOutputStream; 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.io.OutputStream; 8 | import java.util.ArrayList; 9 | import java.util.Enumeration; 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | import java.util.Properties; 13 | 14 | public class PropertiesUtil { 15 | 16 | 17 | /** 18 | * 配置文件对象 19 | */ 20 | private static Properties props=new Properties(); 21 | 22 | /** 23 | * 默认构造函数,用于sh运行,自动找到classpath下的config.properties。 24 | */ 25 | 26 | public PropertiesUtil(){ 27 | 28 | } 29 | 30 | public static void readFile(String path) throws IOException{ 31 | /*String classPath = Thread.currentThread().getContextClassLoader().getResource("").toString(); 32 | String subClassPath = classPath.substring(5, path.length()); 33 | String os = System.getProperty("os.name"); 34 | if(os.toLowerCase().startsWith("win")){ 35 | } 36 | else { 37 | }*/ 38 | InputStream in = PropertiesUtil.class.getClassLoader().getResourceAsStream(path); 39 | // props = new Properties(); 40 | props.load(in); 41 | //关闭资源 42 | in.close(); 43 | } 44 | 45 | /** 46 | * 根据key值读取配置的值 47 | * @param key key值 48 | * @return key 键对应的值 49 | * @throws IOException 50 | */ 51 | public static String readValue(String key) throws IOException { 52 | 53 | return props.getProperty(key); 54 | } 55 | 56 | 57 | 58 | /** 59 | * 根据key值,value值写入配置 60 | * @param key key值; value value值 61 | * @return void 62 | * @throws IOException 63 | */ 64 | public static void writeValue(String key, String value, String path) throws IOException { 65 | 66 | Properties writeProps = new Properties(); 67 | writeProps.setProperty(key, value); 68 | FileOutputStream out = new FileOutputStream(path); 69 | writeProps.store(out, null); 70 | //关闭资源 71 | out.close(); 72 | } 73 | 74 | public static void writeValues(ArrayList24 | * RSA公钥/私钥/签名工具包 25 | *
26 | *27 | * 罗纳德·李维斯特(Ron [R]ivest)、阿迪·萨莫尔(Adi [S]hamir)和伦纳德·阿德曼(Leonard [A]dleman) 28 | *
29 | *
30 | * 字符串格式的密钥在未在特殊说明情况下都为BASE64编码格式
31 | * 由于非对称加密速度极其缓慢,一般文件不使用它来加密而是使用对称加密,
32 | * 非对称加密算法可以用来对对称加密的密钥加密,这样保证密钥的安全也就保证了数据的安全
33 | *
73 | * 生成密钥对(公钥和私钥) 74 | *
75 | * 76 | * @return 77 | * @throws Exception 78 | */ 79 | public static Map93 | * 用私钥对信息生成数字签名 94 | *
95 | * 96 | * @param data 已加密数据 97 | * @param privateKey 私钥(BASE64编码) 98 | * 99 | * @return 100 | * @throws Exception 101 | */ 102 | public static String sign(byte[] data, String privateKey) throws Exception { 103 | byte[] keyBytes = Base64Utils.decode(privateKey); 104 | PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); 105 | KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); 106 | PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec); 107 | Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); 108 | signature.initSign(privateK); 109 | signature.update(data); 110 | return Base64Utils.encode(signature.sign()); 111 | } 112 | 113 | /** 114 | *115 | * 校验数字签名 116 | *
117 | * 118 | * @param data 已加密数据 119 | * @param publicKey 公钥(BASE64编码) 120 | * @param sign 数字签名 121 | * 122 | * @return 123 | * @throws Exception 124 | * 125 | */ 126 | public static boolean verify(byte[] data, String publicKey, String sign) 127 | throws Exception { 128 | byte[] keyBytes = Base64Utils.decode(publicKey); 129 | X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); 130 | KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); 131 | PublicKey publicK = keyFactory.generatePublic(keySpec); 132 | Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); 133 | signature.initVerify(publicK); 134 | signature.update(data); 135 | return signature.verify(Base64Utils.decode(sign)); 136 | } 137 | 138 | /** 139 | *140 | * 私钥解密 141 | *
142 | * 143 | * @param encryptedData 已加密数据 144 | * @param privateKey 私钥(BASE64编码) 145 | * @return 146 | * @throws Exception 147 | */ 148 | public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) 149 | throws Exception { 150 | encryptedData=Base64.getDecoder().decode(encryptedData); //字符串型密文转码成BASE64字符串 151 | byte[] keyBytes = Base64Utils.decode(privateKey); //BASE64-->二进制 152 | PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); 153 | KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); 154 | Key privateK = keyFactory.generatePrivate(pkcs8KeySpec); 155 | Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); 156 | cipher.init(Cipher.DECRYPT_MODE, privateK); 157 | int inputLen = encryptedData.length; 158 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 159 | int offSet = 0; 160 | byte[] cache; 161 | int i = 0; 162 | // 对数据分段解密 163 | while (inputLen - offSet > 0) { 164 | if (inputLen - offSet > MAX_DECRYPT_BLOCK) { 165 | cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); 166 | } else { 167 | cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet); 168 | } 169 | out.write(cache, 0, cache.length); 170 | i++; 171 | offSet = i * MAX_DECRYPT_BLOCK; 172 | } 173 | byte[] decryptedData = out.toByteArray(); 174 | out.close(); 175 | return decryptedData; 176 | } 177 | 178 | /** 179 | *180 | * 公钥解密 181 | *
182 | * 183 | * @param encryptedData 已加密数据 184 | * @param publicKey 公钥(BASE64编码) 185 | * @return 186 | * @throws Exception 187 | */ 188 | public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey) 189 | throws Exception { 190 | byte[] keyBytes = Base64Utils.decode(publicKey); 191 | X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); 192 | KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); 193 | Key publicK = keyFactory.generatePublic(x509KeySpec); 194 | Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); 195 | cipher.init(Cipher.DECRYPT_MODE, publicK); 196 | int inputLen = encryptedData.length; 197 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 198 | int offSet = 0; 199 | byte[] cache; 200 | int i = 0; 201 | // 对数据分段解密 202 | while (inputLen - offSet > 0) { 203 | if (inputLen - offSet > MAX_DECRYPT_BLOCK) { 204 | cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); 205 | } else { 206 | cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet); 207 | } 208 | out.write(cache, 0, cache.length); 209 | i++; 210 | offSet = i * MAX_DECRYPT_BLOCK; 211 | } 212 | byte[] decryptedData = out.toByteArray(); 213 | out.close(); 214 | return decryptedData; 215 | } 216 | 217 | /** 218 | *219 | * 公钥加密 220 | *
221 | * 222 | * @param data 源数据 223 | * @param publicKey 公钥(BASE64编码) 224 | * @return 225 | * @throws Exception 226 | */ 227 | public static byte[] encryptByPublicKey(byte[] data, String publicKey) 228 | throws Exception { 229 | byte[] keyBytes = Base64Utils.decode(publicKey); 230 | X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); 231 | KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); 232 | Key publicK = keyFactory.generatePublic(x509KeySpec); 233 | // 对数据加密 234 | Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); 235 | cipher.init(Cipher.ENCRYPT_MODE, publicK); 236 | int inputLen = data.length; 237 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 238 | int offSet = 0; 239 | byte[] cache; 240 | int i = 0; 241 | // 对数据分段加密 242 | while (inputLen - offSet > 0) { 243 | if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { 244 | cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK); 245 | } else { 246 | cache = cipher.doFinal(data, offSet, inputLen - offSet); 247 | } 248 | out.write(cache, 0, cache.length); 249 | i++; 250 | offSet = i * MAX_ENCRYPT_BLOCK; 251 | } 252 | byte[] encryptedData = out.toByteArray(); 253 | out.close(); 254 | return Base64.getEncoder().encode(encryptedData); //密文转换成base64编码 255 | } 256 | 257 | /** 258 | *259 | * 私钥加密 260 | *
261 | * 262 | * @param data 源数据 263 | * @param privateKey 私钥(BASE64编码) 264 | * @return 265 | * @throws Exception 266 | */ 267 | public static byte[] encryptByPrivateKey(byte[] data, String privateKey) 268 | throws Exception { 269 | byte[] keyBytes = Base64Utils.decode(privateKey); 270 | PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); 271 | KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); 272 | Key privateK = keyFactory.generatePrivate(pkcs8KeySpec); 273 | Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); 274 | cipher.init(Cipher.ENCRYPT_MODE, privateK); 275 | int inputLen = data.length; 276 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 277 | int offSet = 0; 278 | byte[] cache; 279 | int i = 0; 280 | // 对数据分段加密 281 | while (inputLen - offSet > 0) { 282 | if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { 283 | cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK); 284 | } else { 285 | cache = cipher.doFinal(data, offSet, inputLen - offSet); 286 | } 287 | out.write(cache, 0, cache.length); 288 | i++; 289 | offSet = i * MAX_ENCRYPT_BLOCK; 290 | } 291 | byte[] encryptedData = out.toByteArray(); 292 | out.close(); 293 | return encryptedData; 294 | } 295 | 296 | /** 297 | *298 | * 获取私钥 299 | *
300 | * 301 | * @param keyMap 密钥对 302 | * @return 303 | * @throws Exception 304 | */ 305 | public static String getPrivateKey(Map313 | * 获取公钥 314 | *
315 | * 316 | * @param keyMap 密钥对 317 | * @return 318 | * @throws Exception 319 | */ 320 | public static String getPublicKey(Map328 | * 公私钥对写入文件 329 | *
330 | * 331 | * @param keyMap 密钥对 pubKeyPath 公钥地址 priKeyPath 私钥地址 332 | * @return 333 | * @throws Exception 334 | * @author wangbin 335 | */ 336 | public static String keyFiles(Map94 | * 读取文件 95 | * 读取公私钥文件用于后续的加解密 96 | *
97 | * 98 | * @param filePath文件路径 99 | * @return 100 | * @throws Exception 101 | * @author wangbin 102 | */ 103 | public static String fileRead(String filePath) throws Exception { 104 | File file = new File(filePath); //定义一个file对象,用来初始化FileReader 105 | FileReader reader = new FileReader(file); //定义一个fileReader对象,用来初始化BufferedReader 106 | BufferedReader bReader = new BufferedReader(reader); //new一个BufferedReader对象,将文件内容读取到缓存 107 | StringBuilder sb = new StringBuilder(); //定义一个字符串缓存,将字符串存放缓存中 108 | String s = ""; 109 | while ((s =bReader.readLine()) != null) { //逐行读取文件内容,不读取换行符和末尾的空格 110 | sb.append(s); //将读取的字符串添加换行符后累加存放在缓存中 111 | } 112 | bReader.close(); 113 | String str = sb.toString(); 114 | return str; 115 | } 116 | 117 | 118 | /** 119 | * simpleDateFormat统一格式化时间 120 | */ 121 | public static String sdf(Object object) { 122 | SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 123 | return simpleDateFormat.format(object); 124 | } 125 | 126 | } 127 | --------------------------------------------------------------------------------