How to print encrypted text on the console?

I am new using the library and I wonder if there is any method to serialize and display the information of a ciphertext by console in order to save it in a database.

I have seen that there is the SerializeToFile method to save the ciphertext in a file, is there another one to save it in a variable and show it by console?


Hi, this is a common question, you may want to review some of the existing library questions.
by design a ciphertext is encrypted data. so not sure what you want to look at.
A ciphertext is a C++ object. It can be serialized to a binary buffer or a json string using the Serialize method and both could be written directy to files using the method you pointed out. Both will be very, very large. Saving a ciphertext in a conventional data base may not be the best thing to do. Rather you may want to use a map with a <key, CT> pair. or a <key, CT file> pair, or save the filename in a database.

It all depends on your application.

Also remember that for the BGV,BFV,CKKS family of schemes, CT can most efficiently contain a vector of data, and all operations are done on the vector in a SIMD fashion, so that may affect your design as well.

Thank you very much for your reply.

Could you explain this in more depth? Does this mean that all libraries using BGV, BFV and CKKS will generate a long ciphertext?

I am new to homomorphic encryption and I would like to use it to encrypt data, store it in a database and then perform operations with it (addition, multiplication, etc) and in the future apply machine learning techniques on this encrypted data.

@dcousins sorry, I didn’t mention you

yes, while you can encrypt a single value, each of those schemes supports encrypting a vector. See @Caesar 's post Is there a limit to plaintext size? for a good discussion.

@dcousins I have read the post but it is not exactly what I would like to know. Like you said in your first answer:

CT can be serialized to a binary buffer or a json string using the Serialize method and both could be written directy to files using the method you pointed out. Both will be very, very large. Saving a ciphertext in a conventional data base may not be the best thing to do

A CT has a very large value and size so with a database with many CTs it will not be scalable as it would require a lot of storage space, right?

If I want to store this in a database like MySQL how would you advise me to do it? or is it not recommended?


@jtovartr There’s a bit of nuance here that might go over your head (especially as a newbie). Say we have the following dataset:

X = [
    [ 0,  1,  2,  3],
    [ 4,  5,  6,  7],
    [ 8,  9, 10, 11],
    [12, 13, 14, 15],
    [16, 17, 18, 19]

Two things to keep in mind:

  1. Oftentimes, we have more rows than columns in the dataset.
  2. Encryption is slow.

So, instead of encrypting row-by-row, we encrypt column-wise. So, we might have something like the following (think of this as a transpose and then an encrypt):

encX = [
       enc([ 0,  4,  8, 12, 16, 0,0,...0]),
       enc([ 1,  5,  9, 13, 17, 0,0,...0]),
       enc([ 2,  6, 10, 14, 18, 0,0,...0])),
       enc([ 3,  7, 11, 15, 19, 0,0,...0])

in general, the number of generated ciphertexts is the number of features that we have. One question you may have is “Where did those extra 0’s come from?” and the answer is that it’s for the sake of security. These extra 0’s are part of what makes these ciphertexts so large. Depending on your security parameters, each ciphertext can come up to storing 131_072 integers at once. Depending on your use-case that might be acceptable but I think it depends.

Extra Details

Want to know what other things affect your ciphertext size?


when you select all the paramters for a scheme, you will get a resutling ciphertext size. it is based on how much computation (multiplicative depth) you want to do before bootstrapping, as well as security settings.

you can easily determine the size of a Ciphertext by serializing it to file or to a stringstream and looking at the resulting size (I don’t think there is a method that does this directly though I could be wrong). So it depends on the application.

The main question is, what do you want to encrypt, and what do you want to do with it after it is encrypted (assuming it is stored in a DB and retrieved in your application)?

1 Like

Thank you very much for explaining it to me in more detail, now I understand it better.

The main objective, as you say, is to encrypt data, store them in a database and be able to retrieve these encrypted data to be able to apply different operations on them, such as addition, multiplication, calculation of averages, obtaining statistics of the encrypted data.

And in the future, be able to apply machine learning techniques on this encrypted data.

@iquah I am thinking about the information you provided me and I want to pass the following example to real code, but how can I encrypt a matrix or a vector of vectors like the example you provided me?

but when you try to encrypt a value it must be a vector to transform it to plain text as in the following example:

std::vector<int64_t> vectorOfInts1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
Plaintext plaintext1               = cryptoContext->MakePackedPlaintext(vectorOfInts1);
auto ciphertext1 = cryptoContext->Encrypt(keyPair.publicKey, plaintext1);

How can I encrypt the matrix as if it were a dataset like the example you gave me?

Hi, there are a couple of ways to do matrix vector and matrix matrix operations in the literature, most trade efficiency for complexity in implementation. We eventually plan to release a sub-library for this, but it won’t be available in the short term.

In response to the question of how to encrypt a matrix, I suggest looking at Matrix multiplication posted by a user to see how they implemented some basic matrix operations. It may not be the most efficient but you can use that as a starting point.

There is a very detailed example of an applicatoin that uses matrix arithmetic, but it is highly optimized for speed, and not in an easily digestable form (i.e. it is all inline code). It may be difficult to understand. It is based on a IDASH paper: . The code will make sense only after looking at the paper. GitHub - openfheorg/openfhe-genomic-examples

Hope this helps.

1 Like