# 文件解密

当开放接口下载的文件内容需要解密时,请参考以下资料:

# 加密说明

  • 加密方式AES-256-CBC
  • 数据填充:PKCS#7填充至32字节的倍数
  • IV 初始向量:大小为16字节,取 AESKey 前16字节
  • 编码格式:下载的文件内容为 Base64 编码,需先 Base64 解码再进行 AES 解密

⚠️ 重要:加密不仅仅是为了解决网络传输过程被窃取(接口本来就是https加密传输),也是防止调用方拉取文件后,使用完毕没有及时清理,存储于云端等,若没有加密,容易造成通讯录敏感信息泄漏。

建议调用方使用固定的32位字符串,不需要每次请求生成新的。

# 解密流程

  1. download_url 下载加密文件(内容为 Base64 编码)
  2. 对文件内容进行 Base64 解码
  3. 使用 AES-256-CBC 解密(密钥为创建任务时传入的 aeskey,IV 为密钥前16字节)
  4. 解析解密后的 JSON 数据

# 代码示例

# PHP 解密

<?php
$aesKey = "today+yesterday+tomorrow+1234567"; // 32位
$content = file_get_contents("./成员导出.json");
if (!empty($content)) {
    // Base64 解码
    $encrypted = base64_decode($content);
    
    // AES 解密
    $iv = substr($aesKey, 0, 16);
    $decrypted = \openssl_decrypt($encrypted, 'AES-256-CBC', $aesKey, OPENSSL_RAW_DATA, $iv);
    echo $decrypted;    
}
?>

# Python 解密

import base64
import json
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad

# 32位 AES 密钥(与创建任务时传入的 aeskey 相同)
aes_key = "today+yesterday+tomorrow+1234567"

# 读取下载的加密文件
with open("成员导出.json", "r") as f:
    base64_data = f.read().strip()

# 1. Base64 解码
encrypted_data = base64.b64decode(base64_data)

# 2. AES-256-CBC 解密
iv = aes_key[:16].encode('utf-8')  # IV 为密钥前16字节
key = aes_key.encode('utf-8')

cipher = AES.new(key, AES.MODE_CBC, iv)
decrypted_data = unpad(cipher.decrypt(encrypted_data), AES.block_size)

# 3. 解析 JSON
members = json.loads(decrypted_data.decode('utf-8'))
print(f"成员总数: {len(members)}")

# Node.js 解密

const crypto = require('crypto');
const fs = require('fs');

// 32位 AES 密钥
const aesKey = 'today+yesterday+tomorrow+1234567';

// 读取下载的加密文件
const base64Data = fs.readFileSync('./成员导出.json', 'utf8').trim();

// 1. Base64 解码
const encryptedData = Buffer.from(base64Data, 'base64');

// 2. AES-256-CBC 解密
const iv = Buffer.from(aesKey.substring(0, 16), 'utf8');
const decipher = crypto.createDecipheriv('aes-256-cbc', aesKey, iv);

let decrypted = decipher.update(encryptedData);
decrypted = Buffer.concat([decrypted, decipher.final()]);

// 3. 解析 JSON
const members = JSON.parse(decrypted.toString('utf8'));
console.log(`成员总数: ${members.length}`);

# 常见问题

Q: 解密时报错 "Padding is incorrect" 或 "Invalid padding"?

A: 请检查:

  1. 是否先进行了 Base64 解码(下载的文件是 Base64 编码的)
  2. AES 密钥是否为32位(256bit)
  3. IV 是否正确(取密钥前16字节)
  4. 密钥是否与创建任务时传入的 aeskey 一致

Q: 下载的文件内容是什么格式?

A: 下载的文件内容是 Base64 编码的字符串,需要先进行 Base64 解码,再进行 AES 解密。