有时候后端需要通过回调来与前端交互,但回调url上往往有关键性的信息例如用户的token,为了防止此链接被恶意拦截反复使用,有必要将关键参数加上时间戳并用加密算法加密与前端交互。前端可以控制时间戳大于多少分钟则忽略此token,拦截者不知道密钥情况下无法伪造加密文,就可以避免此链接反复被使用。因为前端代码能被破解故而使用非对称加密算法RSA。【当然,前端手机用户可以通过修改系统时间来破解此判断,但可以往所有与后端接口交互中后端加入时间戳判断,一样可以解决此问题。】
后端RSA加解密算法
网上有些算法是只支持加密117位解密128位限制的写法,以下参考互联网的一些代码,变为不限制,密钥长度为1024位,若改为2048位则需要变更解密算法的长度为256。
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import sun.misc.BASE64Decoder;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
import java.util.Base64;
/**
* @author katasea
* 2020/8/11 11:01
*/
public class RSAUtils4Mall {
private static final String RSA_ALGORITHM = "RSA";
private static final int MAX_DECRYPT_BLOCK = 128;
private static final int MAX_ENCRYPT_BLOCK = 117;
private static RSAPublicKey publicKey;
private static RSAPrivateKey privateKey;
public RSAPublicKey getPublicKey() {
return RSAUtils4Mall.publicKey;
}
public RSAPrivateKey getPrivateKey() {
return RSAUtils4Mall.privateKey;
}
public void getKeys() throws Exception {
// 从 公钥保存的文件 读取 公钥的Base64文本
String pubKeyBase64 = "你自己的公钥,可以放此处方便固定,若不需要固定则使用geneKeys() 方法";
// 把 公钥的Base64文本 转换为已编码的 公钥bytes
byte[] encPubKey = new BASE64Decoder().decodeBuffer(pubKeyBase64);
// 创建 已编码的公钥规格
X509EncodedKeySpec encPubKeySpec = new X509EncodedKeySpec(encPubKey);
// 获取指定算法的密钥工厂, 根据 已编码的公钥规格, 生成公钥对象
publicKey = (RSAPublicKey)KeyFactory.getInstance("RSA").generatePublic(encPubKeySpec);
// 从 私钥保存的文件 读取 私钥的base文本
String priKeyBase64 = "你自己的私钥,可以放此处方便固定,若不需要固定则使用geneKeys() 方法";
// 把 私钥的Base64文本 转换为已编码的 私钥bytes
byte[] encPriKey = new BASE64Decoder().decodeBuffer(priKeyBase64);
// 创建 已编码的私钥规格
PKCS8EncodedKeySpec encPriKeySpec = new PKCS8EncodedKeySpec(encPriKey);
// 获取指定算法的密钥工厂, 根据 已编码的私钥规格, 生成私钥对象
privateKey = (RSAPrivateKey)KeyFactory.getInstance("RSA").generatePrivate(encPriKeySpec);
}
public void geneKeys() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(RSA_ALGORITHM, new BouncyCastleProvider());
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
privateKey = (RSAPrivateKey) keyPair.getPrivate();
publicKey = (RSAPublicKey) keyPair.getPublic();
}
public String encodeByPrivateKey(String body) throws Exception {
Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] inputArray = body.getBytes();
int inputLength = inputArray.length;
System.out.println("加密字节数:" + inputLength);
// 标识
int offSet = 0;
byte[] resultBytes = {
};
byte[] cache = {
};
while (inputLength - offSet > 0) {
if (inputLength - offSet > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(inputArray, offSet, MAX_ENCRYPT_BLOCK);
offSet += MAX_ENCRYPT_BLOCK;
} else {
cache = cipher.doFinal(inputArray, offSet, inputLength - offSet);
offSet = inputLength;