加密说明
- 在每刻安全设置页面,开启OpenAPI返回加密数据开关,同时设置加密秘钥,即可启用接口加密功能
- 除了【单点登录SSO】【审批列表】【审批历史】【认证】接口,其余接口都会对返回数据进行整体加密
- 加密策略:先对返回的数据进行DES加密,然后再进行base64编码,即可得到加密后的数据
- 解密策略:先对加密数据进行base64解码,然后DES解密
- 加密解密的Java代码示例如下
import java.io.IOException;
import java.security.SecureRandom;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class DesUtil {
private final static String DES = "DES";
private final static String ENCODE = "UTF-8";
public static String encrypt(String data, String key) throws Exception {
byte[] bt = encrypt(data.getBytes(ENCODE), key.getBytes(ENCODE));
String strs = Base64.getEncoder().encodeToString(bt);
return strs;
}
public static String decrypt(String data, String key) throws IOException, Exception {
if (data == null)
return null;
Base64.Decoder decoder = Base64.getDecoder();
byte[] buf = decoder.decode(data);
byte[] bt = decrypt(buf, key.getBytes(ENCODE));
return new String(bt, ENCODE);
}
private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
SecureRandom sr = new SecureRandom();
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(DES);
cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
SecureRandom sr = new SecureRandom();
DESKeySpec dks = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
SecretKey securekey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(DES);
cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
return cipher.doFinal(data);
}
}