# Encrypt a cv::Mat

**URL:** https://forum.opencv.org/t/encrypt-a-cv-mat/9295
**Category:** C++
**Tags:** programming
**Created:** [June 26, 2022, 1:24pm UTC](https://forum.opencv.org/t/encrypt-a-cv-mat/9295 "2022-06-26T13:24:32Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Christophe\_Jacquelin](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/christophe_jacquelin/32/135_2.png) [@Christophe\_Jacquelin](https://forum.opencv.org/u/Christophe_Jacquelin)
#### Post date: [June 26, 2022, 1:24pm UTC](https://forum.opencv.org/t/encrypt-a-cv-mat/9295/1 "2022-06-26T13:24:32Z")

</div>

Hello, How to encrypt a cv::Mat with a home made encryption algorithm ?  
Thank you

---

<div class="post-metadata">

### Author: ![Micka](https://avatars.discourse-cdn.com/v4/letter/m/7c8e57/32.png) [@Micka](https://forum.opencv.org/u/Micka)
#### Post date: [June 26, 2022, 2:49pm UTC](https://forum.opencv.org/t/encrypt-a-cv-mat/9295/2 "2022-06-26T14:49:44Z")

</div>

There is a .data pointer which you can use to access the raw byte data, but pay attention to possible padding. And you will have to remember things like pixel format and image resolution if you later want to use the decrypted data in another cv::Mat

Another way could be to first cv::imencode the data to serial byte array. However encoding to .png will take some processing time. Maybe cv:: imencode to .bmp format is a good way.

Process then would be:  
Encrypt:

1. Mat image input
2. imencode
3. encryption on the byte array

Decrypt:

1. Decryption
2. imdecode
3. Mat image output

---

<div class="post-metadata">

### Author: ![imneonizer](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/imneonizer/32/2506_2.png) [@imneonizer](https://forum.opencv.org/u/imneonizer)
#### Post date: [June 30, 2022, 2:19pm UTC](https://forum.opencv.org/t/encrypt-a-cv-mat/9295/3 "2022-06-30T14:19:50Z")

</div>

Take a look at [GitHub - imneonizer/imcrypt: Auto Encrypt anything with a private / local key.](https://github.com/imneonizer/imcrypt)
