记录一次,代码中使用rsa加密,本地以及测试环境中文加密正常,不会出现乱码,但是生产环境会出现中文乱码。
排查一段时间,定位到问题;
针对中文获取字节流时,指定utf-8编码--代码如下
int inputLen = data.getBytes(UTF_8).length;以此类推,所以获取字节流位置,均添加指定utf-8编码
/**
* RSA加密
*
* @param data 待加密数据
* @param publicKey 公钥
* @return
*/
public static String encrypt(String data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(ENCRYPT_CONTENT);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
int inputLen = data.getBytes(UTF_8).length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offset = 0;
byte[] cache;
int i = 0;
// 对数据分段加密
while (inputLen - offset > 0) {
if (inputLen - offset > MAX_ENCRYPT_BLOCK) {