Skip to content

SM2相关example例程运行报错 #8

Description

@zhangyongann

开发者您好,我电脑win11 64位系统,python版本是3.12.0
已经正确安装gmssl和gmssl-python如下:

>>> import gmssl
>>> gmssl.GMSSL_PYTHON_VERSION
'2.2.2'
>>> gmssl.GMSSL_LIBRARY_VERSION
'GmSSL 3.1.1 Dev'

我在测试example文件夹下的例程时,sm3.py和sm4.py运行正常,sm2_key.py运行时报错如下:

File "C:\Users\admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\gmssl.py", line 436, in export_encrypted_private_key_info_pem
    if gmssl.sm2_private_key_info_encrypt_to_pem(byref(self), c_char_p(passwd), c_void_p(fp)) != 1:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: exception: access violation writing 0x0000000000000024

在网上搜索该错误一直没找到解决办法,特在此提问,烦请进行解答,感谢

Activity

  1. zhangyongann commented on Oct 20, 2023

    @zhangyongann
    Author

    补充更详细的系统信息:

    系统类型:64 位操作系统, 基于 x64 的处理器
    版本:Windows 11 专业版 22H2

    python版本信息:

    PS C:\Users\admin> python
    Python 3.12.0 (tags/v3.12.0:0fb18b0, Oct  2 2023, 13:03:39) [MSC v.1935 64 bit (AMD64)] on win32
    
  2. 0xHyperia commented on Oct 25, 2023

    @0xHyperia

    开发者您好,我的电脑win11 64位系统,python版本是3.12.0 已经正确安装gmssl和gmssl-python如下:

    >>> import gmssl
    >>> gmssl.GMSSL_PYTHON_VERSION
    '2.2.2'
    >>> gmssl.GMSSL_LIBRARY_VERSION
    'GmSSL 3.1.1 Dev'
    

    我在测试example文件夹下的示例程序时,sm3.py和sm4.py运行正常,sm2_key.py运行错误时报如下:

    File "C:\Users\admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\gmssl.py", line 436, in export_encrypted_private_key_info_pem
        if gmssl.sm2_private_key_info_encrypt_to_pem(byref(self), c_char_p(passwd), c_void_p(fp)) != 1:
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    OSError: exception: access violation writing 0x0000000000000024
    

    在网上搜索该错误一直没有找到解决办法,特此提问,烦请进行解答,谢谢

    解决了吗?

  3. Beitadoge commented on Nov 16, 2023

    @Beitadoge

    同样的版本,我也遇到了这个问题,请问解决了吗?

  4. r1is commented on Nov 17, 2023

    @r1is

    开发者您好,我电脑win11 64位系统,python版本是3.12.0 已经正确安装gmssl和gmssl-python如下:

    >>> import gmssl
    >>> gmssl.GMSSL_PYTHON_VERSION
    '2.2.2'
    >>> gmssl.GMSSL_LIBRARY_VERSION
    'GmSSL 3.1.1 Dev'
    

    我在测试example文件夹下的例程时,sm3.py和sm4.py运行正常,sm2_key.py运行时报错如下:

    File "C:\Users\admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\gmssl.py", line 436, in export_encrypted_private_key_info_pem
        if gmssl.sm2_private_key_info_encrypt_to_pem(byref(self), c_char_p(passwd), c_void_p(fp)) != 1:
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    OSError: exception: access violation writing 0x0000000000000024
    

    在网上搜索该错误一直没找到解决办法,特在此提问,烦请进行解答,感谢

    我也遇到了这个问题,请问解决了吗

  5. Jinkeee commented on Nov 22, 2023

    @Jinkeee

    遇到相同的问题了。请问解决了吗?

    开发者您好,我电脑win11 64位系统,python版本是3.12.0 已经正确安装gmssl和gmssl-python如下:

    >>> import gmssl
    >>> gmssl.GMSSL_PYTHON_VERSION
    '2.2.2'
    >>> gmssl.GMSSL_LIBRARY_VERSION
    'GmSSL 3.1.1 Dev'
    

    我在测试example文件夹下的例程时,sm3.py和sm4.py运行正常,sm2_key.py运行时报错如下:

    File "C:\Users\admin\AppData\Local\Programs\Python\Python312\Lib\site-packages\gmssl.py", line 436, in export_encrypted_private_key_info_pem
        if gmssl.sm2_private_key_info_encrypt_to_pem(byref(self), c_char_p(passwd), c_void_p(fp)) != 1:
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    OSError: exception: access violation writing 0x0000000000000024
    

    在网上搜索该错误一直没找到解决办法,特在此提问,烦请进行解答,感谢

    我也遇到了这个问题,请问解决了吗

  6. sqs404 commented on Dec 31, 2023

    @sqs404

    sm2和sm9的examples都用不了,我也是这个问题

  7. KKKGod commented on Jan 10, 2024

    @KKKGod

    使用ctype和python指针交互出问题了 等开发者处理吧

  8. j-z10 commented on Mar 28, 2024

    @j-z10

    降低GmSSL版本到 v3.1.1

  9. yujingyue9166 commented on Mar 29, 2024

    @yujingyue9166

    降低GmSSL版本到 v3.1.1

    GmSSL 是3.1.1 依然是同样的问题

    import gmssl
    gmssl.GMSSL_LIBRARY_VERSION
    'GmSSL 3.1.1'
    gmssl.GMSSL_PYTHON_VERSION
    '2.2.2'

    Traceback (most recent call last):
    File "sm2_key.py", line 13, in
    sm2.export_encrypted_private_key_info_pem('sm2.pem', 'password')
    File "D:\anaconda3\lib\site-packages\gmssl.py", line 432, in export_encrypted_private_key_info_pem
    if gmssl.sm2_private_key_info_encrypt_to_pem(byref(self), c_char_p(passwd), c_void_p(fp)) != 1:
    OSError: exception: access violation writing 0x0000000000000024

  10. Zhanghanhan1 commented on Mar 29, 2024

    @Zhanghanhan1

    降低GmSSL版本到 v3.1.1

    可是装的就是3.1.1

  11. j-z10 commented on Mar 30, 2024

    @j-z10

    降低GmSSL版本到 v3.1.1

    看起来是windows下 pem_write 和 pem_read 导致的问题,我这个PR针对windows做了临时处理,把这一步放在python里面执行
    你们可以参考一下 j-z10/pygmssl#9

  12. ferstar commented on Oct 18, 2025

    @ferstar

    ✅ 问题已修复

    你好,感谢报告这个问题!

    这个 Windows 平台的 access violation 错误已经在 PR #26 中完全修复:
    #26

    🔧 修复内容

    根本原因:
    Windows 平台 FILE* 指针无法跨 DLL 边界传递,且 ctypes 参数传递存在双重包装问题。

    修复方案:

    1. 重构了 Windows 平台的 PEM 文件操作,使用 Python 原生 I/O 替代 FILE* 指针
    2. 修正了所有 c_char_p 参数的双重包装问题
    3. 影响范围:所有 SM2 和 SM9 的 PEM 加密导入/导出操作

    测试验证:

    • ✅ 修复了 12 个 Windows 平台单元测试(从失败到通过)
    • ✅ 在 Windows/Linux/macOS 三平台上通过了 124 个单元测试
    • ✅ 测试覆盖了 sm2_key.py 等所有示例程序

    📦 如何测试修复

    方式 1: 直接安装预编译包(最快,推荐)

    ```bash

    Windows (Python 3.8-3.14)

    pip install https://github.com/ferstar/GmSSL-Python/releases/download/v2.2.2/gmssl_python-2.2.2-py3-none-any.whl
    ```

    下载链接:https://github.com/ferstar/GmSSL-Python/releases/tag/v2.2.2

    方式 2: 从源码安装

    ```bash
    git clone https://github.com/GmSSL/GmSSL-Python.git
    cd GmSSL-Python
    git fetch origin pull/26/head:pr-26
    git checkout pr-26
    pip install -e .
    ```

    方式 3: 等待 PR 合并后

    ```bash
    pip install --upgrade gmssl-python
    ```

    🎁 额外改进

    PR #26 还包含了其他重大改进:

    • 📦 预编译 GmSSL 库打包,无需手动安装依赖
    • 🧪 测试覆盖率从 19 提升到 124 个测试
    • 🌍 完整的跨平台支持(Linux x86_64/ARM64, macOS, Windows)
    • 🔄 添加 CI 自动化测试

    欢迎测试反馈!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions