You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

liboqs-python报错AttributeError:模块'oqs'无指定属性

运行Open Quantum Safe示例代码时出现AttributeError的问题

我尝试运行Open Quantum Safe的KEM示例代码,却遭遇报错:

(myenv) user@mx:~
$ python3 '/home/user/Documents/Dev/quantum_algo_tests/# Key encapsulation Python example.py' 
Enabled KEM mechanisms:
Traceback (most recent call last):
  File "/home/user/Documents/Dev/quantum_algo_tests/# Key encapsulation Python example.py", line 9, in <module>
    kems = oqs.get_enabled_kem_mechanisms()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'oqs' has no attribute 'get_enabled_kem_mechanisms'
(myenv) user@mx:~
$

用inspect查看该oqs模块的内容,得到:

ExpressionInput
FunctionNode
OQSInterpreter
oqs_engine

随后我又尝试了文档中的基础示例代码:

import oqs
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
import os
import base64

# Message to be encrypted
message = "Hello world!"

# Step 1: Key Encapsulation using Kyber
kemalg = "Kyber512"
with oqs.KeyEncapsulation(kemalg) as client:
    # Client generates keypair
    public_key = client.generate_keypair()

    # Server (could be another party) encapsulates secret with client's public key
    with oqs.KeyEncapsulation(kemalg) as server:
        ciphertext, shared_secret_server = server.encap_secret(public_key)
    
    # Client decapsulates to get the same shared secret
    shared_secret_client = client.decap_secret(ciphertext)

# The shared secret is now the same for both client and server and will be used as the encryption key
assert shared_secret_client == shared_secret_server, "Shared secrets do not match!"

# Step 2: Encrypt the message using AES-GCM with the shared secret
iv = os.urandom(12)
cipher = Cipher(algorithms.AES(shared_secret_client), modes.GCM(iv), backend=None)
encryptor = cipher.encryptor()
ciphertext = encryptor.update(message.encode()) + encryptor.finalize()

# The tag ensures the integrity of the message
tag = encryptor.tag

# Combine the IV, ciphertext, and tag into a single encrypted package
encrypted_message = base64.b64encode(iv + ciphertext + tag)

print(f"Encrypted message: {encrypted_message.decode()}")

# Step 3: Decryption process (using the shared secret derived from Kyber)

# Decrypt the message
decryptor = Cipher(algorithms.AES(shared_secret_server), modes.GCM(iv, tag), backend=None).decryptor()
decrypted_message = decryptor.update(ciphertext) + decryptor.finalize()

print(f"Decrypted message: {decrypted_message.decode()}")

依然出现类似报错:

$ python3 /home/user/Documents/Dev/quantum_algo_tests/hello_world_example.py 
Traceback (most recent call last):
  File "/home/user/Documents/Dev/quantum_algo_tests/hello_world_example.py", line 11, in <module>
    with oqs.KeyEncapsulation(kemalg) as client:
         ^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'oqs' has no attribute 'KeyEncapsulation'

为何如此简单的程序会抛出该错误?


问题原因与解决方案

核心问题:你安装的oqs库并非Open Quantum Safe官方的liboqs-python,而是一个同名的无关第三方库。从你用inspect查到的模块内容(ExpressionInput、FunctionNode等)可以确认,这和官方liboqs-python的结构完全不符。

解决步骤:

  1. 卸载当前错误的oqs库:
pip uninstall oqs -y
  1. 安装官方的liboqs-python库:
pip install liboqs-python
  1. 验证安装是否正确:
    进入Python交互环境,执行以下代码:
import oqs
print(dir(oqs))

如果输出中包含KeyEncapsulation、get_enabled_kem_mechanisms等属性,说明安装正确,此时再运行示例代码即可正常执行。

内容的提问来源于stack exchange,提问作者Emily

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 16:05:04