C/C++使用公钥加密/解密

发布于 2024-10-01 03:19:50 字数 619 浏览 2 评论 0原文

我正在寻找两个概念上类似于这些的函数:

// 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_keyprivate_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我也只是我 2024-10-08 03:19:50

不幸的是,加密总是需要对所涉及的算法有非肤浅的了解。很难做到正确。 《应用密码学手册》是一本关于各种可用算法的相对可读的指南,因此可能值得一看。

您也可以尝试 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.

怪我闹别瞎闹 2024-10-08 03:19:50

当有人要求轻松加密时,我只能推荐 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.

看轻我的陪伴 2024-10-08 03:19:50

假设您不需要与平台无关的东西,下一代加密技术 (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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文