我使用 liboqs 中的 FALCON 和 OQS 中的 python 包装器。我有 2 个 python 文件,一个用于生成公钥、散列并将结果转储到外部文件。而第二个文件将用于验证从外部文件中提取的公钥的哈希值。
下面是生成密钥、散列和验证的完整代码。
message = "Secret messagea".encode('utf-8')
print(message)
algo = "Falcon-512"
with oqs.Signature(algo) as falcon:
print("\nDetails:")
pprint(falcon.details)
print("\n\n\n============================================")
pub_key = falcon.generate_keypair()
pri_key = falcon.export_secret_key()
sign = falcon.sign(message)
check = falcon.verify(message, sign, pub_key)
print("\n Valid signature?", check)
到目前为止我已经尝试过: 将密钥和哈希值直接转储到字节格式的 .txt
编写代码
with open(file, "wb") as file:
file.write(key)
file.write(hash)
阅读代码
with open(file, "rb") as file:
key = file.read()
hash = file.read()
提取时,数值不同
我尝试转换为 Base64,但是当我将其从 Base64 解码为字节时,值是不同的。下面是从base64解码的代码
pub_key = base64.b64decode(pub_key)
我有研究使用 .pem 文件格式,下面是写入 .pem 的函数
from cryptography.hazmat.primitives import serialization
def writepem(key):
pem_key = key.public_bytes(encoding=serialization.Encoding.PEM)
with open(file, "wb") as file:
file.write(pem_key)
但是它导致了如下所示的错误
AttributeError: 'bytes' object has no attribute 'private_bytes'
我不知道如何正确使用密码模块。对于加密编程来说相当陌生。
你能告诉我你是如何使用 python 包装器让 facon 算法为你工作的吗?