Skip to content

Commit f091d43

Browse files
committed
Merge 'dev-aicc' into 'master'
feat: 开发加密拦截器,支持以密文形式发送请求:(1)完成证书获取功能(2)完成请求加密和响应解密功能 See merge request: !723
2 parents f039aec + a51eb10 commit f091d43

File tree

1 file changed

+0
-83
lines changed

1 file changed

+0
-83
lines changed

volcengine-java-sdk-ark-runtime/src/main/java/com/volcengine/ark/runtime/utils/KeyAgreementUtil.java

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121

2222
import org.bouncycastle.jce.provider.BouncyCastleProvider;
2323

24-
/**
25-
* 密钥协商工具类
26-
* 实现ECIES (Elliptic Curve Integrated Encryption Scheme) 密钥协商方案
27-
*/
2824
public class KeyAgreementUtil {
2925
static {
3026
Security.addProvider(new BouncyCastleProvider());
@@ -60,12 +56,6 @@ public String getSessionToken() {
6056
}
6157
}
6258

63-
/**
64-
* 生成ECIES密钥对并计算共享密钥
65-
* @param publicKey 服务器公钥
66-
* @return SessionTokenData 包含加密密钥、随机数和会话令牌
67-
* @throws GeneralSecurityException 安全异常
68-
*/
6959
public static SessionData generateEciesKeyPair(PublicKey publicKey) throws GeneralSecurityException {
7060
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");
7161
ECParameterSpec ecSpec = ((java.security.interfaces.ECPublicKey) publicKey).getParams();
@@ -91,15 +81,6 @@ public static SessionData generateEciesKeyPair(PublicKey publicKey) throws Gener
9181
return new SessionData(cryptoKey, cryptoNonce, sessionToken);
9282
}
9383

94-
/**
95-
* 使用HKDF从共享密钥派生AES密钥和Nonce
96-
* @param sharedSecret 共享密钥
97-
* @param salt 盐值
98-
* @param info 信息
99-
* @param length 输出长度
100-
* @return byte[] 派生的密钥材料
101-
* @throws GeneralSecurityException 安全异常
102-
*/
10384
public static byte[] hkdf(byte[] sharedSecret, byte[] salt, byte[] info, int length)
10485
throws GeneralSecurityException {
10586
Mac hmacExtract = Mac.getInstance(HKDF_ALGORITHM);
@@ -132,11 +113,6 @@ public static byte[] hkdf(byte[] sharedSecret, byte[] salt, byte[] info, int len
132113
return result;
133114
}
134115

135-
/**
136-
* 将EC公钥序列化为字节数组
137-
* @param publicKey EC公钥
138-
* @return byte[] 序列化后的公钥字节数组
139-
*/
140116
public static byte[] marshalEcPublicKey(java.security.interfaces.ECPublicKey publicKey) {
141117
try {
142118
ECPoint point = publicKey.getW();
@@ -158,12 +134,6 @@ public static byte[] marshalEcPublicKey(java.security.interfaces.ECPublicKey pub
158134
}
159135
}
160136

161-
/**
162-
* 将BigInteger转换为指定长度的无符号大端字节数组
163-
* @param value BigInteger值
164-
* @param length 输出字节数组长度
165-
* @return byte[] 无符号大端字节数组
166-
*/
167137
public static byte[] toUnsignedBigEndian(BigInteger value, int length) {
168138
byte[] bytes = value.toByteArray();
169139
byte[] result = new byte[length];
@@ -179,11 +149,6 @@ public static byte[] toUnsignedBigEndian(BigInteger value, int length) {
179149
return result;
180150
}
181151

182-
/**
183-
* 验证密文本格式是否有效
184-
* @param ciphertext 密文本
185-
* @return boolean 是否有效
186-
*/
187152
public static boolean decryptValidate(String ciphertext) {
188153
try {
189154
byte[] cipherBytes = ciphertext.getBytes(StandardCharsets.UTF_8);
@@ -199,13 +164,6 @@ public static boolean decryptValidate(String ciphertext) {
199164
}
200165
}
201166

202-
/**
203-
* 使用密钥解密字符串
204-
* @param key 加密密钥
205-
* @param nonce 随机数
206-
* @param encryptedContent 加密内容
207-
* @return String 解密后的明文
208-
*/
209167
public static String decryptStringWithKey(byte[] key, byte[] nonce, String encryptedContent) {
210168
try {
211169
String content;
@@ -226,13 +184,6 @@ public static String decryptStringWithKey(byte[] key, byte[] nonce, String encry
226184
}
227185
}
228186

229-
/**
230-
* 使用密钥加密字符串
231-
* @param key 加密密钥
232-
* @param nonce 随机数
233-
* @param plaintext 明文
234-
* @return String Base64编码的加密结果
235-
*/
236187
public static String encryptStringWithKey(byte[] key, byte[] nonce, String plaintext) {
237188
try {
238189
Cipher cipher = javax.crypto.Cipher.getInstance("AES/GCM/NoPadding");
@@ -249,13 +200,6 @@ public static String encryptStringWithKey(byte[] key, byte[] nonce, String plain
249200
}
250201
}
251202

252-
/**
253-
* 解密Base64编码的AES-GCM加密字符串
254-
* @param key 加密密钥
255-
* @param nonce 随机数
256-
* @param ciphertext Base64编码的密文
257-
* @return String 解密后的明文
258-
*/
259203
public static String aesGcmDecryptBase64String(byte[] key, byte[] nonce, String ciphertext) {
260204
try {
261205
String cleaned = ciphertext.replaceAll("\\s", "");
@@ -275,14 +219,6 @@ public static String aesGcmDecryptBase64String(byte[] key, byte[] nonce, String
275219
}
276220
}
277221

278-
/**
279-
* AES-GCM解密核心方法
280-
* @param key 加密密钥
281-
* @param iv 初始化向量
282-
* @param cipherBytes 密文字节数组
283-
* @return String 解密后的明文
284-
* @throws GeneralSecurityException 安全异常
285-
*/
286222
public static String aesGcmDecrypt(byte[] key, byte[] iv, byte[] cipherBytes) throws GeneralSecurityException {
287223
try {
288224
Cipher decryptor = Cipher.getInstance("AES/GCM/NoPadding", "BC");
@@ -297,13 +233,6 @@ public static String aesGcmDecrypt(byte[] key, byte[] iv, byte[] cipherBytes) th
297233
}
298234
}
299235

300-
/**
301-
* 解密包含多个Base64块的密文
302-
* @param key 加密密钥
303-
* @param nonce 随机数
304-
* @param ciphertext 包含多个Base64块的密文
305-
* @return String 拼接后的解密结果
306-
*/
307236
public static String aesGcmDecryptBase64List(byte[] key, byte[] nonce, String ciphertext) {
308237
List<String> result = new ArrayList<>();
309238

@@ -322,11 +251,6 @@ public static String aesGcmDecryptBase64List(byte[] key, byte[] nonce, String ci
322251
return String.join("", result);
323252
}
324253

325-
/**
326-
* 从字符串中提取所有Base64编码块
327-
* @param ciphertext 原始字符串
328-
* @return List<String> Base64块列表
329-
*/
330254
public static List<String> extractBase64Blocks(String ciphertext) {
331255
List<String> blocks = new ArrayList<>();
332256

@@ -344,13 +268,6 @@ public static List<String> extractBase64Blocks(String ciphertext) {
344268
return blocks;
345269
}
346270

347-
/**
348-
* 处理边界情况的递归解密方法
349-
* @param key 加密密钥
350-
* @param nonce 随机数
351-
* @param data 待解密数据
352-
* @return String 解密结果
353-
*/
354271
private static String decryptCornerCase(byte[] key, byte[] nonce, String data) {
355272
for (int i = 20; i < data.length(); i += 4) {
356273
try {

0 commit comments

Comments
 (0)