fix: 修复 Gemini 解密调试脚本的密钥生成逻辑

- 使用 config.security.encryptionKey 而不是直接读取环境变量
- 添加 crypto.scryptSync 和 ENCRYPTION_SALT 以匹配实际加密逻辑
- 显示实际使用的加密配置信息

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
shaw
2025-07-26 12:18:57 +08:00
parent a939fcebac
commit 8f0da8e914

View File

@@ -13,18 +13,16 @@ dotenv.config({ path: path.join(__dirname, '..', '.env') });
const redis = require('../src/models/redis'); const redis = require('../src/models/redis');
const logger = require('../src/utils/logger'); const logger = require('../src/utils/logger');
const config = require('../config/config');
const ALGORITHM = 'aes-256-cbc'; const ALGORITHM = 'aes-256-cbc';
const IV_LENGTH = 16; const IV_LENGTH = 16;
const GEMINI_ACCOUNT_KEY_PREFIX = 'gemini_account:'; const GEMINI_ACCOUNT_KEY_PREFIX = 'gemini_account:';
const ENCRYPTION_SALT = 'gemini-encryption-salt-2024';
// 生成加密密钥 // 生成加密密钥(使用与 geminiAccountService 相同的方法)
function generateEncryptionKey() { function generateEncryptionKey() {
const configKey = process.env.ENCRYPTION_KEY; return crypto.scryptSync(config.security.encryptionKey, ENCRYPTION_SALT, 32);
if (!configKey || configKey.length !== 32) {
throw new Error('ENCRYPTION_KEY must be exactly 32 characters long');
}
return Buffer.from(configKey);
} }
// 旧版解密函数(使用冒号分隔) // 旧版解密函数(使用冒号分隔)
@@ -68,10 +66,11 @@ async function debugGeminiDecrypt() {
try { try {
console.log('🚀 开始调试 Gemini refreshToken 解密...\n'); console.log('🚀 开始调试 Gemini refreshToken 解密...\n');
// 显示环境变量 // 显示加密配置
console.log('📋 环境变量检查:'); console.log('📋 加密配置检查:');
console.log(` ENCRYPTION_KEY 长度: ${process.env.ENCRYPTION_KEY ? process.env.ENCRYPTION_KEY.length : 'undefined'}`); console.log(` config.security.encryptionKey: ${config.security.encryptionKey}`);
console.log(` ENCRYPTION_KEY 前8位: ${process.env.ENCRYPTION_KEY ? process.env.ENCRYPTION_KEY.substring(0, 8) + '...' : 'undefined'}`); console.log(` 实际使用的加密密钥长度: ${config.security.encryptionKey.length}`);
console.log(` ENCRYPTION_SALT: ${ENCRYPTION_SALT}`);
console.log(); console.log();
// 连接 Redis // 连接 Redis