(improvement)(Chat) Add encryption and decryption for llm api key (#1267)

* (improvement)(Chat) Add encryption and decryption for llm api key

* (improvement)(Chat) Change Plugin to ChatPlugin

---------

Co-authored-by: lxwcodemonkey
This commit is contained in:
LXW
2024-06-28 15:51:31 +08:00
committed by GitHub
parent ce0eec7aba
commit b34268c236
19 changed files with 180 additions and 83 deletions

View File

@@ -1,5 +1,6 @@
package com.tencent.supersonic.common.config;
import com.tencent.supersonic.common.util.AESUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@@ -36,4 +37,9 @@ public class LLMConfig {
this.modelName = modelName;
this.temperature = temperature;
}
public String keyDecrypt() {
return AESUtil.aesDecrypt(apiKey);
}
}

View File

@@ -0,0 +1,84 @@
package com.tencent.supersonic.common.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
@Slf4j
public class AESUtil {
private static final String KEY = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08";
//算法
private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";
public static String aesDecrypt(String encrypt) {
try {
return aesDecrypt(encrypt, KEY);
} catch (Exception e) {
log.warn("content decrypt failed:{}", encrypt);
return encrypt;
}
}
private static String aesDecrypt(String encryptStr, String decryptKey) throws Exception {
return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
}
private static String base64Encode(byte[] bytes) {
return Base64.encodeBase64String(bytes);
}
private static byte[] base64Decode(String base64Code) throws Exception {
return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);
}
private static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(hexStringToByteArray(encryptKey), "AES"));
return cipher.doFinal(content.getBytes("utf-8"));
}
private static String aesEncrypt(String content, String encryptKey) throws Exception {
return base64Encode(aesEncryptToBytes(content, encryptKey));
}
private static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(hexStringToByteArray(decryptKey), "AES"));
byte[] decryptBytes = cipher.doFinal(encryptBytes);
return new String(decryptBytes);
}
public static byte[] hexStringToByteArray(String hexString) {
int len = hexString.length();
byte[] byteArray = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
byteArray[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
+ Character.digit(hexString.charAt(i + 1), 16));
}
return byteArray;
}
public static void main(String[] args) throws Exception {
String content = "123";
System.out.println("before encrypt" + content);
System.out.println("key" + KEY);
String encrypt = aesEncrypt(content, KEY);
System.out.println("after encrypt" + encrypt);
String decrypt = aesDecrypt(encrypt);
System.out.println("after decrypt" + decrypt);
}
}

View File

@@ -22,7 +22,7 @@ public class S2ChatModelProvider {
.builder()
.baseUrl(llmConfig.getBaseUrl())
.modelName(llmConfig.getModelName())
.apiKey(llmConfig.getApiKey())
.apiKey(llmConfig.keyDecrypt())
.temperature(llmConfig.getTemperature())
.timeout(Duration.ofSeconds(llmConfig.getTimeOut()))
.build();