https://github.com/langgenius/dify查看设置的apikey
2026/5/15 19:33:37 网站建设 项目流程

现在我已经掌握了足够的信息,来做一个完整清晰的分析。好的,现在我来给出一个完整的分析。

Difyprovider_model_credentials.encrypted_config解密分析

整体加密架构

Dify 使用 PKCS1_OAEP 加密来保护 API key。每个用户(tenant)都有一个唯一的密钥对,确保 API key 的机密性。

实际上从源码看,Dify 用的是混合加密(RSA + AES),下面是完整的加解密流程图:### 核心源码解读

加密逻辑api/libs/rsa.py):

prefix_hybrid=b"HYBRID:"defencrypt(text:str,public_key:...)->bytes:aes_key=get_random_bytes(16)# 随机生成 AES-128 Keycipher_aes=AES.new(aes_key,AES.MODE_EAX)ciphertext,tag=cipher_aes.encrypt_and_digest(text.encode())# AES 加密明文cipher_rsa=gmpy2_pkcs10aep_cipher.new(rsa_key)enc_aes_key=cipher_rsa.encrypt(aes_key)# RSA-OAEP 加密 AES Key# 拼接成最终密文returnprefix_hybrid+enc_aes_key+cipher_aes.nonce+tag+ciphertext

所以encrypted_config字段的字节结构为:

HYBRID: | enc_aes_key(256B) | nonce(16B) | tag(16B) | ciphertext(变长)

解密逻辑api/core/provider_manager.py):

解密时,系统通过encrypter.get_decrypt_decoding(tenant_id)获取租户的 RSA 私钥,再调用encrypter.decrypt_token_with_decodingprovider_model_credentials中各字段的加密值进行解密。

如何手动查看 API Key

你需要同时具备两个东西:

1. 从数据库取出密文

-- 找到目标记录SELECTid,tenant_id,model_name,encrypted_configFROMprovider_model_credentialsWHEREprovider_name='openai';

2. 取出对应租户的 RSA 私钥

私钥存储位置取决于你的文件存储配置(STORAGE_TYPE),路径为privkeys/{tenant_id}/private.pem

  • 本地部署:默认在api/storage/privkeys/{tenant_id}/private.pem
  • S3/OSS 等:在对应的 Bucket 中

3. 用 Python 脚本解密

fromCrypto.PublicKeyimportRSAfromCrypto.CipherimportAESimportbase64 prefix=b"HYBRID:"defdecrypt_api_key(encrypted_hex_or_bytes:bytes,private_key_pem:bytes)->str:# 1. 去掉前缀data=encrypted_hex_or_bytesifdata.startswith(prefix):data=data[len(prefix):]# 2. 加载私钥rsa_key=RSA.import_key(private_key_pem)key_size=rsa_key.size_in_bytes()# 256 bytes for RSA-2048# 3. 拆分各部分enc_aes_key=data[:key_size]nonce=data[key_size:key_size+16]tag=data[key_size+16:key_size+32]ciphertext=data[key_size+32:]# 4. RSA-OAEP 解密 AES Keyfromlibsimportgmpy2_pkcs10aep_cipher# Dify 的 OAEP 实现cipher_rsa=gmpy2_pkcs10aep_cipher.new(rsa_key)aes_key=cipher_rsa.decrypt(enc_aes_key)# 5. AES-EAX 解密明文cipher_aes=AES.new(aes_key,AES.MODE_EAX,nonce=nonce)plaintext=cipher_aes.decrypt_and_verify(ciphertext,tag)returnplaintext.decode()# 使用withopen("private.pem","rb")asf:pem=f.read()# encrypted_config 字段值(bytes)api_key=decrypt_api_key(encrypted_config_bytes,pem)print(api_key)

注意事项

关键点说明
每个 tenant 独立密钥私钥按tenant_id分目录存放,不同租户不能互解
encrypted_config的格式provider_model_credentials表里存的是 JSON 序列化后的字段级加密(每个敏感字段单独加密),不是整条记录加密
SECRET_KEY环境变量用于 AES-256 加密第三方 OAuth 凭证,与 model credentials 是不同的加密路径
不要直接改密钥替换 RSA 私钥后,已存储的所有encrypted_config都将无法解密

简单来说:encrypted_config是 RSA+AES 混合加密的密文,解密必须持有对应tenant_id的 RSA 私钥文件,这个文件在你的文件存储后端(本地磁盘或对象存储)中,而不在数据库里。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询