使用openssl加密和解密小文件
我想用 C/C++ 编写一个小程序,它读取一个小文本文件,并使用“内部”密钥对其进行加密。 然后我还想编写另一个小程序,它可以使用内部相同的密钥解密加密的文件。
我查看了 openSSL 网站并用谷歌搜索,但发现不是简单的例子,有人尝试过做这件事吗?
I want to write a small program in C/C++ which reads a small text file, and encrypts it, using a "internal" key. Then I also want to write another small program which can decrypt the encrypted file using internally the same key.
I looked at openSSL site and googled but found not simple example, has somebody ever tried to do this thing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
理想情况下,您可以使用
ccrypt
等现有工具,但这里是:通过调用
AES_cfb128_encrypt
并使用AES_DECRYPT
作为最后一个参数来完成解密。 请注意,此代码仅经过最基本的测试,并且您确实应该为 ckey 和 ivec 使用正确的 8 位随机数据。编辑:
AES_cfb128_encrypt
似乎接受任意长度的数据,因此您不需要以AES_BLOCK_SIZE
(16) 字节的块进行加密。Ideally, you could use an existing tool like
ccrypt
, but here goes:Decryption is done by calling
AES_cfb128_encrypt
withAES_DECRYPT
as the last parameter. Note that this code hasn't been given anything more than the most elementary of testing, and that you really should use proper 8-bits random data for ckey and ivec.EDIT: It seems
AES_cfb128_encrypt
accepts data of arbitrary length, so you're not required to encrypt in blocks ofAES_BLOCK_SIZE
(16) bytes.以前的答案已经向您指出了如何做您所要求的事情。
我想补充一下为什么你可能不应该这样做。
您所说的称为“对称加密”(使用相同的密钥进行加密和解密,而不是非对称加密,其中使用一个密钥加密的所有内容只能由特定的对应方解密)。
反汇编可执行文件以确定正在使用的硬编码密钥几乎是微不足道的。 这意味着,任何获得您的可执行文件的人都可以破解任何已交换消息的加密。
除非您想要的应用程序非常具体,否则这意味着您的设置可能“看起来”安全,但事实并非如此。 在这些情况下,通常最好根本不加密,这样就不会有人陷入这种错误的安全感...
您希望使用标准库来进行加密(而不是实现/创建算法),这是非常好的您自己),但是协议(如何应用程序、密钥和消息的使用和交换)至少与密码本身一样重要。 您可能希望让从事密码学的人测试您的想法,以告诉您弱点。 (我确信 StackOverflow 上有足够多的此类内容。;-))
Previous answers have pointed you towards how to do what you asked for.
I'd like to add a word on why you probably shouldn't do this.
What you are talking about is called "symmetric encryption" (the same key is used for encrypting and decrypting, as opposed to asymmetric encryption where everything encrypted with one key can only be decrypted by a specific counterpart).
Disassembling an executable to determine a hardcoded key being used is next-to-trivial. That means, anyone who gets his/her hands on one of your executables, ever, can break the encryption of any message ever exchanged.
Unless the application you have in mind is very specific, this means your setup might "look" secure, but isn't. In these cases, it's usually better not to encrypt at all, so that no-one involved falls for that false sense of security...
It's very good you are looking to standard libraries to do the encryption (instead of implementing / creating an algorithm yourself), but the protocoll (how applications, keys, and messages are used and exchanged) is at least as important as the cipher itself. You might want to have your ideas tested by someone dealing in cryptography, to tell you the weaknesses. (I'm sure there's enough of that kind here at StackOverflow. ;-) )
OpenSSL 特别关注 SSL 和 TLS 的实现,它们是通过网络加密数据的协议。 由于您只想加密文件,因此可以使用 OpenSSL,但并不理想。
相反,我会使用类似 BeeCrypt 或 Crypto++® Library 5.6.0 两者都提供了使用示例。
OpenSSL is specifically concerned with implementing SSL and TLS which are protocols for encrypting data over a network. Since you are just looking to encrypt a file, it is possible to use OpenSSL but not ideal.
Instead, I would use something like BeeCrypt or Crypto++® Library 5.6.0 which both provide examples for their use.