API安全机制包括两部分:数字签名、敏感信息加密。
一、数字签名
服务端使用客户端的消息签名验证客户端的身份。如果一个请求不包含签名或者签名验证失败,服务端将返回身份验证错误。它背后的技术是:数字签名技术。
1、待签参数准备
待签参数包含以下字段
参数 | 说明 |
---|---|
Url | Url 指 Path + Query + Body 中 Form 参数,域名无需参与签名。组织方法:对 Query+Form 参数按照字典对 Key 进行排序后按照如下方法拼接,如果 Query 或 Form 参数为空,则 Url = Path,不需要添加 ?,如果某个参数的 Value 为空只保留 Key 参与签名,等号不需要再加入签名。Query参数的Value如果存在中文,则需要对Value值进行UrlEncode编码,否则会出现中文乱码或签名报错等情况。 |
Content-MD5 | 指 Body 的 MD5 值,只有当 Body 非 Form 表单时才计算 MD5。把MD5摘要后的二进制数组使用Base64进行编码 |
Timestamp | 获取发起请求时的系统当前时间戳,即格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数,作为请求时间戳。例如1554208460 |
2、待签参数拼接
String stringToSign=
Url + "\n" +
Content-MD5 + "\n"
Timestamp
注意:Content-MD5如果为空也需要参与拼接 。
3、计算签名
使用应用密钥(APP KEY)对待签名字符串采用HmacSHA256算法进行签名运算,然后使用Base64算法进行编码,从而得到签名字符串。
Mac hmacSha256 = Mac.getInstance("HmacSHA256");
byte[] appSecretBytes = appSecret.getBytes(Charset.forName("UTF-8"));
hmacSha256.init(new SecretKeySpec(appSecretBytes, 0, appSecretBytes.length, "HmacSHA256"));
byte[] md5Result = hmacSha256.doFinal(stringToSign.getBytes(Charset.forName("UTF-8")));
String signature = Base64.encodeBase64String(md5Result);
二、敏感信息加密
客户端如果有敏感字段需要上送,则需要对字段进行加密。服务端收到后,需要对敏感字段解密。
下面使用AES算法对敏感信息加解密。
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class MyTest {
// AES算法
private static final String ALGORITHM = "AES";
// 使用CBC模式、PKCS5Padding填充
private static final String TRANSFORMATION = "AES/CBC/PKCS5PADDING";
/**
* AES加密
*
* @param secretKey 密钥
* @param ivKey 偏移量
* @param data 明文数据
* @return 加密后的数据
*/
public static String encrypt(String secretKey, String ivKey, String data) {
try {
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), ALGORITHM);
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivKey.getBytes(StandardCharsets.UTF_8));
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] encrypted = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(encrypted);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* AES解密
*
* @param secretKey 密钥
* @param ivKey 偏移量
* @param data 加密后的数据
* @return 明文数据
*/
public static String decrypt(String secretKey, String ivKey, String data) {
try {
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), ALGORITHM);
IvParameterSpec ivParameterSpec = new IvParameterSpec(ivKey.getBytes(StandardCharsets.UTF_8));
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(data.getBytes(StandardCharsets.UTF_8)));
return new String(decrypted);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws Exception {
String secretKey = "1234567890123456";
String ivKey = "1234567890123456";
String data = "你好,世界";
String encryptData = encrypt(secretKey, ivKey, data);
System.out.println("加密后的数据:" + encryptData);
String decryptData = decrypt(secretKey, ivKey, encryptData);
System.out.println("解密后的数据:" + decryptData);
}
}