# How to print encrypted text on the console?

**URL:** <https://openfhe.discourse.group/t/how-to-print-encrypted-text-on-the-console/325>\
**Category:** Library Questions\
**Created:** [January 30, 2023, 11:40am UTC](https://openfhe.discourse.group/t/how-to-print-encrypted-text-on-the-console/325 "2023-01-30T11:40:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtovartr](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jtovartr](https://openfhe.discourse.group/u/jtovartr)\
**Post date:** [January 30, 2023, 11:40am UTC](https://openfhe.discourse.group/t/how-to-print-encrypted-text-on-the-console/325/1 "2023-01-30T11:40:41Z")

</div>

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?

Thanks!

---

<div class="post-metadata">

**Author:** ![dcousins](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/dcousins/32/71_2.png) [@dcousins](https://openfhe.discourse.group/u/dcousins)\
**Post date:** [January 30, 2023, 10:23pm UTC](https://openfhe.discourse.group/t/how-to-print-encrypted-text-on-the-console/325/2 "2023-01-30T22:23:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jtovartr](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jtovartr](https://openfhe.discourse.group/u/jtovartr)\
**Post date:** [January 31, 2023, 9:08am UTC](https://openfhe.discourse.group/t/how-to-print-encrypted-text-on-the-console/325/3 "2023-01-31T09:08:54Z")

</div>

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?

> [@dcousins](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![jtovartr](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jtovartr](https://openfhe.discourse.group/u/jtovartr)\
**Post date:** [February 1, 2023, 3:38pm UTC](https://openfhe.discourse.group/t/how-to-print-encrypted-text-on-the-console/325/4 "2023-02-01T15:38:05Z")

</div>

@dcousins sorry, I didn’t mention you

---

<div class="post-metadata">

**Author:** ![dcousins](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/dcousins/32/71_2.png) [@dcousins](https://openfhe.discourse.group/u/dcousins)\
**Post date:** [February 1, 2023, 4:48pm UTC](https://openfhe.discourse.group/t/how-to-print-encrypted-text-on-the-console/325/5 "2023-02-01T16:48:13Z")

</div>

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?](https://openfhe.discourse.group/t/is-there-a-limit-to-plaintext-size/329) for a good discussion.

---

<div class="post-metadata">

**Author:** ![jtovartr](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jtovartr](https://openfhe.discourse.group/u/jtovartr)\
**Post date:** [February 2, 2023, 8:59am UTC](https://openfhe.discourse.group/t/how-to-print-encrypted-text-on-the-console/325/6 "2023-02-02T08:59:14Z")

</div>

@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?

Thanks!

---

<div class="post-metadata">

**Author:** ![iquah](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/iquah/32/8_2.png) [@iquah](https://openfhe.discourse.group/u/iquah)\
**Post date:** [February 3, 2023, 2:24pm UTC](https://openfhe.discourse.group/t/how-to-print-encrypted-text-on-the-console/325/7 "2023-02-03T14:24:18Z")

</div>

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

```auto
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):

```auto
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?

> [@How to reduce the size of ckks ciphertext？](https://openfhe.discourse.group/t/how-to-reduce-the-size-of-ckks-ciphertext/290/2):
>
> Ciphertext expansion is a known cost with FHE. There are various research level approaches to reduce ciphertext but I think you are talking about what you can do right now. First, ciphertext size in CKKS are driven by three main things: native word size, ringsize, and multiplicative depth. these are ususally determined by the parameters selected when building the crypto context, and we recommend using one of the standard settings based on your security requirements. You already fixed your de…

HTH!

---

<div class="post-metadata">

**Author:** ![dcousins](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/dcousins/32/71_2.png) [@dcousins](https://openfhe.discourse.group/u/dcousins)\
**Post date:** [February 6, 2023, 3:38pm UTC](https://openfhe.discourse.group/t/how-to-print-encrypted-text-on-the-console/325/8 "2023-02-06T15:38:20Z")

</div>

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)?

---

<div class="post-metadata">

**Author:** ![jtovartr](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jtovartr](https://openfhe.discourse.group/u/jtovartr)\
**Post date:** [February 7, 2023, 8:28am UTC](https://openfhe.discourse.group/t/how-to-print-encrypted-text-on-the-console/325/9 "2023-02-07T08:28:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jtovartr](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jtovartr](https://openfhe.discourse.group/u/jtovartr)\
**Post date:** [February 7, 2023, 8:31am UTC](https://openfhe.discourse.group/t/how-to-print-encrypted-text-on-the-console/325/10 "2023-02-07T08:31:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jtovartr](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@jtovartr](https://openfhe.discourse.group/u/jtovartr)\
**Post date:** [February 7, 2023, 11:48am UTC](https://openfhe.discourse.group/t/how-to-print-encrypted-text-on-the-console/325/11 "2023-02-07T11:48:05Z")

</div>

@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?

> [@iquah](#):
>
> ```auto
> 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):
> 
> ```auto
> 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])
> ]
> 
> ```

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![dcousins](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/dcousins/32/71_2.png) [@dcousins](https://openfhe.discourse.group/u/dcousins)\
**Post date:** [February 7, 2023, 2:30pm UTC](https://openfhe.discourse.group/t/how-to-print-encrypted-text-on-the-console/325/12 "2023-02-07T14:30:46Z")

</div>

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](https://openfhe.discourse.group/t/matrix-multiplication/180) 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: [https://eprint.iacr.org/2019/223.pdf](https://eprint.iacr.org/2019/223.pdf) . The code will make sense only after looking at the paper. [GitHub - openfheorg/openfhe-genomic-examples](https://github.com/openfheorg/openfhe-genomic-examples)

Hope this helps.
