tink 的第一次用户(整体密码学菜鸟,但我正在学习)
我可以使用
创建私钥tinkey create-keyset --key-template [MY TEMPLATE] --out-format json --out foo.json
然后,一旦我有了该文件,我就会尝试通过
创建公钥集tinkey create-public-keyset --in-format=json --in=foo.json
输出是
{"primaryKeyId":3104918340,"key":[{"keyData":{"typeUrl":"type.googleapis.com/google.crypto.tink.HpkePublicKey","value":"EgYIAxACGAIaYQTKiU+xhistDls5CMGMC311ZRfELnUGdfLXBx++SHiMOMOzsaFryaKljLlHHKegOeC6vbG8AXXpoPrvaouxtU5CgHCXGEczYwo9p/PHN4gKnJFfJJWCerzC5lEtV4SJUVo=","keyMaterialType":"ASYMMETRIC_PUBLIC"},"status":"ENABLED","keyId":3104918340,"outputPrefixType":"TINK"}]}
所以现在我需要通过网络将 pem 发送到服务器,但我的服务器团队不断告诉我,我向他们提供了无效的 pem 文档。我的理解是上面 json 中的 valued 是 pem 格式。有什么建议吗?
正如总裁 James K. Polk指出的那样,您一开始就没有在处理 PEM。
您显示的输出是keyset 的 JSON 表示形式,这是 Tink 自己的用于存储密钥的格式。 value
字段内的值是base64编码的二进制数据,而不是PEM或DER格式的数据。
Tink 密钥对象的结构方式封装了加密操作的所有必要信息,包括密钥材料本身以及密钥类型、状态和密钥 ID 等元数据。 Tink 密钥对象中的keyData
字段包含实际的密钥材料,以特定于类型的格式进行编码,并且通常在 JSON 输出中进行 Base64 编码。 所以你必须:
typeUrl
字段了解加密算法和密钥类型(例如 RSA、ECDSA、AES)。
value
对象中解码 base64 编码的
keyData
字段以获取原始密钥材料。
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
import base64
import json
# Placeholder: Load your Tink keyset JSON, identify the key type, and decode the key material
# That is very dependent on the key type and the specifics of the Tink key format
# Assuming you have the key material as a Python bytes object for an RSA key
key_material = b'...' # That would be the result of decoding the base64 'value' for the correct key type
# Convert the RSA key material to an RSA key object (example)
# Again, you will need to adapt that, based on actual key type and format
public_key = serialization.load_der_public_key(key_material)
# Convert to PEM format
pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
print(pem.decode('utf-8'))