Simple Encrypted Arithmetic Library (SEAL) how to save the ciphertext to a file

1.3k views Asked by At

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!

1

There are 1 answers

0
Javier Junquera Sánchez On

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