使用 OpenSSL ECC 加密/解密文本字符串
如何使用 OpenSSL 的 ECC 支持来加密或解密文本字符串? 我能够使用 OpenSSL API 生成 ECC 私钥/公钥,但我不知道如何使用这些密钥加密纯文本。
How can I use OpenSSL's ECC support to encrypt or decrypt a text string? I am able to generate ECC private/public keys using OpenSSL APIs, but I don't know how to encrypt plain text using those keys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于很难找到展示如何使用 ECC 加密数据的示例,因此我想发布一些代码供其他人使用。 有关完整列表,请查看我的 openssl-dev 帖子:
http: //www.mail-archive.com/[email protected]/msg28042.html
基本上它是一个可用版本如何使用 ECDH 来保护数据块。 ECDH 用于生成共享秘密。 然后使用 SHA 512 对共享密钥进行哈希处理。生成的 512 位被拆分,其中 256 位用作对称密码的密钥(在我的示例中为 AES 256),其他 256 位用作 HMAC 的密钥。 我的实施大致基于 SECG 工作组概述的 ECIES 标准。
关键函数是 ecies_encrypt() ,它接受十六进制形式的公钥并返回加密数据:
和 ecies_decrypt() ,它再次以十六进制形式获取私钥,并解密先前安全的缓冲区:
我发布这个是因为我个人找不到任何其他有关如何使用 ECC 和 OpenSSL 库保护文件的示例。 也就是说,值得一提的是不使用 OpenSSL 的替代方案。 一种是安全的,它遵循与我的示例类似的模式,只是它依赖于 libgcrypt。 由于 libgcrypt 不提供所需的所有底层 ECC 功能,因此安全程序填补了空白并实现了 libgcrypt 中缺少的 ECC 逻辑。
另一个值得关注的程序是 SKS,它使用与上面的示例类似的基于 ECC 的加密过程,但没有任何外部依赖项(因此所有 ECC 代码都在那里供您查看)。
Since its so hard to find examples showing how to use ECC to encrypt data I thought I'd post some code for others to use. For the complete listing, check out my openssl-dev posting:
http://www.mail-archive.com/[email protected]/msg28042.html
Basically its a flushed out usable version of how to use ECDH to secure a block of data. ECDH is used to generate a shared secret. The shared secret is then hashed using SHA 512. The resulting 512 bits are split up, with 256 serving as the key to the symmetric cipher (AES 256 in my example) and the other 256 bits used as the key for the HMAC. My implementation is loosely based on the ECIES standard outlined by SECG working group.
The key functions are ecies_encrypt() which accepts the public key in hex form and returns the encrypted data:
And ecies_decrypt() which takes the private key, again in hex form, and decrypts the previously secured buffer:
I'm posting this because I personally couldn't find any other examples of how to secure files using ECC and the OpenSSL library. That said its worth mentioning alternatives that don't use OpenSSL. One is seccure which follows a pattern similar to my example, only it relies libgcrypt. Since libgcrypt doesn't provide all of the underlying ECC functions needed, the seccure program fills in the gaps and implements the ECC logic missing from libgcrypt.
Another program worth looking at is SKS, which uses a similar ECC based encryption process as the example above, but doesn't have any external dependencies (so all the ECC code is right there for you to look at).
ECC 本身并没有真正定义任何加密/解密操作——基于椭圆曲线的算法可以定义。
椭圆曲线 Diffie-Hellman 就是一个例子。 您可以通过以下方式使用 ECDH 加密消息:
解密:
编辑:以下是使用 ECDH 生成秘密的基本思想。 首先,我们需要定义一个密钥派生函数 - 该函数使用 SHA1 哈希。
这是发送方的 ECDH 代码。 它假设接收者的公钥已在“recip_key”中,并且您已使用 EC_KEY_check_key() 对其进行了验证。 为了简洁起见,它还省略了许多重要的错误检查,您肯定希望将其包含在生产代码中。
此后,缓冲区“buf”包含 20 个字节的可用于键控的材料。 这个简短的示例基于 openssl 源代码分发中的“ecdhtest.c”中的代码 - 我建议看一下它。
您需要将 ephemeral_key 的公钥部分与加密消息一起发送,并安全地丢弃私钥部分。 数据上的 MAC 也是一个好主意,如果您需要超过 20 个字节的密钥材料,则可能需要更长的哈希值。
接收者执行类似的操作,只不过其私钥已经存在(因为发送者必须事先知道相应的公钥),并且公钥是从发送者处接收的。
ECC itself doesn't really define any encryption/decryption operations - algorithms built on elliptic curves do.
One example is Elliptic-Curve Diffie-Hellman. You could encrypt a message using ECDH by:
To decrypt:
EDIT: The following is the basic idea to generate a secret using ECDH. First we need to define a key derivation function - this one uses the SHA1 hash.
This is the ECDH code for the sender side. It assumes that the recipient's public key is already in "recip_key", and you have verified it with EC_KEY_check_key(). It also omits much important error checking, for the sake of brevity, which you will definitely want to include in production code.
After this the buffer 'buf' contains 20 bytes of material you can use for keying. This abbreviated example is based on the code in "ecdhtest.c" in the openssl source distribution - I suggest taking a look at it.
You will want to send the public key portion of ephemeral_key with the encrypted message, and securely discard the private key portion. A MAC over the data is also a good idea, and if you need more than 20 bytes of keying material a longer hash is probably in order.
The recipient does something similar, except that its private key already exists (since the sender had to know the corresponding public key beforehand), and the public key is recieved from the sender.