2 //g++ -O3 one_round_new.cpp pixmap_io.o -o one_round_new -std=c++11
15 /*#include <cryptopp/hex.h>
16 #include <cryptopp/sha.h>
17 #include <cryptopp/osrng.h>
18 #include <cryptopp/secblock.h>
23 int load_RGB_pixmap(char *filename, int *width, int *height, unsigned char**R_data, unsigned char**G_data, unsigned char**B_data);
24 void store_RGB_pixmap(char *filename, unsigned char *R_data, unsigned char *G_data, unsigned char *B_data, int width, int height);
28 //using namespace CryptoPP;
44 typedef unsigned char uchar;
49 struct timeval tstart;
50 gettimeofday(&tstart,0);
51 return( (double) (tstart.tv_sec + tstart.tv_usec*1e-6) );
54 double TimeStop(double t)
58 gettimeofday(&tend,0);
59 t = (double) (tend.tv_sec + tend.tv_usec*1e-6) - t;
66 uint xorshift32(const uint t)
68 /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
76 ulong xorshift64(ulong t)
78 /* Algorithm "xor" from p. 4 of Marsaglia, "Xorshift RNGs" */
88 __uint128_t g_lehmer64_state;
90 inline uint64_t splitmix64_stateless(uint64_t index) {
91 uint64_t z = (index + UINT64_C(0x9E3779B97F4A7C15));
92 z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9);
93 z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB);
98 inline void lehmer64_seed(uint64_t seed) {
99 g_lehmer64_state = (((__uint128_t)splitmix64_stateless(seed)) << 64) +
100 splitmix64_stateless(seed + 1);
103 inline uint64_t lehmer64() {
104 g_lehmer64_state *= UINT64_C(0xda942042e4dd58b5);
106 return g_lehmer64_state >> 64;
113 void inverse_tables(uchar *tab, int size_tab,uchar *inv_perm_tabs) {
115 for(int i=0;i<size_tab;i++) {
116 inv_perm_tabs[tab[i]] = i;
121 void inverse_tables_int(int *tab, int size_tab,int *inv_perm_tabs) {
123 for(int i=0;i<size_tab;i++) {
124 inv_perm_tabs[tab[i]] = i;
131 void rc4key(uchar *key, uchar *sc, int size_DK) {
133 for(int i=0;i<256;i++) {
139 for(int i0=0; i0<256; i0++) {
140 j0 = (j0 + sc[i0] + key[i0%size_DK] )&0xFF;
149 void rc4keyperm(uchar *key,int len, int rp,int *sc, int size_DK) {
155 for (int i=0;i<len;i++) {
158 for (int it = 0; it < rp; it++) {
160 for(int i0 = 0; i0<len; i0++) {
161 j0 = (j0 + sc[i0] + sc[j0] + key[i0%size_DK] )% len;
170 void prga(uchar *sc, int ldata, uchar *r) {
174 for (int it=0; it<ldata; it++) {
176 j0 = (j0 + sc[i0])&0xFF;
180 r[it]=sc[(sc[i0]+sc[j0])&0xFF];
190 void encrypt_ecb(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, ulong myrand, int debug) {
199 ulong *rm1=(ulong*)RM1;
200 ulong *rm2=(ulong*)RM2;
202 for(int it=0;it<len/2;it++) {
204 int ind2=Pbox[it+len/2]*h;
207 for(int a=0;a<(h>>3);a++) {
216 for(int a=0;a<h;a+=4) {
218 X[a+1]=seq_in[ind2+a+1];
219 X[a+2]=seq_in[ind2+a+2];
220 X[a+3]=seq_in[ind2+a+3];
223 for(int a=0;a<h;a+=4) {
225 Y[a+1]=seq_in[ind1+a+1];
226 Y[a+2]=seq_in[ind1+a+2];
227 Y[a+3]=seq_in[ind1+a+3];
231 for(int a=0;a<h;a+=4) {
232 tmp[a]=Sbox1[X[a]^RM1[a]];
233 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]];
234 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]];
235 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]];
238 for(int a=0;a<h;a+=4) {
239 fX[a]=Sbox2[tmp[a]^Y[a]];
240 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
241 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
242 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
246 /*for(int a=0;a<h;a+=4) {
247 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]]^Y[a]];
248 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]]^Y[a+1]];
249 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]]^Y[a+2]];
250 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]]^Y[a+3]];
256 for(int a=0;a<h;a+=4) {
257 tmp[a]=Sbox2[fX[a]^Y[a]];
258 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]];
259 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]];
260 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]];
263 for(int a=0;a<h;a+=4) {
264 gY[a]=Sbox1[tmp[a]^RM2[a]];
265 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
266 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
267 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
272 /* for(int a=0;a<h;a+=4) {
273 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]]^RM2[a]];
274 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]]^RM2[a+1]];
275 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]]^RM2[a+2]];
276 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]]^RM2[a+3]];
285 for(int a=0;a<h;a+=4) {
286 seq_out[ind2+a]=gY[a];
287 seq_out[ind2+a+1]=gY[a+1];
288 seq_out[ind2+a+2]=gY[a+2];
289 seq_out[ind2+a+3]=gY[a+3];
292 for(int a=0;a<h;a+=4) {
293 seq_out[ind1+a]=fX[a];
294 seq_out[ind1+a+1]=fX[a+1];
295 seq_out[ind1+a+2]=fX[a+2];
296 seq_out[ind1+a+3]=fX[a+3];
316 void decrypt_ecb(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, ulong myrand, int debug) {
325 ulong *rm1=(ulong*)RM1;
326 ulong *rm2=(ulong*)RM2;
328 for(int it=0;it<len/2;it++) {
330 int ind2=Pbox[it+len/2]*h;
333 for(int a=0;a<(h>>3);a++) {
342 for(int a=0;a<h;a+=4) {
343 gY[a]=seq_in[ind2+a];
344 gY[a+1]=seq_in[ind2+a+1];
345 gY[a+2]=seq_in[ind2+a+2];
346 gY[a+3]=seq_in[ind2+a+3];
349 for(int a=0;a<h;a+=4) {
350 fX[a]=seq_in[ind1+a];
351 fX[a+1]=seq_in[ind1+a+1];
352 fX[a+2]=seq_in[ind1+a+2];
353 fX[a+3]=seq_in[ind1+a+3];
359 for(int a=0;a<h;a+=4) {
360 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
361 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
362 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
363 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
367 for(int a=0;a<h;a+=4) {
368 invgY[a]=Inv_Sbox2[tmp[a]]^fX[a];
369 invgY[a+1]=Inv_Sbox2[tmp[a+1]]^fX[a+1];
370 invgY[a+2]=Inv_Sbox2[tmp[a+2]]^fX[a+2];
371 invgY[a+3]=Inv_Sbox2[tmp[a+3]]^fX[a+3];
376 /* for(int a=0;a<h;a+=4) {
377 invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a];
378 invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1];
379 invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2];
380 invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3];
386 for(int a=0;a<h;a+=4) {
387 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
388 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
389 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
390 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
394 for(int a=0;a<h;a+=4) {
395 invfX[a]=Inv_Sbox1[tmp[a]]^RM1[a];
396 invfX[a+1]=Inv_Sbox1[tmp[a+1]]^RM1[a+1];
397 invfX[a+2]=Inv_Sbox1[tmp[a+2]]^RM1[a+2];
398 invfX[a+3]=Inv_Sbox1[tmp[a+3]]^RM1[a+3];
404 for(int a=0;a<h;a+=4) {
405 invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a];
406 invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1];
407 invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2];
408 invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3];
413 for(int a=0;a<h;a+=4) {
414 seq_out[ind2+a]=invfX[a];
415 seq_out[ind2+a+1]=invfX[a+1];
416 seq_out[ind2+a+2]=invfX[a+2];
417 seq_out[ind2+a+3]=invfX[a+3];
420 for(int a=0;a<h;a+=4) {
421 seq_out[ind1+a]=invgY[a];
422 seq_out[ind1+a+1]=invgY[a+1];
423 seq_out[ind1+a+2]=invgY[a+2];
424 seq_out[ind1+a+3]=invgY[a+3];
441 void encrypt_ecb(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uint myrand, int debug) {
450 for(int it=0;it<len/2;it++) {
452 int ind2=Pbox[it+len/2]*h;
456 RM1=&RM[PboxSRM[it]*h];
457 RM2=&RM[h*h+PboxSRM[it]*h];
460 for(int a=0;a<h;a+=4) {
462 X[a+1]=seq_in[ind2+a+1];
463 X[a+2]=seq_in[ind2+a+2];
464 X[a+3]=seq_in[ind2+a+3];
467 for(int a=0;a<h;a+=4) {
469 Y[a+1]=seq_in[ind1+a+1];
470 Y[a+2]=seq_in[ind1+a+2];
471 Y[a+3]=seq_in[ind1+a+3];
475 for(int a=0;a<h;a+=4) {
476 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]]^Y[a]];
477 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]]^Y[a+1]];
478 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]]^Y[a+2]];
479 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]]^Y[a+3]];
482 for(int a=0;a<h;a+=4) {
483 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]]^RM2[a]];
484 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]]^RM2[a+1]];
485 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]]^RM2[a+2]];
486 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]]^RM2[a+3]];
490 for(int a=0;a<h;a+=4) {
491 seq_out[ind2+a]=gY[a];
492 seq_out[ind2+a+1]=gY[a+1];
493 seq_out[ind2+a+2]=gY[a+2];
494 seq_out[ind2+a+3]=gY[a+3];
497 for(int a=0;a<h;a+=4) {
498 seq_out[ind1+a]=fX[a];
499 seq_out[ind1+a+1]=fX[a+1];
500 seq_out[ind1+a+2]=fX[a+2];
501 seq_out[ind1+a+3]=fX[a+3];
521 void decrypt_ecb(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uint myrand, int debug) {
530 for(int it=0;it<len/2;it++) {
532 int ind2=Pbox[it+len/2]*h;
535 RM1=&RM[PboxSRM[it]*h];
536 RM2=&RM[h*h+PboxSRM[it]*h];
539 for(int a=0;a<h;a+=4) {
540 gY[a]=seq_in[ind2+a];
541 gY[a+1]=seq_in[ind2+a+1];
542 gY[a+2]=seq_in[ind2+a+2];
543 gY[a+3]=seq_in[ind2+a+3];
546 for(int a=0;a<h;a+=4) {
547 fX[a]=seq_in[ind1+a];
548 fX[a+1]=seq_in[ind1+a+1];
549 fX[a+2]=seq_in[ind1+a+2];
550 fX[a+3]=seq_in[ind1+a+3];
553 for(int a=0;a<h;a+=4) {
554 invgY[a]=Inv_Sbox2[Inv_Sbox1[gY[a]]^RM2[a]]^fX[a];
555 invgY[a+1]=Inv_Sbox2[Inv_Sbox1[gY[a+1]]^RM2[a+1]]^fX[a+1];
556 invgY[a+2]=Inv_Sbox2[Inv_Sbox1[gY[a+2]]^RM2[a+2]]^fX[a+2];
557 invgY[a+3]=Inv_Sbox2[Inv_Sbox1[gY[a+3]]^RM2[a+3]]^fX[a+3];
562 for(int a=0;a<h;a+=4) {
563 invfX[a]=Inv_Sbox1[Inv_Sbox2[fX[a]]^invgY[a]]^RM1[a];
564 invfX[a+1]=Inv_Sbox1[Inv_Sbox2[fX[a+1]]^invgY[a+1]]^RM1[a+1];
565 invfX[a+2]=Inv_Sbox1[Inv_Sbox2[fX[a+2]]^invgY[a+2]]^RM1[a+2];
566 invfX[a+3]=Inv_Sbox1[Inv_Sbox2[fX[a+3]]^invgY[a+3]]^RM1[a+3];
571 for(int a=0;a<h;a+=4) {
572 seq_out[ind2+a]=invfX[a];
573 seq_out[ind2+a+1]=invfX[a+1];
574 seq_out[ind2+a+2]=invfX[a+2];
575 seq_out[ind2+a+3]=invfX[a+3];
578 for(int a=0;a<h;a+=4) {
579 seq_out[ind1+a]=invgY[a];
580 seq_out[ind1+a+1]=invgY[a+1];
581 seq_out[ind1+a+2]=invgY[a+2];
582 seq_out[ind1+a+3]=invgY[a+3];
601 void encrypt_cbc_prng(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uint myrand, int debug) {
612 ulong *rm1=(ulong*)RM1;
613 ulong *rm2=(ulong*)RM2;
615 for(int it=0;it<len/2;it++) {
617 int ind2=Pbox[it+len/2]*h;
620 for(int a=0;a<(h>>3);a++) {
629 for(int a=0;a<h;a+=4) {
631 X[a+1]=seq_in[ind2+a+1];
632 X[a+2]=seq_in[ind2+a+2];
633 X[a+3]=seq_in[ind2+a+3];
636 for(int a=0;a<h;a+=4) {
638 Y[a+1]=seq_in[ind1+a+1];
639 Y[a+2]=seq_in[ind1+a+2];
640 Y[a+3]=seq_in[ind1+a+3];
644 for(int a=0;a<h;a+=4) {
645 tmp[a]=X[a]^RM1[a]^IV1[a];
646 tmp[a+1]=X[a+1]^RM1[a+1]^IV1[a+1];
647 tmp[a+2]=X[a+2]^RM1[a+2]^IV1[a+2];
648 tmp[a+3]=X[a+3]^RM1[a+3]^IV1[a+3];
651 for(int a=0;a<h;a+=4) {
652 tmp[a]=Sbox1[tmp[a]];
653 tmp[a+1]=Sbox1[tmp[a+1]];
654 tmp[a+2]=Sbox1[tmp[a+2]];
655 tmp[a+3]=Sbox1[tmp[a+3]];
659 /*for(int a=0;a<h;a+=4) {
660 tmp[a]=Sbox1[X[a]^RM1[a]^IV1[a]];
661 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]];
662 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]];
663 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]];
666 for(int a=0;a<h;a+=4) {
667 fX[a]=Sbox2[tmp[a]^Y[a]];
668 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
669 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
670 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
674 for(int a=0;a<h;a+=4) {
675 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
676 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
677 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
678 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
682 for(int a=0;a<h;a+=4) {
683 tmp[a]=fX[a]^Y[a]^IV2[a];
684 tmp[a+1]=fX[a+1]^Y[a+1]^IV2[a+1];
685 tmp[a+2]=fX[a+2]^Y[a+2]^IV2[a+2];
686 tmp[a+3]=fX[a+3]^Y[a+3]^IV2[a+3];
690 for(int a=0;a<h;a+=4) {
691 tmp[a]=Sbox2[tmp[a]];
692 tmp[a+1]=Sbox2[tmp[a+1]];
693 tmp[a+2]=Sbox2[tmp[a+2]];
694 tmp[a+3]=Sbox2[tmp[a+3]];
699 for(int a=0;a<h;a+=4) {
700 tmp[a]=Sbox2[fX[a]^Y[a]^IV2[a]];
701 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]];
702 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]];
703 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]];
708 for(int a=0;a<h;a+=4) {
709 gY[a]=Sbox1[tmp[a]^RM2[a]];
710 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
711 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
712 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
719 for(int a=0;a<h;a+=4) {
720 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
721 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
722 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
723 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
728 for(int a=0;a<h;a+=4) {
729 seq_out[ind2+a]=gY[a];
730 seq_out[ind2+a+1]=gY[a+1];
731 seq_out[ind2+a+2]=gY[a+2];
732 seq_out[ind2+a+3]=gY[a+3];
735 for(int a=0;a<h;a+=4) {
736 seq_out[ind1+a]=fX[a];
737 seq_out[ind1+a+1]=fX[a+1];
738 seq_out[ind1+a+2]=fX[a+2];
739 seq_out[ind1+a+3]=fX[a+3];
741 for(int a=0;a<h;a+=4) {
748 for(int a=0;a<h;a+=4) {
770 void decrypt_cbc_prng(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uint myrand, int debug) {
781 ulong *rm1=(ulong*)RM1;
782 ulong *rm2=(ulong*)RM2;
784 for(int it=0;it<len/2;it++) {
786 int ind2=Pbox[it+len/2]*h;
789 for(int a=0;a<(h>>3);a++) {
796 for(int a=0;a<h;a+=4) {
797 gY[a]=seq_in[ind2+a];
798 gY[a+1]=seq_in[ind2+a+1];
799 gY[a+2]=seq_in[ind2+a+2];
800 gY[a+3]=seq_in[ind2+a+3];
803 for(int a=0;a<h;a+=4) {
804 fX[a]=seq_in[ind1+a];
805 fX[a+1]=seq_in[ind1+a+1];
806 fX[a+2]=seq_in[ind1+a+2];
807 fX[a+3]=seq_in[ind1+a+3];
811 for(int a=0;a<h;a+=4) {
812 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
813 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
814 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
815 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
819 for(int a=0;a<h;a+=4) {
820 tmp[a]=Inv_Sbox2[tmp[a]];
821 tmp[a+1]=Inv_Sbox2[tmp[a+1]];
822 tmp[a+2]=Inv_Sbox2[tmp[a+2]];
823 tmp[a+3]=Inv_Sbox2[tmp[a+3]];
828 for(int a=0;a<h;a+=4) {
829 invgY[a]=tmp[a]^fX[a]^IV2[a];
830 invgY[a+1]=tmp[a+1]^fX[a+1]^IV2[a+1];
831 invgY[a+2]=tmp[a+2]^fX[a+2]^IV2[a+2];
832 invgY[a+3]=tmp[a+3]^fX[a+3]^IV2[a+3];
836 for(int a=0;a<h;a+=4) {
837 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
838 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
839 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
840 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
845 for(int a=0;a<h;a+=4) {
846 tmp[a]=Inv_Sbox1[tmp[a]];
847 tmp[a+1]=Inv_Sbox1[tmp[a+1]];
848 tmp[a+2]=Inv_Sbox1[tmp[a+2]];
849 tmp[a+3]=Inv_Sbox1[tmp[a+3]];
856 for(int a=0;a<h;a+=4) {
857 invfX[a]=tmp[a]^RM1[a]^IV1[a];
858 invfX[a+1]=tmp[a+1]^RM1[a+1]^IV1[a+1];
859 invfX[a+2]=tmp[a+2]^RM1[a+2]^IV1[a+2];
860 invfX[a+3]=tmp[a+3]^RM1[a+3]^IV1[a+3];
865 for(int a=0;a<h;a+=4) {
866 seq_out[ind2+a]=invfX[a];
867 seq_out[ind2+a+1]=invfX[a+1];
868 seq_out[ind2+a+2]=invfX[a+2];
869 seq_out[ind2+a+3]=invfX[a+3];
872 for(int a=0;a<h;a+=4) {
873 seq_out[ind1+a]=invgY[a];
874 seq_out[ind1+a+1]=invgY[a+1];
875 seq_out[ind1+a+2]=invgY[a+2];
876 seq_out[ind1+a+3]=invgY[a+3];
878 for(int a=0;a<h;a+=4) {
885 for(int a=0;a<h;a+=4) {
908 void encrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uint myrand, int debug) {
921 int gg=(Sbox1[12]&(h-1))*(Sbox2[32]&(h-1));
923 for(int a=0;a<h;a+=4) {
929 gg=gg=(Sbox1[102]&(h-1))*(Sbox2[72]&(h-1));
932 for(int a=0;a<h;a+=4) {
940 for(int it=0;it<len/2;it++) {
942 int ind2=Pbox[it+len/2]*h;
943 RM1=&RM[PboxSRM[it]*h+Sbox1[it&255]];
944 RM2=&RM[PboxSRM[len/2-it]*h+Sbox2[it&255]];
946 for(int a=0;a<h;a+=4) {
948 X[a+1]=seq_in[ind2+a+1];
949 X[a+2]=seq_in[ind2+a+2];
950 X[a+3]=seq_in[ind2+a+3];
953 for(int a=0;a<h;a+=4) {
955 Y[a+1]=seq_in[ind1+a+1];
956 Y[a+2]=seq_in[ind1+a+2];
957 Y[a+3]=seq_in[ind1+a+3];
961 for(int a=0;a<h;a+=4) {
962 tmp[a]=X[a]^RM1[a]^IV1[a];
963 tmp[a+1]=X[a+1]^RM1[a+1]^IV1[a+1];
964 tmp[a+2]=X[a+2]^RM1[a+2]^IV1[a+2];
965 tmp[a+3]=X[a+3]^RM1[a+3]^IV1[a+3];
968 for(int a=0;a<h;a+=4) {
969 tmp[a]=Sbox1[tmp[a]];
970 tmp[a+1]=Sbox1[tmp[a+1]];
971 tmp[a+2]=Sbox1[tmp[a+2]];
972 tmp[a+3]=Sbox1[tmp[a+3]];
976 /*for(int a=0;a<h;a+=4) {
977 tmp[a]=Sbox1[X[a]^RM1[a]^IV1[a]];
978 tmp[a+1]=Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]];
979 tmp[a+2]=Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]];
980 tmp[a+3]=Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]];
983 for(int a=0;a<h;a+=4) {
984 fX[a]=Sbox2[tmp[a]^Y[a]];
985 fX[a+1]=Sbox2[tmp[a+1]^Y[a+1]];
986 fX[a+2]=Sbox2[tmp[a+2]^Y[a+2]];
987 fX[a+3]=Sbox2[tmp[a+3]^Y[a+3]];
991 for(int a=0;a<h;a+=4) {
992 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
993 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
994 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
995 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
999 for(int a=0;a<h;a+=4) {
1000 tmp[a]=fX[a]^Y[a]^IV2[a];
1001 tmp[a+1]=fX[a+1]^Y[a+1]^IV2[a+1];
1002 tmp[a+2]=fX[a+2]^Y[a+2]^IV2[a+2];
1003 tmp[a+3]=fX[a+3]^Y[a+3]^IV2[a+3];
1007 for(int a=0;a<h;a+=4) {
1008 tmp[a]=Sbox2[tmp[a]];
1009 tmp[a+1]=Sbox2[tmp[a+1]];
1010 tmp[a+2]=Sbox2[tmp[a+2]];
1011 tmp[a+3]=Sbox2[tmp[a+3]];
1016 for(int a=0;a<h;a+=4) {
1017 tmp[a]=Sbox2[fX[a]^Y[a]^IV2[a]];
1018 tmp[a+1]=Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]];
1019 tmp[a+2]=Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]];
1020 tmp[a+3]=Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]];
1025 for(int a=0;a<h;a+=4) {
1026 gY[a]=Sbox1[tmp[a]^RM2[a]];
1027 gY[a+1]=Sbox1[tmp[a+1]^RM2[a+1]];
1028 gY[a+2]=Sbox1[tmp[a+2]^RM2[a+2]];
1029 gY[a+3]=Sbox1[tmp[a+3]^RM2[a+3]];
1036 for(int a=0;a<h;a+=4) {
1037 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
1038 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
1039 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
1040 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
1045 for(int a=0;a<h;a+=4) {
1046 seq_out[ind2+a]=gY[a];
1047 seq_out[ind2+a+1]=gY[a+1];
1048 seq_out[ind2+a+2]=gY[a+2];
1049 seq_out[ind2+a+3]=gY[a+3];
1052 for(int a=0;a<h;a+=4) {
1053 seq_out[ind1+a]=fX[a];
1054 seq_out[ind1+a+1]=fX[a+1];
1055 seq_out[ind1+a+2]=fX[a+2];
1056 seq_out[ind1+a+3]=fX[a+3];
1058 for(int a=0;a<h;a+=4) {
1065 for(int a=0;a<h;a+=4) {
1087 void decrypt_cbc_rm(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uint myrand, int debug) {
1099 int gg=(Sbox1[12]&(h-1))*(Sbox2[32]&(h-1));
1101 for(int a=0;a<h;a+=4) {
1103 IV1[a+1]=RM[gg+a+1];
1104 IV1[a+2]=RM[gg+a+2];
1105 IV1[a+3]=RM[gg+a+3];
1107 gg=gg=(Sbox1[102]&(h-1))*(Sbox2[72]&(h-1));
1110 for(int a=0;a<h;a+=4) {
1112 IV2[a+1]=RM[gg+a+1];
1113 IV2[a+2]=RM[gg+a+2];
1114 IV2[a+3]=RM[gg+a+3];
1119 for(int it=0;it<len/2;it++) {
1120 int ind1=Pbox[it]*h;
1121 int ind2=Pbox[it+len/2]*h;
1124 RM1=&RM[PboxSRM[it]*h+Sbox1[it&255]];
1125 RM2=&RM[PboxSRM[len/2-it]*h+Sbox2[it&255]];
1130 for(int a=0;a<h;a+=4) {
1131 gY[a]=seq_in[ind2+a];
1132 gY[a+1]=seq_in[ind2+a+1];
1133 gY[a+2]=seq_in[ind2+a+2];
1134 gY[a+3]=seq_in[ind2+a+3];
1137 for(int a=0;a<h;a+=4) {
1138 fX[a]=seq_in[ind1+a];
1139 fX[a+1]=seq_in[ind1+a+1];
1140 fX[a+2]=seq_in[ind1+a+2];
1141 fX[a+3]=seq_in[ind1+a+3];
1145 for(int a=0;a<h;a+=4) {
1146 tmp[a]=Inv_Sbox1[gY[a]]^RM2[a];
1147 tmp[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1148 tmp[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1149 tmp[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1153 for(int a=0;a<h;a+=4) {
1154 tmp[a]=Inv_Sbox2[tmp[a]];
1155 tmp[a+1]=Inv_Sbox2[tmp[a+1]];
1156 tmp[a+2]=Inv_Sbox2[tmp[a+2]];
1157 tmp[a+3]=Inv_Sbox2[tmp[a+3]];
1162 for(int a=0;a<h;a+=4) {
1163 invgY[a]=tmp[a]^fX[a]^IV2[a];
1164 invgY[a+1]=tmp[a+1]^fX[a+1]^IV2[a+1];
1165 invgY[a+2]=tmp[a+2]^fX[a+2]^IV2[a+2];
1166 invgY[a+3]=tmp[a+3]^fX[a+3]^IV2[a+3];
1170 for(int a=0;a<h;a+=4) {
1171 tmp[a]=Inv_Sbox2[fX[a]]^invgY[a];
1172 tmp[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1173 tmp[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1174 tmp[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1179 for(int a=0;a<h;a+=4) {
1180 tmp[a]=Inv_Sbox1[tmp[a]];
1181 tmp[a+1]=Inv_Sbox1[tmp[a+1]];
1182 tmp[a+2]=Inv_Sbox1[tmp[a+2]];
1183 tmp[a+3]=Inv_Sbox1[tmp[a+3]];
1190 for(int a=0;a<h;a+=4) {
1191 invfX[a]=tmp[a]^RM1[a]^IV1[a];
1192 invfX[a+1]=tmp[a+1]^RM1[a+1]^IV1[a+1];
1193 invfX[a+2]=tmp[a+2]^RM1[a+2]^IV1[a+2];
1194 invfX[a+3]=tmp[a+3]^RM1[a+3]^IV1[a+3];
1199 for(int a=0;a<h;a+=4) {
1200 seq_out[ind2+a]=invfX[a];
1201 seq_out[ind2+a+1]=invfX[a+1];
1202 seq_out[ind2+a+2]=invfX[a+2];
1203 seq_out[ind2+a+3]=invfX[a+3];
1206 for(int a=0;a<h;a+=4) {
1207 seq_out[ind1+a]=invgY[a];
1208 seq_out[ind1+a+1]=invgY[a+1];
1209 seq_out[ind1+a+2]=invgY[a+2];
1210 seq_out[ind1+a+3]=invgY[a+3];
1212 for(int a=0;a<h;a+=4) {
1219 for(int a=0;a<h;a+=4) {
1241 void encrypt_cbc(uchar* seq_in, uchar *seq_out, int len,uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uint myrand, int debug) {
1257 for(int a=0;a<h;a+=4) {
1258 myrand=xorshift32(myrand);
1269 for(int a=0;a<h;a+=4) {
1270 myrand=xorshift32(myrand);
1284 for(int it=0;it<len/2;it++) {
1285 int ind1=Pbox[it]*h;
1286 int ind2=Pbox[it+len/2]*h;
1290 RM1=&RM[PboxSRM[it]*h];
1291 RM2=&RM[h*h+PboxSRM[it]*h];
1294 for(int a=0;a<h;a+=4) {
1295 X[a]=seq_in[ind2+a];
1296 X[a+1]=seq_in[ind2+a+1];
1297 X[a+2]=seq_in[ind2+a+2];
1298 X[a+3]=seq_in[ind2+a+3];
1301 for(int a=0;a<h;a+=4) {
1302 Y[a]=seq_in[ind1+a];
1303 Y[a+1]=seq_in[ind1+a+1];
1304 Y[a+2]=seq_in[ind1+a+2];
1305 Y[a+3]=seq_in[ind1+a+3];
1309 for(int a=0;a<h;a+=4) {
1310 fX[a]=Sbox2[Sbox1[X[a]^RM1[a]^IV1[a]]^Y[a]];
1311 fX[a+1]=Sbox2[Sbox1[X[a+1]^RM1[a+1]^IV1[a+1]]^Y[a+1]];
1312 fX[a+2]=Sbox2[Sbox1[X[a+2]^RM1[a+2]^IV1[a+2]]^Y[a+2]];
1313 fX[a+3]=Sbox2[Sbox1[X[a+3]^RM1[a+3]^IV1[a+3]]^Y[a+3]];
1316 for(int a=0;a<h;a+=4) {
1317 gY[a]=Sbox1[Sbox2[fX[a]^Y[a]^IV2[a]]^RM2[a]];
1318 gY[a+1]=Sbox1[Sbox2[fX[a+1]^Y[a+1]^IV2[a+1]]^RM2[a+1]];
1319 gY[a+2]=Sbox1[Sbox2[fX[a+2]^Y[a+2]^IV2[a+2]]^RM2[a+2]];
1320 gY[a+3]=Sbox1[Sbox2[fX[a+3]^Y[a+3]^IV2[a+3]]^RM2[a+3]];
1324 for(int a=0;a<h;a+=4) {
1325 seq_out[ind2+a]=gY[a];
1326 seq_out[ind2+a+1]=gY[a+1];
1327 seq_out[ind2+a+2]=gY[a+2];
1328 seq_out[ind2+a+3]=gY[a+3];
1331 for(int a=0;a<h;a+=4) {
1332 seq_out[ind1+a]=fX[a];
1333 seq_out[ind1+a+1]=fX[a+1];
1334 seq_out[ind1+a+2]=fX[a+2];
1335 seq_out[ind1+a+3]=fX[a+3];
1337 for(int a=0;a<h;a+=4) {
1344 for(int a=0;a<h;a+=4) {
1366 void decrypt_cbc(uchar* seq_in, uchar *seq_out, int len, uchar* RM, int *Pbox, int *PboxSRM, uchar *Sbox1, uchar *Sbox2, uchar *Inv_Sbox1, uchar *Inv_Sbox2, uint myrand, int debug) {
1377 for(int a=0;a<h;a+=4) {
1378 myrand=xorshift32(myrand);
1389 for(int a=0;a<h;a+=4) {
1390 myrand=xorshift32(myrand);
1407 for(int it=0;it<len/2;it++) {
1408 int ind1=Pbox[it]*h;
1409 int ind2=Pbox[it+len/2]*h;
1412 RM1=&RM[PboxSRM[it]*h];
1413 RM2=&RM[h*h+PboxSRM[it]*h];
1416 for(int a=0;a<h;a+=4) {
1417 gY[a]=seq_in[ind2+a];
1418 gY[a+1]=seq_in[ind2+a+1];
1419 gY[a+2]=seq_in[ind2+a+2];
1420 gY[a+3]=seq_in[ind2+a+3];
1423 for(int a=0;a<h;a+=4) {
1424 fX[a]=seq_in[ind1+a];
1425 fX[a+1]=seq_in[ind1+a+1];
1426 fX[a+2]=seq_in[ind1+a+2];
1427 fX[a+3]=seq_in[ind1+a+3];
1431 for(int a=0;a<h;a+=4) {
1432 invgY[a]=Inv_Sbox1[gY[a]]^RM2[a];
1433 invgY[a+1]=Inv_Sbox1[gY[a+1]]^RM2[a+1];
1434 invgY[a+2]=Inv_Sbox1[gY[a+2]]^RM2[a+2];
1435 invgY[a+3]=Inv_Sbox1[gY[a+3]]^RM2[a+3];
1439 for(int a=0;a<h;a+=4) {
1440 invgY[a]=Inv_Sbox2[invgY[a]]^fX[a]^IV2[a];
1441 invgY[a+1]=Inv_Sbox2[invgY[a+1]]^fX[a+1]^IV2[a+1];
1442 invgY[a+2]=Inv_Sbox2[invgY[a+2]]^fX[a+2]^IV2[a+2];
1443 invgY[a+3]=Inv_Sbox2[invgY[a+3]]^fX[a+3]^IV2[a+3];
1447 for(int a=0;a<h;a+=4) {
1448 invfX[a]=Inv_Sbox2[fX[a]]^invgY[a];
1449 invfX[a+1]=Inv_Sbox2[fX[a+1]]^invgY[a+1];
1450 invfX[a+2]=Inv_Sbox2[fX[a+2]]^invgY[a+2];
1451 invfX[a+3]=Inv_Sbox2[fX[a+3]]^invgY[a+3];
1455 for(int a=0;a<h;a+=4) {
1456 invfX[a]=Inv_Sbox1[invfX[a]]^RM1[a]^IV1[a];
1457 invfX[a+1]=Inv_Sbox1[invfX[a+1]]^RM1[a+1]^IV1[a+1];
1458 invfX[a+2]=Inv_Sbox1[invfX[a+2]]^RM1[a+2]^IV1[a+2];
1459 invfX[a+3]=Inv_Sbox1[invfX[a+3]]^RM1[a+3]^IV1[a+3];
1464 for(int a=0;a<h;a+=4) {
1465 seq_out[ind2+a]=invfX[a];
1466 seq_out[ind2+a+1]=invfX[a+1];
1467 seq_out[ind2+a+2]=invfX[a+2];
1468 seq_out[ind2+a+3]=invfX[a+3];
1471 for(int a=0;a<h;a+=4) {
1472 seq_out[ind1+a]=invgY[a];
1473 seq_out[ind1+a+1]=invgY[a+1];
1474 seq_out[ind1+a+2]=invgY[a+2];
1475 seq_out[ind1+a+3]=invgY[a+3];
1477 for(int a=0;a<h;a+=4) {
1484 for(int a=0;a<h;a+=4) {
1502 int main(int argc, char** argv) {
1511 for(int i=1; i<argc; i++){
1512 if(strncmp(argv[i],"nb",2)==0) nb_test = atoi(&(argv[i][2])); //nb of test
1513 if(strncmp(argv[i],"cbc",3)==0) cbc=1;
1514 if(strncmp(argv[i],"cbcprng",7)==0) {cbcprng=1;cbc=0;}
1515 if(strncmp(argv[i],"ecb",3)==0) ecb = 1;
1516 if(strncmp(argv[i],"ecbprng",7)==0) {ecbprng=1; ecb=0;}
1517 if(strncmp(argv[i],"h",1)==0) h = atoi(&(argv[i][1])); //size of block
1518 if(strncmp(argv[i],"sizebuf",7)==0) size_buf = atoi(&(argv[i][7])); //SIZE of the buffer
1519 if(strncmp(argv[i],"lena",4)==0) lena = atoi(&(argv[i][4])); //Use Lena or buffer
1522 printf("nb times %d\n",nb_test);
1523 printf("cbc %d\n",cbc);
1524 printf("cbcprng %d\n",cbcprng);
1525 printf("ecb %d\n",ecb);
1527 printf("lena %d\n",lena);
1528 printf("size_buf %d\n",size_buf);
1533 int seed=time(NULL);
1534 // cout<<seed<<endl;
1537 uchar Secretkey[key_size];
1539 uchar counter[key_size];
1541 for(int i=0;i<key_size;i++) {
1542 Secretkey[i]=lrand48()&0xFF;
1543 counter[i]=lrand48()&0xFF;
1556 uchar *data_R, *data_G, *data_B;
1565 load_RGB_pixmap("lena.ppm", &width, &height, &data_R, &data_G, &data_B);
1566 // load_RGB_pixmap("8192.ppm", &width, &height, &data_R, &data_G, &data_B);
1567 imsize=width*height*3;
1568 // load_RGB_pixmap("No_ecb_mode_picture.ppm", &width, &height, &data_R, &data_G, &data_B);
1571 width=height=size_buf;
1572 imsize=width*height;
1573 buffer=new uchar[imsize];
1574 for(int i=0;i<imsize;i++) {
1575 buffer[i]=lrand48();
1583 uchar* seq= new uchar[imsize];
1584 uchar* seq2= new uchar[imsize];
1586 int oneD=width*height;
1588 for(int i=0;i<oneD;i++) {
1590 seq[oneD+i]=data_G[i];
1591 seq[2*oneD+i]=data_B[i];
1595 for(int i=0;i<oneD;i++) {
1604 int total_len=imsize;
1606 int len= total_len/h;
1610 uchar *mix=new uchar[256];
1615 for (int i = 0; i < 256 ; i++) {
1616 mix[i]=Secretkey[i]^counter[i];
1621 sha512 = g_compute_checksum_for_string(G_CHECKSUM_SHA512, (const char*) mix, 256);
1622 // g_print("%s\n", sha512);
1632 // cout<<"hash "<<endl;
1633 for (int i = 0; i < 128 ; i++) {
1640 int *Pbox=new int[len];
1641 int *PboxSRM=new int[len/2];
1642 int *PboxSRM2=new int[len/2];
1645 uchar Inv_Sbox1[256];
1646 uchar Inv_Sbox2[256];
1654 double time_encrypt=0;
1655 double time_decrypt=0;
1658 double t=TimeStart();
1659 rc4key(DK, Sbox1, 8);
1662 rc4key(&DK[8], Sbox2, 8);
1664 rc4key(&DK[16], sc, 16);
1665 prga(sc, h*h+256, RM);
1671 rc4keyperm(&DK[72], len, rp, Pbox, 16);
1674 rc4keyperm(&DK[88], len/2, rp, PboxSRM2, 16);
1676 for(int i=0;i<len/2;i++) {
1677 PboxSRM[i]=PboxSRM2[i]&(h-1);
1681 for(int i=0;i<h*2;i++) {
1682 for(int j=0;j<h;j++)
1683 cout<<(int)RM[i*h+j]<<" ";
1690 //time+=TimeStop(t);
1691 //cout<<"Time initializaton "<<time<<endl;
1696 for(int i=0;i<64;i++) {
1707 inverse_tables(Sbox1,256,Inv_Sbox1);
1708 inverse_tables(Sbox2,256,Inv_Sbox2);
1712 lehmer64_seed(myrand);
1719 for(i=0;i<nb_test;i++)
1722 encrypt_cbc_prng<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1724 encrypt_cbc_rm<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1726 encrypt_ecb<4>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1730 for(i=0;i<nb_test;i++)
1733 encrypt_cbc_prng<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1735 encrypt_cbc_rm<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1737 encrypt_ecb<8>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1741 for(i=0;i<nb_test;i++)
1744 encrypt_cbc_prng<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1746 encrypt_cbc_rm<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1748 encrypt_ecb<16>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1752 for(i=0;i<nb_test;i++)
1755 encrypt_cbc_prng<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1757 encrypt_cbc_rm<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1759 encrypt_ecb<32>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1763 for(i=0;i<nb_test;i++)
1766 encrypt_cbc_prng<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1768 encrypt_cbc_rm<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1770 encrypt_ecb<64>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1775 for(i=0;i<nb_test;i++)
1778 encrypt_cbc_prng<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1780 encrypt_cbc_rm<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1782 encrypt_ecb<128>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1787 for(i=0;i<nb_test;i++)
1790 encrypt_cbc_prng<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1792 encrypt_cbc_rm<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1794 encrypt_ecb<256>(seq, seq2,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,myrand,0);
1799 time_encrypt+=TimeStop(t);
1800 //cout<<"Time encrypt "<<
1801 cout<<(double)imsize*nb_test/time_encrypt<<"\t";
1805 for(int i=0;i<oneD;i++) {
1807 data_G[i]=seq2[oneD+i];
1808 data_B[i]=seq2[2*oneD+i];
1810 store_RGB_pixmap("lena2.ppm", data_R, data_G, data_B, width, height);
1814 lehmer64_seed(myrand);
1819 for(i=0;i<nb_test;i++) {
1821 decrypt_cbc_prng<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1823 decrypt_cbc_rm<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1825 decrypt_ecb<4>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1829 for(i=0;i<nb_test;i++) {
1831 decrypt_cbc_prng<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1833 decrypt_cbc_rm<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1835 decrypt_ecb<8>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1839 for(i=0;i<nb_test;i++) {
1841 decrypt_cbc_prng<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1843 decrypt_cbc_rm<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1845 decrypt_ecb<16>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1849 for(i=0;i<nb_test;i++) {
1851 decrypt_cbc_prng<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1853 decrypt_ecb<32>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1857 for(i=0;i<nb_test;i++) {
1859 decrypt_cbc_prng<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1861 decrypt_cbc_rm<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1863 decrypt_ecb<64>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1867 for(i=0;i<nb_test;i++) {
1869 decrypt_cbc_prng<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1871 decrypt_cbc_rm<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1873 decrypt_ecb<128>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1877 for(i=0;i<nb_test;i++) {
1879 decrypt_cbc_prng<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1881 decrypt_cbc_rm<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1883 decrypt_ecb<256>(seq2,seq,len,RM,Pbox,PboxSRM,Sbox1,Sbox2,Inv_Sbox1,Inv_Sbox2,myrand,0);
1888 time_decrypt+=TimeStop(t);
1889 //cout<<"Time decrypt "
1890 cout<<(double)imsize*nb_test/time_decrypt<<"\t";
1893 for(int i=0;i<oneD;i++) {
1895 data_G[i]=seq[oneD+i];
1896 data_B[i]=seq[2*oneD+i];
1898 store_RGB_pixmap("lena3.ppm", data_R, data_G, data_B, width, height);
1902 for(int i=0;i<imsize;i++) {
1903 //cout<<(int)buffer[i]<<endl;
1904 if(buffer[i]!=seq[i]) {
1908 // cout<<"RESULT CORRECT: "<<equal<<endl;