本次实现直接使用了开源工具 hutool 自己看一下网址
http://hutool.mydoc.io/#text_319474
1、数字签名
可选签名算法
// The RSA signature algorithm
NONEwithRSA
// The MD2/MD5 with RSA Encryption signature algorithm
MD2withRSA
MD5withRSA
// The signature algorithm with SHA-* and the RSA
SHA1withRSA
SHA256withRSA
SHA384withRSA
SHA512withRSA
// The Digital Signature Algorithm
NONEwithDSA
// The DSA with SHA-1 signature algorithm
SHA1withDSA
// The ECDSA signature algorithms
NONEwithECDSA
SHA1withECDSA
SHA256withECDSA
SHA384withECDSA
SHA512withECDSA
//使用方式
byte[] data = "我是一段测试字符串".getBytes();
Sign sign = SecureUtil.sign(SignAlgorithm.MD5withRSA);
//签名
byte[] signed = sign.sign(data);
//验证签名
boolean verify = sign.verify(data, signed);
//使用私钥。进行签名。方式选择 MD5withRSA 方式
Sign sign = SecureUtil.sign(SignAlgorithm.MD5withRSA,privateKey,null);
//签名成功
byte[] signed = sign.sign(substring.getBytes());
//base64编码
String base64 = cn.hutool.core.codec.Base64.encode(signed);
//使用公钥 解密
//首先Base64解密
byte[] decode = cn.hutool.core.codec.Base64.decode(signStr);
//选择验证方式 以及 设置公钥
Sign sign = SecureUtil.sign(SignAlgorithm.MD5withRSA,null,publicKey);
boolean verify = sign.verify(substring.getBytes(), decode);
System.out.println("是否验收成功:"+ verify);
2、RSA加密 以及 RSA分段加密。 解密。
首先RSA加密。
简单的加密解密
//无参数构造方法:他会帮你自动生成 公钥 私钥
RSA rsa = new RSA();
//有参构造方法
RSA rsa = new RSA(privateKey,publicKey);
//参数1:S1为需要加密的字符串
//参数2:选择使用什么进行加密
rsa.encrypt(s1, KeyType.PrivateKey);
//解密
//参数1:需要解密的数据 byte数组
//参数2:使用什么进行解密
rsa.decrypt(decoByte, KeyType.PublicKey);
上面的就是简单的加密、解密。
下面说一下分段的加密、解密。
主要因为有一些字符串过长。有时候会报错。
分段这个原理比较简单
就是把需要加密的字符串分割成多个字符串。 然后分别进行加密。 加密以后拼接在一起。
一段一段的加密
废话不多说。上代码
//分段加密长度
public static final Integer MAX_ENCRYPT_BLOCK = 117;
//分段解密长度
public static final Integer MAX_DECRYPT_BLOCK = 128;
分段加密思路
假设需要加密的字符串为:
String str = "123456789123456789.....N";
1、先将 str变成数组。 数组中的元素每个的长度为117
2、循环数组。对每个元素使用rsa的私钥进行加密
3、加所有加密以后的str拼接在一起。 使用base64编码(编码不编码看业务需求)。
废话不多说。继续上代码!!!
RSA rsa = new RSA(privateKey,publicKey);
//JSON = 需要加密的字符串
//StrUtil是hutools里面的字符串工具类
String[] strList = StrUtil.split(json, MAX_ENCRYPT_BLOCK);
//加密以后的byte数组
List<byte[]> strEncryp = new ArrayList<byte[]>();
for (String s1 : strList) {
//这里针对于数组中的每一个字符串进行RSA 私钥方式加密
strEncryp.add(rsa.encrypt(s1, KeyType.PrivateKey));
}
//ArrayUtils 是 commons-lang3包下的
//把所有加密后的byte数组合并到一个数组里面
byte[] bytes1 = strEncryp.get(0);
for (int i = 1; i < strEncryp.size(); i++) {
bytes1 = ArrayUtils.addAll(bytes1, strEncryp.get(i));
}
System.out.println("RSA分段加密后的数据:" + bytes1);
System.out.println("RSA分段加密数据 在base64编码一下:" + Base64.encode(bytes1));
分段解密的思路(加密逆向过来不就好了!!!)
PS:加密都写出来了。按理来讲。我不应该在贴解密了。让大家活跃一下大脑。
但是谁叫我人好呢。 记得给我点个赞。兄弟们!
1、首先拿到base64编码后的内容。进行解码。
2、解码后形成的是byte[] 将byte数组进行拆分。 按照解密的长度 形成 byte[][] 数组
3、然后循环byte[]数组。 每段使用 公钥 进行 解密。
4、最后把解密后的所有byte数组合并在一起。
5、使用 new String() 把byte创建成字符串。 就可以啦。
废话不多说。继续上代码!
我先上一个分割字节数组成为多个数组的方法。
将数组进行拆分。分段处理。
public static byte[][] splitBytes(byte[] bytes, int size) {
double splitLength = Double.parseDouble(size + "");
int arrayLength = (int) Math.ceil(bytes.length / splitLength);
byte[][] result = new byte[arrayLength][];
int from, to;
for (int i = 0; i < arrayLength; i++) {
from = (int) (i * splitLength);
to = (int) (from + splitLength);
if(to > bytes.length){
to = bytes.length;
}
result[i] = Arrays.copyOfRange(bytes, from, to);
}
return result;
}
分段解密:
//所返回加密的数据
System.out.println("需要解密的数据:"+pay_result);
//设置rsa准备解密 那边用私钥加密的。所以我用公钥解密
RSA rsaDeco = new RSA(null,publicKey);
//1、第一步先整个字符串用base64解码
byte[] decode = Base64.decode(pay_result);
//2、第二步分段 用 平台公钥解码。
byte[][] bytes = ListTools.splitBytes(decode, MAX_DECRYPT_BLOCK);
List<byte[]> strDeco = new ArrayList<byte[]>();
for (byte[] decoByte : bytes) {
strDeco.add(rsaDeco.decrypt(decoByte, KeyType.PublicKey));
}
//3、第三步拼接一起获取字符串
byte[] byteDECR = strDeco.get(0);
for (int i = 1; i < strDeco.size(); i++) {
byteDECR = ArrayUtils.addAll(byteDECR, strDeco.get(i));
}
//4、字节数组恢复成字符串
String retInfoStr = new String(byteDECR);
各位要觉得有用。帮兄弟我点个赞。留个言。抱拳了。老铁们!