1

我在 C++ 中使用 SEAL 库。我想将密文保存到文件中。我正在考虑将其转换为字符串类型并保存它。我想要一个包含所有密文的文件,并在需要时将文件上传到内存以再次使用它们。我还必须保存加密和解密密钥,以便以后解密结果。

有人使用过这个密码库并且知道如何将生成的密文保存到文件中吗?我只是在学习如何使用这个库,而且我是 C++ 新手,所以我正在为此苦苦挣扎。

谢谢!

4

1 回答 1

0

我就是这样做的。

对于这两种操作,我都使用这样提供的 API:

void saveCiphertext(Ciphertext encrypted, string filename){
  ofstream ct;
  ct.open(filename, ios::binary);
  encrypted.save(ct);
};

要再次加载,您有两种方法:


/* 
  If you can't / don't want / don't need to verify the encryption parameters
*/
Ciphertext unsafe_loadCiphertext(string filename){

  ifstream ct;
  ct.open(filename, ios::binary);
  Ciphertext result;
  result.unsafe_load(context);

  return result;
};

// Verifying encryption parameters
Ciphertext loadCiphertext(string filename, EncryptionParameters parms){

  auto context = SEALContext::Create(parms);

  ifstream ct;
  ct.open(filename, ios::binary);
  Ciphertext result;
  result.load(context, ct);

  return result;
};
于 2019-08-17T12:34:33.103 回答