RSA 数据加密解密

 下述代码中: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}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

swap空间的增减方法

(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap