1

I'm using the SEAL library in C++. I would like to save the ciphertext to a file. I was thinking something like transforming it to a string type and saving it. I want to have a file with all the ciphertexts and when needed upload the file to memory to use them again. I will also have to save the encryption and decryption keys to be able to decrypt later the results.

Has someone used this cryptography library and knows how to save the ciphertexts generated to a file? I am just learning how to use this library and I'm new with C++, so I'm struggling with this.

Thanks!

Gabb
  • 11
  • 5
  • The Ciphertexts provide a `save()` and `load()` functions. You can use `save()` for saving the Ciphertext into a `stringstream` variable and then write them into a file. Later you can read from the file into a `stringstream` variable and use the `load()` function to load it in to a Ciphertext. But I think you should to Save your private key, public key and the defined parameters into files as well, if you want to decrypt the saved Ciphertexts later. Their instances also offer the `save()` and `load()` functions. – TalG Sep 19 '18 at 17:18
  • I've had the same intention in mind as you and asked about saving the Ciphertexts here https://stackoverflow.com/q/51416347/4418333 Of course I ended up using the provided `save()` and `load()` functions for the Ciphertexts. – TalG Sep 19 '18 at 17:27
  • Like TalG said you can use the `save()` and `load()` methods for all these types. Note that they write/read the contents to any stream, e.g. an `fstream`, so you don't have to use the `stringstream` in between when writing to a file. Make sure you use the `ios::binary` flag when opening the `fstream`. – Kim Laine Sep 20 '18 at 15:58
  • @TalG. Thanks, I read about these funtions save() and load() and the question that you have posted. I am saving the ciphertexts in a stringstream and then using stringstream.str() to save the content to a file. I'm not sure if using str() is correct. I'm getting lot of symbols and I haven't be able to load the ciphertext. Did you save your ciphertexts like that? I will read about ios::binary that Kim Laine mentioned to try to save the ciphertexts to fstream. Thanks to both of you. – Gabb Sep 20 '18 at 16:41

1 Answers1

0

This is the way I do.

For both operatios I use the API provided like this:

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

For loading again you have two ways:


/* 
  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;
};