Code base obscuration (for images) 1.0
It implements different obscuration methods on portable images (.pgm and .ppm)
Loading...
Searching...
No Matches
PaillierControllerPGM.hpp
Go to the documentation of this file.
1
11#ifndef PAILLIERCONTROLLER_PGM
12#define PAILLIERCONTROLLER_PGM
13
14#include <stdio.h>
15#include <cctype>
16#include <fstream>
17#include <string>
18#include <string_view>
19#include <stdio.h>
20#include <ctype.h> //uintN_t
21#include <bitset> //Bitwise operators
22
27
38{
39
40private:
41 char *c_file;
43public:
53
61
66 void init();
67
76 const char *getCFile() const;
77
85 void setCFile(char *newCFile);
86
104 void checkParameters(char *arg_in[], int size_arg, bool param[]);
105
112 void printHelp();
113
114 /*********************** Encryption/Decryption ***********************/
125 uint8_t histogramExpansion(OCTET ImgPixel, bool recropPixels);
126
127 /************** 8bits **************/
141 template <typename T_in, typename T_out>
142 void encrypt(bool distributeOnTwo, bool recropPixels, Paillier<T_in, T_out> paillier);
143
156 template <typename T_in, typename T_out>
157 void decrypt(bool distributeOnTwo, Paillier<T_in, T_out> paillier);
158
170 template <typename T_in, typename T_out>
171 void encryptCompression(bool recropPixels, Paillier<T_in, T_out> paillier);
172
188 uint16_t *compressBits(uint16_t *ImgInEnc, int nb_lignes, int nb_colonnes);
189
201 uint16_t *decompressBits(uint16_t *ImgInEnc, int nb_lignes, int nb_colonnes, int nTailleOriginale);
202
213 pair<int, int> decomposeDimension(int n);
214
226 template <typename T_in, typename T_out>
228
229 /************** n > 8bits**************/
230
231 // /**
232 // * \brief
233 // * \details
234 // * \param bool distributeOnTwo
235 // * \param bool recropPixels
236 // * \param Paillier<T_in, T_out> paillier
237 // * \authors Katia Auxilien
238 // * \date 29 May 2024, 13:55:00
239 // */
240 // template <typename T_in, typename T_out>
241 // void encrypt2(bool distributeOnTwo, bool recropPixels, Paillier<T_in,T_out> paillier);
242
243 // /**
244 // * \brief
245 // * \details
246 // * \param bool distributeOnTwo
247 // * \param bool recropPixels
248 // * \param Paillier<T_in, T_out> paillier
249 // * \authors Katia Auxilien
250 // * \date 29 May 2024, 13:55:00
251 // */
252 // template <typename T_in, typename T_out>
253 // void decrypt2(bool distributeOnTwo, Paillier<T_in,T_out> paillier);
254};
255
256/************** 8bits **************/
257
258template <typename T_in, typename T_out>
259void PaillierControllerPGM::encrypt(bool distributeOnTwo, bool recropPixels, Paillier<T_in, T_out> paillier)
260{
261 string s_file = getCFile();
262
263 char cNomImgLue[250];
264 strcpy(cNomImgLue, s_file.c_str());
265
266 string toErase = ".pgm";
267 size_t pos = s_file.find(".pgm");
268 s_file.erase(pos, toErase.length());
269 string s_fileNew = s_file + "_E.pgm";
270 char cNomImgEcriteEnc[250];
271 strcpy(cNomImgEcriteEnc, s_fileNew.c_str());
272
273 int nH, nW, nTaille;
274 uint64_t n = model->getInstance()->getPublicKey().getN();
275 uint64_t g = model->getInstance()->getPublicKey().getG();
276
277 OCTET *ImgIn;
278 image_pgm::lire_nb_lignes_colonnes_image_p(cNomImgLue, &nH, &nW);
279
280 if (distributeOnTwo)
281 {
282 uint8_t *ImgOutEnc;
283 // T_in *ImgOutEnc;
284 nTaille = nH * nW;
285
286 allocation_tableau(ImgIn, OCTET, nTaille);
287 image_pgm::lire_image_p(cNomImgLue, ImgIn, nTaille);
288 allocation_tableau(ImgOutEnc, OCTET, nH * (2 * nW));
289 uint64_t x = 0, y = 1;
290 for (int i = 0; i < nTaille; i++)
291 {
292 uint8_t pixel = histogramExpansion(ImgIn[i], recropPixels);
293
294 uint16_t pixel_enc = paillier.paillierEncryption(n, g, pixel);
295 uint8_t pixel_enc_dec_x = pixel_enc / n;
296 uint8_t pixel_enc_dec_y = pixel_enc % n;
297 ImgOutEnc[x] = pixel_enc_dec_x;
298 ImgOutEnc[y] = pixel_enc_dec_y;
299 x = x + 2;
300 y = y + 2;
301 }
302
303 image_pgm::ecrire_image_pgm_variable_size(cNomImgEcriteEnc, ImgOutEnc, nH, nW * 2, n);
304
305 free(ImgIn);
306 free(ImgOutEnc);
307 }
308 else
309 {
310 uint16_t *ImgOutEnc;
311 nTaille = nH * nW;
312
313 allocation_tableau(ImgIn, OCTET, nTaille);
314 image_pgm::lire_image_p(cNomImgLue, ImgIn, nTaille);
315 allocation_tableau(ImgOutEnc, uint16_t, nTaille);
316
317 for (int i = 0; i < nTaille; i++)
318 {
319 uint8_t pixel = histogramExpansion(ImgIn[i], recropPixels);
320 uint16_t pixel_enc = paillier.paillierEncryption(n, g, pixel);
321 ImgOutEnc[i] = pixel_enc;
322 }
323
324 image_pgm::ecrire_image_pgm_variable_size(cNomImgEcriteEnc, ImgOutEnc, nH, nW, n * n);
325
326 free(ImgIn);
327 free(ImgOutEnc);
328 nTaille = nH * nW;
329 }
330}
331
332template <typename T_in, typename T_out>
333void PaillierControllerPGM::decrypt(bool distributeOnTwo, Paillier<T_in, T_out> paillier)
334{
335 string s_file = getCFile();
336 char cNomImgLue[250];
337 strcpy(cNomImgLue, s_file.c_str());
338
339 string toErase = ".pgm";
340 size_t pos = s_file.find(".pgm");
341 s_file.erase(pos, toErase.length());
342 string s_fileNew = s_file + "_D.pgm";
343 char cNomImgEcriteDec[250];
344 strcpy(cNomImgEcriteDec, s_fileNew.c_str());
345
346 int nH, nW, nTaille;
347 uint64_t n, lambda, mu;
348 lambda = model->getInstance()->getPrivateKey().getLambda();
351
352 OCTET *ImgOutDec;
353 image_pgm::lire_nb_lignes_colonnes_image_p(cNomImgLue, &nH, &nW);
354 nTaille = nH * nW;
355
356 if (distributeOnTwo)
357 {
358 uint8_t *ImgIn;
359
360 allocation_tableau(ImgIn, uint8_t, nTaille);
361 image_pgm::lire_image_pgm_and_get_maxgrey(cNomImgLue, ImgIn, nTaille); // TODO : Retirer and_get_maxgrey
362 allocation_tableau(ImgOutDec, OCTET, nH * (nW / 2));
363
364 int x = 0, y = 1;
365 for (int i = 0; i < nH * (nW / 2); i++)
366 {
367 uint16_t pixel;
368 uint8_t pixel_enc_dec_x = ImgIn[x];
369 uint8_t pixel_enc_dec_y = ImgIn[y];
370 pixel = (pixel_enc_dec_x * n) + pixel_enc_dec_y;
371 x = x + 2;
372 y = y + 2;
373 uint8_t c = paillier.paillierDecryption(n, lambda, mu, pixel);
374 ImgOutDec[i] = static_cast<OCTET>(c);
375 }
376 image_pgm::ecrire_image_p(cNomImgEcriteDec, ImgOutDec, nH, nW / 2);
377 free(ImgIn);
378 free(ImgOutDec);
379 }
380 else
381 {
382 uint16_t *ImgIn;
383 allocation_tableau(ImgIn, uint16_t, nTaille);
384 image_pgm::lire_image_pgm_and_get_maxgrey(cNomImgLue, ImgIn, nTaille);
385 allocation_tableau(ImgOutDec, OCTET, nTaille);
386
387 for (int i = 0; i < nTaille; i++)
388 {
389 uint16_t pixel = ImgIn[i];
390 uint8_t c = paillier.paillierDecryption(n, lambda, mu, pixel);
391 ImgOutDec[i] = static_cast<OCTET>(c);
392 }
393 image_pgm::ecrire_image_p(cNomImgEcriteDec, ImgOutDec, nH, nW);
394 free(ImgIn);
395 free(ImgOutDec);
396 }
397}
398
399template <typename T_in, typename T_out>
401{
402 string s_file = getCFile();
403
404 char cNomImgLue[250];
405 strcpy(cNomImgLue, s_file.c_str());
406
407 string toErase = ".pgm";
408 size_t pos = s_file.find(".pgm");
409 s_file.erase(pos, toErase.length());
410 string s_fileNew = s_file + "_E.pgm";
411 char cNomImgEcriteEnc[250];
412 strcpy(cNomImgEcriteEnc, s_fileNew.c_str());
413
414 int nH, nW, nTaille; // TODO : Change nH nW to uint16_t and nTaille type to uint32_t
415 uint64_t n = model->getInstance()->getPublicKey().getN();
416 uint64_t g = model->getInstance()->getPublicKey().getG();
417
418 OCTET *ImgIn;
419 image_pgm::lire_nb_lignes_colonnes_image_p(cNomImgLue, &nH, &nW);
420
421 uint16_t *ImgOutEnc;
422
423 nTaille = nH * nW;
424
425 allocation_tableau(ImgIn, OCTET, nTaille);
426 image_pgm::lire_image_p(cNomImgLue, ImgIn, nTaille);
427 allocation_tableau(ImgOutEnc, uint16_t, nTaille);
428
429 for (int i = 0; i < nTaille; i++)
430 {
431 uint8_t pixel = histogramExpansion(ImgIn[i], recropPixels);
432 uint16_t pixel_enc = paillier.paillierEncryption(n, g, pixel);
433
434 while (pixel_enc % 32 != 0)
435 {
436 pixel_enc = paillier.paillierEncryption(n, g, pixel);
437 }
438
439 ImgOutEnc[i] = pixel_enc;
440 }
441
442 uint16_t *ImgOutEncComp = compressBits(ImgOutEnc, nH, nW);
443 int nbPixelsComp = ceil((double)(nH * nW * 11) / 16);
444 printf("Size : %d\n", nbPixelsComp);
445
446 pair<int, int> dimensionComp = decomposeDimension(nbPixelsComp);
447 int nHComp = dimensionComp.first;
448 int nWComp = dimensionComp.second;
449
450 image_pgm::write_image_pgm_compressed_variable_size(cNomImgEcriteEnc, ImgOutEncComp, nHComp, nWComp, n * n, nbPixelsComp, nH, nW);
451
452 free(ImgIn);
453 free(ImgOutEnc);
454 delete[] ImgOutEncComp;
455}
456
457template <typename T_in, typename T_out>
459{
460 string s_file = getCFile();
461 char cNomImgLue[250];
462 strcpy(cNomImgLue, s_file.c_str());
463
464 string toErase = ".pgm";
465 size_t pos = s_file.find(".pgm");
466 s_file.erase(pos, toErase.length());
467 string s_fileNew = s_file + "_D.pgm";
468 char cNomImgEcriteDec[250];
469 strcpy(cNomImgEcriteDec, s_fileNew.c_str());
470
471 int nH, nW, nTaille, nHComp, nWComp, nTailleComp;
472 uint64_t n, lambda, mu;
473 lambda = model->getInstance()->getPrivateKey().getLambda();
476
477 OCTET *ImgOutDec;
478 image_pgm::lire_nb_lignes_colonnes_image_p(cNomImgLue, &nHComp, &nWComp);
479 nTailleComp = nHComp * nWComp;
480 printf("Controller : %d %d\n", nHComp, nWComp);
481 uint16_t *ImgInComp;
482 allocation_tableau(ImgInComp, uint16_t, nTailleComp);
483 pair<int, int> dimesionOriginal = image_pgm::read_image_pgm_compressed_and_get_originalDimension(cNomImgLue, ImgInComp);
484
485 nH = dimesionOriginal.second;
486 nW = dimesionOriginal.first;
487 nTaille = nH * nW;
488
489 allocation_tableau(ImgOutDec, OCTET, nTaille);
490
491 uint16_t *ImgInEnc = decompressBits(ImgInComp, nH, nW, nTaille);
492
493 for (int i = 0; i < nTaille; i++)
494 {
495 uint16_t pixel = ImgInEnc[i];
496 uint8_t c = paillier.paillierDecryption(n, lambda, mu, pixel);
497 ImgOutDec[i] = static_cast<OCTET>(c);
498 }
499 image_pgm::ecrire_image_p(cNomImgEcriteDec, ImgOutDec, nH, nW);
500 free(ImgInComp);
501 free(ImgOutDec);
502}
503
504#endif // PAILLIERCONTROLLER_PGM
Superclass, of Paillier main, that contain common methods between subclasses.
Superclass of Paillier main that contains common methods between subclasses.
Definition PaillierController.hpp:31
PaillierModel * model
Instance of PaillierModel.
Definition PaillierController.hpp:35
Controller for the Paillier cryptosystem applied to PGM images.
Definition PaillierControllerPGM.hpp:38
void printHelp()
Print the man page message.
Definition PaillierControllerPGM.cpp:196
void encryptCompression(bool recropPixels, Paillier< T_in, T_out > paillier)
Encrypt an image using the Paillier cryptosystem with compression.
Definition PaillierControllerPGM.hpp:400
void decrypt(bool distributeOnTwo, Paillier< T_in, T_out > paillier)
Decrypt an image using the Paillier cryptosystem.
Definition PaillierControllerPGM.hpp:333
uint16_t * compressBits(uint16_t *ImgInEnc, int nb_lignes, int nb_colonnes)
This function compresses the encrypted bits of an image.
Definition PaillierControllerPGM.cpp:217
const char * getCFile() const
Getter for the c_file attribute.
Definition PaillierControllerPGM.cpp:34
void setCFile(char *newCFile)
Setter for the c_file attribute.
Definition PaillierControllerPGM.cpp:39
uint16_t * decompressBits(uint16_t *ImgInEnc, int nb_lignes, int nb_colonnes, int nTailleOriginale)
Method to decompress an encrypted 8-bit PGM image.
Definition PaillierControllerPGM.cpp:265
pair< int, int > decomposeDimension(int n)
Method to decompose the dimensions of a compressed image.
Definition PaillierControllerPGM.cpp:309
~PaillierControllerPGM()
Destructor.
Definition PaillierControllerPGM.cpp:24
void init()
Initializes the controller.
Definition PaillierControllerPGM.cpp:26
void checkParameters(char *arg_in[], int size_arg, bool param[])
Check the parameters passed to the program.
Definition PaillierControllerPGM.cpp:46
void encrypt(bool distributeOnTwo, bool recropPixels, Paillier< T_in, T_out > paillier)
Encrypt an image using the Paillier cryptosystem.
Definition PaillierControllerPGM.hpp:259
uint8_t histogramExpansion(OCTET ImgPixel, bool recropPixels)
Perform histogram expansion on an image pixel.
Definition PaillierControllerPGM.cpp:201
PaillierControllerPGM()
Default constructor.
Definition PaillierControllerPGM.cpp:20
void decryptCompression(Paillier< T_in, T_out > paillier)
Method to decrypt an 8-bit PGM image with compression.
Definition PaillierControllerPGM.hpp:458
This class implements the Paillier cryptosystem.
Definition Paillier.hpp:33
T_out paillierEncryption(uint64_t n, uint64_t g, T_in m)
Encrypt a message using Paillier cryptosystem.
Definition Paillier.hpp:407
T_in paillierDecryption(uint64_t n, uint64_t lambda, uint64_t mu, T_out c)
Decrypt a ciphertext using Paillier cryptosystem.
Definition Paillier.hpp:480
PaillierPublicKey getPublicKey() const
Getter function for the public key.
Definition Paillier_model.cpp:32
static PaillierModel * getInstance()
Static function to get the singleton instance of the PaillierModel class.
Definition Paillier_model.hpp:73
PaillierPrivateKey getPrivateKey() const
Getter function for the private key.
Definition Paillier_model.cpp:31
uint64_t getMu() const
Getter method for the mu value.
Definition Paillier_private_key.cpp:41
uint64_t getN() const
Getter method for the n value.
Definition Paillier_private_key.cpp:46
uint64_t getLambda() const
Getter method for the lambda value.
Definition Paillier_private_key.cpp:36
uint64_t getN() const
Getter method for the n value.
Definition Paillier_public_key.cpp:29
uint64_t getG() const
Getter method for the g value.
Definition Paillier_public_key.cpp:34
static void lire_image_p(char nom_image[], OCTET *pt_image, int taille_image)
Reads a PGM image with variable size and stores it in an OCTET array.
Definition image_pgm.cpp:69
static void lire_nb_lignes_colonnes_image_p(char nom_image[], int *nb_lignes, int *nb_colonnes)
Reads the number of lines and columns of a PGM image.
Definition image_pgm.cpp:45
static pair< int, int > read_image_pgm_compressed_and_get_originalDimension(char nom_image[], uint16_t *pt_image)
Reads a compressed PGM image with 16-bit depth and returns the original dimensions.
Definition image_pgm.cpp:233
static void ecrire_image_p(char nom_image[], OCTET *pt_image, int nb_lignes, int nb_colonnes)
Writes a PGM image from an OCTET array with given dimensions.
Definition image_pgm.cpp:21
static void ecrire_image_pgm_variable_size(char nom_image[], uint8_t *pt_image, int nb_lignes, int nb_colonnes, uint8_t max_value)
Writes a PGM image with variable size.
Definition image_pgm.cpp:124
static uint8_t lire_image_pgm_and_get_maxgrey(char nom_image[], uint8_t *pt_image, int taille_image)
Reads a PGM image with 8-bit depth and returns the maximum grey value.
Definition image_pgm.cpp:96
static void write_image_pgm_compressed_variable_size(char nom_image[], uint16_t *pt_image, int nb_lignes, int nb_colonnes, uint16_t max_value, int imgSize, int nHOriginal, int nWOriginal)
Writes a compressed PGM image with variable size and 16-bit depth.
Definition image_pgm.cpp:202
This file contains the declaration of the image_pgm class, which is used to read and write PGM images...
unsigned char OCTET
Definition image_pgm.hpp:21
This file defines the image_portable class, which is a base class for different image formats.
#define allocation_tableau(nom, type, nombre)
Allocate a dynamic array of a given type and size.
Definition image_portable.hpp:27