试了一下午终于跑通了,一开始尝试RC4算法生成的密文在java中解密不出来,放弃了,改用AES。
js代码
import aes from 'crypto-js/aes';
import base from 'crypto-js/enc-base64';
function encrypt(plaintext: string) {
const iv = base.parse('ZGYyMzRzZDIzNDEyMzU2Zw==');
const key = base.parse('ZGYyMzRzZGRzYWVxZWZ2ZA==');
// 如果.toString()改成.ciphertext.toString(),则java解密时要使用HexUtil.decode,否则使用BASE64Util.decode
return aes.encrypt(plaintext, key, {iv: iv}).toString();
}
java代码
import com.google.api.client.repackaged.org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* AES工具类,密钥必须是16位字符串
*/
public class AESUtils {
/**偏移量,必须是16位字符串*/
private static final String IV_STRING = "df234sd23412356g";
/**
* 默认的密钥
*/
public static final String DEFAULT_KEY = "df234sddsaeqefvd";
public static String encryptData(String content) {
byte[] encryptedBytes = null;
try {
byte[] byteContent = content.getBytes("UTF-8");
// 注意,为了能与 iOS 统一
// 这里的 key 不可以使用 KeyGenerator、SecureRandom、SecretKey 生成
byte[] enCodeFormat = DEFAULT_KEY.getBytes();
SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
byte[] initParam = IV_STRING.getBytes();
IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
// 指定加密的算法、工作模式和填充方式
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
encryptedBytes = cipher.doFinal(byteContent);
// 同样对加密后数据进行 base64 编码
String encodeStr = BASE64Util.encode(encryptedBytes);
// String encodeStr = HexUtil.encode(encryptedBytes);
encodeStr = encodeStr.replace("%","%25").replace("&","%26").replace("+","%2B");
return encodeStr;
// return parseByte2HexStr(encryptedBytes);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String decryptData(String content) {
try {
content = content.replace("%25","%").replace("%26","&").replace("%2B","+");
// base64 解码
byte[] encryptedBytes = BASE64Util.decode(content);
// byte[] encryptedBytes = HexUtil.decode(content);
// byte[] encryptedBytes = parseHexStr2Byte(content);
byte[] enCodeFormat = DEFAULT_KEY.getBytes();
SecretKeySpec secretKey = new SecretKeySpec(enCodeFormat, "AES");
byte[] initParam = IV_STRING.getBytes();
IvParameterSpec ivParameterSpec = new IvParameterSpec(initParam);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
byte[] result = cipher.doFinal(encryptedBytes);
return new String(result, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
byte[] bytes = IV_STRING.getBytes();
String base64Str = Base64.encodeBase64String(bytes);
// js 中的iv用这个base64Str
System.out.println(base64Str);
bytes = DEFAULT_KEY.getBytes();
base64Str = Base64.encodeBase64String(bytes);
// js 中的key用这个base64Str
System.out.println(base64Str);
System.out.println("用户加密"+AESUtils.encryptData("《湖北省能源发展“十四五”规划》提出,适度超前推进充电础基设施建设,到2025年全省充电桩达到50万个以上。"));
System.out.println("用户解密"+AESUtils.decryptData("HYezy8lsT3ql6uAZxm/7vBCZAI0kxUpvny9tJLRbMJg/Mr4dCalEqC19z8Yl21tEuEdzGWTqTjIASOqx/FL+XimSZt1seFUpJrJ5E/2XorWwFZNMXKSURFdw8wV7savQJbVaTwZuJ4yML6ok1VwQ8y8yAqmr0TG38FLovyfEhQcVVOx0+UmQdsOLpERuYQDE9Pw216beyhml1BZNDhC91Q=="));
}
}
上面用到的BASE64Util.decode为
public static byte[] decode(String base64str) throws Exception { //
byte[] b=base64str.getBytes();
Base64 base64=new Base64();
b=base64.decode(b);
return b;
}
HexUtil.decode为
public static byte[] decode(String src) {
int m = 0, n = 0;
int byteLen = src.length() / 2;
byte[] ret = new byte[byteLen];
for (int i = 0; i < byteLen; i++) {
m = i * 2 + 1;
n = m + 1;
int intVal = Integer.decode("0x" + src.substring(i * 2, m) + src.substring(m, n));
ret[i] = Byte.valueOf((byte)intVal);
}
return ret;
}