接口描述
该接口用于修改 P12 证书文件的密码。通过上传原始 P12 证书文件及其旧密码,接口将生成一个使用新密码保护的 P12 证书,并以 base64 编码形式返回。
接口信息
请求参数
| 参数名 | 类型 | 必填 | 说明 |
| file | File | 是 | 要修改密码的 P12 证书文件 |
| old_password | String | 是 | 原始 P12 证书的密码 |
| new_password | String | 是 | 新的 P12 证书密码 |
返回参数
| 参数名 | 类型 | 说明 |
| code | Integer | 状态码,200 表示成功 |
| msg | String | 状态描述 |
| data | Object | 返回数据 |
| data.certificateName | String | 新证书文件名 |
| data.certificateContent | String | 编码的证书内容 |
| data.certificatePassword | String | 新证书密码 |
状态码说明
| 状态码 | 说明 |
| 200 | 成功 |
| 400 | 参数错误 |
| 405 | 请求方式错误 |
| 500 | 服务器内部错误 |
请求示例
使用 curl 命令
curl -X POST \
-F "file=@/path/to/your/certificate.p12" \
-F "old_password=旧密码" \
-F "new_password=新密码" \
http://example.com/p12pwd.php
使用 JavaScript (Fetch API)
const formData = new FormData();
formData.append('file', document.getElementById('certificateFile').files[0]);
formData.append('old_password', '旧密码');
formData.append('new_password', '新密码');
fetch('http://example.com/p12pwd.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
console.log(data);
if (data.code === 200) {
// 将 base64 转换为文件并下载
const link = document.createElement('a');
link.href = 'data:application/x-pkcs12;base64,' + data.data.certificateContent;
link.download = data.data.certificateName;
link.click();
} else {
alert('错误: ' + data.msg);
}
})
.catch(error => console.error('Error:', error));
使用 PHP
<?php
$url = 'http://example.com/p12pwd.php';
$file = new CURLFile('/path/to/your/certificate.p12', 'application/x-pkcs12', 'certificate.p12');
$data = [
'file' => $file,
'old_password' => '旧密码',
'new_password' => '新密码'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
print_r($result);
// 如果成功,可以将 base64 内容保存为文件
if ($result['code'] === 200) {
$certificateContent = base64_decode($result['data']['certificateContent']);
file_put_contents($result['data']['certificateName'], $certificateContent);
echo "证书已保存为: " . $result['data']['certificateName'];
}
?>
返回示例
成功响应
{
"code": 200,
"msg": "证书密码修改成功",
"data": {
"certificateName": "certificate_new.p12",
"certificateContent": "MIIKBAIBAzCCCcwGCSqGSIb3DQEHAaCCCb0Egg...[省略base64编码内容]",
"certificatePassword": "新密码"
}
}
错误响应
{
"code": 400,
"msg": "缺少必要参数",
"data": []
}
{
"code": 500,
"msg": "密码错误或文件损坏",
"data": []
}
注意事项
- 接口仅支持 P12 格式的证书文件
- 确保提供的旧密码正确,否则将无法解密证书
- 返回的证书内容为 base64 编码,需要解码后保存为文件使用
暂无评论内容