我试图将iPhone中的公钥传递给其他方,但是我无法使用iOS的输出。在
let parameters: [String: Any] = [
kSecAttrKeySizeInBits as String: 384,
kSecAttrKeyType as String: kSecAttrKeyTypeEC,
kSecPrivateKeyAttrs as String: [
kSecAttrIsPermanent as String: false
]
]
var error: Unmanaged<CFError>?
let privateKey = SecKeyCreateRandomKey(parameters as CFDictionary, &error)
let publicKey = SecKeyCopyPublicKey(privateKey!)
let pub = SecKeyCopyExternalRepresentation(publicKey!, &error)
let pubData = pub as Data?
print(pubData!.base64EncodedString())
输出示例:
BJSCZtBatd2BYEHtyLB0qTZNlphKf3ZTGI6Nke3dSxIDpyP9FWMZbG0zcdIXWENyndskfxV0No/yz369ngL2EHZYw6ggNysOnZ5IQSPOLFFl44m1aAk0o0NdaRXTVAz4jQ==
在python中(我的第二方在那里),我有以下内容:
^{pr2}$我得到的错误是ValueError: Could not deserialize key data.
那么,文档中描述的SecKeyCopyExternalRepresentation
的输出到底是什么:
The method returns data in the PCKS #1 format for an RSA key. For an elliptic curve public key, the format follows the ANSI X9.63 standard using a byte string of 04 || X || Y. For an elliptic curve private key, the output is formatted as the public key concatenated with the big endian encoding of the secret scalar, or 04 || X || Y || K. All of these representations use constant size integers, including leading zeros as needed.
如何描述X6.93
格式?如何将它转换成可以在python代码中使用的东西呢?在
另外,我已经尝试向xcode输出添加诸如-----BEGIN PUBLIC KEY-----
这样的头。在
我还没有找到这个问题的答案,因为我仍然不知道苹果到底提供了什么输出,但是,我想出了一个在this key import export manager中找到的解决方案。在
输出示例:
Exporting EC raw key: 97 bytes -BEGIN PUBLIC KEY - MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEFpCnTrJFQq0mZBvy+vzl9noKLZ4/s1cf I6hygug6s8dvBreMhabAcAbbhSa1losjCxV450nq92W9ZymonYasaAuhshDWjmvx 2qTXHEpVEVb9GawqX6XqpWtIBf+meHKS -END PUBLIC KEY -
python中的实现using cryptography
^{pr2}$输出如下:
<cryptography.hazmat.backends.openssl.ec._EllipticCurvePublicKey object at 0x7fb4f6f50e10>
请注意,您必须自己在python中添加新行,才能使这一切正常工作。在
键的格式不正确。它似乎是一个EC密钥。我怀疑它是公开的,详细说明是
04 || X || Y
,但我可能错了。你知道什么领域的钥匙结束了吗?该字段告诉您X
和Y
中有多少字节。在相关问题 更多 >
编程相关推荐