c++ - 在 Linux 中寻找将加密文本从 C++ 应用程序写入加密 gpg 文件的正确方法

标签 c++ linux gcc encryption gnupg

我有从 Solaris OS 移植的 Linux C++ 应用程序,该应用程序需要通过 gpg(或 gpg2)将一些文本数据写入加密文件。所以文本数据应该直接写入gpg(加密文件)。 我创建管道:

if(pipe(pipes) == -1)
  throw Exception("Error creating pipes");

switch(fork())
{
  case -1:
    throw Exception("fork() failure");
  case 0: // child process
    close(pipes[PIPE_PARENT]);
    close(READ);
    close(ERR);
    if(dup(pipes[PIPE_CHILD]) != READ)
      throw Exception("dup() failure, READ descriptor unavailable");

    execlp("gpg", "gpg", "--no-tty", "-r", "username", "-e", "-o", "home/username/out.pgp", NULL);
    break;
}

... 然后我使用 write():

intWCount = write(intTextFD, strData.str().c_str(), strData.str().size()); 

然后我看到 errno == 9,intWCount = -1。 从 STDOUT 我得到:

“gpg:警告:重新打开标准错误”

并且文本数据没有写入文件。 我使用 Ubuntu 16.04、gpg (GnuPG) 1.4.20、gcc 5.4.0。

主要问题是 - 如何将我的文本数据安全地写入加密的 gpg 文件?

谢谢!

最佳答案

https://wiki.gnupg.org/APIs声明如下:

A number of elder applications call GnuPG executables in a subprocess and interact with them via command line arguments and file-descriptors. This is less perferable to GPGME, because the command line arguments and text outputs are not an (official) API to GnuPG. While there is an effort made to keep them stable, using the official GPGME API can manage this more precisely and thus you end up with a more robust solution.

正确的方法是使用 gpgme 作为 api,它有 c++ 绑定(bind)。

如果您绝对确定要自己完成此操作,则可能需要考虑 popen()ening 二进制文件而不使用“--no-tty”并写入生成的文件指针。不过,这是次优的,因为 GnuPG 习惯于提出问题,您需要准备好让您的程序回答问题。

最好使用 API。

关于c++ - 在 Linux 中寻找将加密文本从 C++ 应用程序写入加密 gpg 文件的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42389350/

相关文章:

c# - 非常基本的代码性能

c - 如果我是一线程写多读,怎么只写锁不读锁呢?

linux - 如何在linux中使用我自己的动态库(Makefile)

c++ - 如何使用 __attribute__((visibility ("default")))?

c - 局部变量在栈上的分配顺序

c - 为什么 gcc 从不存在的文件夹中搜索头文件?

c++ - Opencv 3.3.1 c++ 跟踪导致随机崩溃

c++ - 为什么从 fscanf 读取了不正确的数据?

c++ - kAudioDevicePropertyNominalSampleRate 的 AudioObjectAddPropertyListener

Linux TortoiseSVN