C/C++使用公钥加密/解密
我正在寻找两个概念上类似于这些的函数:
// returns the encrypted text
string encrypt( string public_key, string pass_phrase, string text );
// returns the original text
string decrypt( string private_key, string pass_phrase, string encrypted_text );
其中 string
可以是 char*
、std::string
或易于转换的东西对那两个人。其中 public_key
和 private_key
基本上可以是任何东西,从使用某些命令(gpg/ssl 等)生成的密钥,到使用其他简单生成的密钥强>功能。
我研究了一些密码学库(libgcrypt、libgpgme、openssl ...),但使用这些库实现此类功能看起来并不容易:它们需要有关非对称加密的非肤浅知识以及很多知识的代码。
无论如何,这个任务似乎并不罕见。 如何实现上面两个功能呢?
I'm looking for two functions conceptually similar to these:
// returns the encrypted text
string encrypt( string public_key, string pass_phrase, string text );
// returns the original text
string decrypt( string private_key, string pass_phrase, string encrypted_text );
where string
could be a char*
, a std::string
or something easily convertible to those two. And where public_key
and private_key
can be basically anything, from keys generated with some commands (gpg/ssl stuff or whatever), to keys generated with other simple functions.
I've looked into a few cryptography libraries (libgcrypt, libgpgme, openssl ...), but it doesn't look easy at all to implement such functions with those libraries: they require a non-superficial knowledge about asymmetric encryption and a lot of code.
Anyway this task doesn't seem uncommon.
How can I implement the two functions above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,加密总是需要对所涉及的算法有非肤浅的了解。很难做到正确。 《应用密码学手册》是一本关于各种可用算法的相对可读的指南,因此可能值得一看。
您也可以尝试 cryptlib。它似乎有一个分层良好的设计,为您提供了许多参数的合理默认值,因此您可以开始使用,而不必过多担心细节。
Unfortunately, encryption always requires a non-superficial knowledge of the algorithms involved. It is hard to get right. The "Handbook of Applied Cryptography" is a relatively readable guide to the various algorithms available so it's probably worth a look.
You could also try cryptlib. It seems to have a well-layered design that gives you sensible defaults for a lot of parameters so you can hopefully get started without having to worry too much about the details.
当有人要求轻松加密时,我只能推荐 KeyCzar。
它不仅提供了多种语言的干净界面(可以使用相同的密钥),而且还提供了处理密钥轮换等的机制。
当然,所实现的算法的安全默认值使您不必担心技术细节。
真的,越简单越好到目前为止我见过的安全组合。
When someone asks for easy encryption, I can only recommend KeyCzar.
It not only provides a clean interface in several languages (that can use the same keys) but also mechanisms to handle key rotations and the like.
And of course, safe defaults for the algorithms implemented so that you don't have to worry about the technical details.
Really, the better easy & safe combination I've seen so far.
假设您不需要与平台无关的东西,下一代加密技术 (CNG) 是 Windows 上相对较新的加密 API,并且非常直观且易于使用。我写了一篇文章,其中包含典型应用程序中可能需要的所有主要加密操作的示例。本文的示例代码还提供了使用 Visual C++ 编译器进行这些操作的完整工作示例。
http://msdn.microsoft.com/en-us/magazine/cc163389.aspx 要将公钥
和私钥视为字符串,您可以简单地使用 Base64 或类似的编码。
Assuming you don’t need something platform agnostic, Cryptography Next Generation (CNG) is a relatively new cryptography API on Windows and is surprisingly intuitive and easy to use. I wrote an article that includes examples of all the main cryptographic operations you’re likely to need in typical applications. The sample code for the article also provides a complete working example for these operations using the Visual C++ compiler.
http://msdn.microsoft.com/en-us/magazine/cc163389.aspx
To treat public and private keys as strings you could simply use Base64 or a similar encoding.