Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 27, 2025

loadConfigInputStreamprivateKeyString 进行 base64 解码后,PemUtils.loadPrivateKey 会再次尝试 base64 解码,导致用户设置纯 base64 私钥内容时抛出解码异常。

修复内容

  • 区分密钥格式处理逻辑:base64 解码后检查结果是否为 PEM 格式

    • PEM 格式(含 -----BEGIN/END 标记):使用解码后内容
    • p12 证书:使用解码后的二进制数据
    • 纯 base64 私钥/公钥内容:返回原始字符串,由 PemUtils 处理解码
  • 代码质量改进

    • 添加 CERT_NAME_P12 常量
    • 提取 isPemFormat() 辅助方法
    • 添加 base64 解码异常处理

支持的输入格式

// 1. 完整 PEM 格式字符串
config.setPrivateKeyString("-----BEGIN PRIVATE KEY-----\nMIIEvg...\n-----END PRIVATE KEY-----");

// 2. base64 编码的完整 PEM 文件
config.setPrivateKeyString(Base64.encode(pemFileContent));

// 3. 纯 base64 私钥内容(PEM 中间部分,不含头尾标记)
config.setPrivateKeyString("MIIEvgIBADANBgkqhki...");

Fixes #3765

Original prompt

This section details on the original issue you should resolve

<issue_title>从base64字符串加载密钥时报错</issue_title>
<issue_description>版本:4.7.8.B

在构建WxPayConfig配置时,如果设置base64密钥串,读取时会出现base64解码异常。定位源码,发现问题出在WxPayConfig中的方法:

Image Image

调用this.loadConfigInputStream 获取 inputStream,再执行 merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream);

Image

在 PemUtils.loadPrivateKey 对流再次进行base64解码
</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits November 27, 2025 12:55
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix base64 decoding error when loading key 修复从 base64 字符串加载私钥时的双重解码问题 Nov 27, 2025
Copilot AI requested a review from binarywang November 27, 2025 13:01
Copilot finished work on behalf of binarywang November 27, 2025 13:01
@binarywang binarywang marked this pull request as ready for review November 27, 2025 15:02
@binarywang binarywang merged commit e57b7e1 into develop Nov 27, 2025
1 check passed
@binarywang binarywang deleted the copilot/fix-base64-decoding-error branch November 28, 2025 02:11
@binarywang binarywang added this to the 4.7.9 milestone Nov 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

从base64字符串加载密钥时报错

2 participants