Advertisement
Chris_M_Thomasson

Funny Fractal Encryption ver:0.0.3

Jul 31st, 2015
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <limits.h>
  5. #include <math.h>
  6.  
  7.  
  8.  
  9. /*-------------------------*/
  10. #define CT_LB "\r\n"
  11. #define CT_PREC "%.11lf"
  12. #define CT_PREC_EPS "%.13lf"
  13. #define CT_EPS 0.00000000001
  14. #define CT_MIN(mp_0, mp_1) ((mp_0) < (mp_1) ? (mp_0) : (mp_1))
  15.  
  16. struct main_args
  17. {
  18.     int argc;
  19.     char** argv;
  20. };
  21.  
  22.  
  23.  
  24.  
  25.  
  26. /*-------------------------*/
  27. double
  28. double_to_string_to_double(
  29.     double n
  30. ){
  31.     double nn = 0.0;
  32.     double ndif = 0.0;
  33.  
  34.     char nbuf[UCHAR_MAX + 1] = { '\0' };
  35.  
  36.     sprintf(nbuf, CT_PREC, n);
  37.  
  38.     sscanf(nbuf, "%lf", &nn);
  39.  
  40.     ndif = fabs(n - nn);
  41.  
  42.     /* `nn' has lost precision of `n'.*/
  43.     printf("lost precision: " CT_PREC_EPS CT_LB, ndif);
  44.  
  45.     return nn;
  46. }
  47.  
  48.  
  49. double
  50. string_to_double(
  51.     char const* nbuf
  52. ){
  53.     double n = 0.0;
  54.  
  55.     sscanf(nbuf, "%lf", &n);
  56.  
  57.     return n;
  58. }
  59.  
  60.  
  61. unsigned int
  62. string_to_uint(
  63.     char const* nbuf
  64. ){
  65.     unsigned int n = 0;
  66.  
  67.     sscanf(nbuf, "%u", &n);
  68.  
  69.     return n;
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
  76. /*-------------------------*/
  77. #define SQ(mp_0) ((mp_0)*(mp_0))
  78.  
  79.  
  80. struct complexn
  81. {
  82.     double x;
  83.     double y;
  84. };
  85.  
  86.  
  87. #define COMPLEXN_SINIT() { 0.0, 0.0 }
  88.  
  89.  
  90. struct complexn
  91. complexn_add(
  92.     struct complexn self,
  93.     struct complexn n
  94. ){
  95.     struct complexn p = {
  96.         self.x + n.x,
  97.         self.y + n.y
  98.     };
  99.  
  100.     return p;
  101. }
  102.  
  103.  
  104. struct complexn
  105. complexn_mul(
  106.     struct complexn self,
  107.     struct complexn n
  108. ){
  109.     struct complexn p = {
  110.         self.x * n.x - self.y * n.y,
  111.         self.x * n.y + self.y * n.x
  112.     };
  113.  
  114.     return p;
  115. }
  116.  
  117.  
  118.  
  119.  
  120. /*--------------------------*/
  121. unsigned long
  122. cantor_packing(
  123.     unsigned int n0,
  124.     unsigned int n1
  125. ){
  126.     unsigned long ret = 0.5 * (n0 + n1) * (n0 + n1 + 1) + n1;
  127.  
  128.     return ret;
  129. }
  130.  
  131.  
  132.  
  133.  
  134.  
  135. /*-------------------------*/
  136. #define FFE_MARGS_NMIN 7
  137. #define FFE_BUF_NMAX (1024 * 756)
  138. #define FFE_ABETN (UCHAR_MAX + 1U)
  139.  
  140.  
  141. struct ffe_buf
  142. {
  143.     unsigned char buf[FFE_BUF_NMAX];
  144.     size_t bufsz;
  145. };
  146.  
  147.  
  148. struct ffe_abet
  149. {
  150.     unsigned int abet[UCHAR_MAX + 1];
  151. };
  152.  
  153.  
  154. struct ffe_cplane
  155. {
  156.     double xmin;
  157.     double xmax;
  158.     double ymin;
  159.     double ymax;
  160. };
  161.  
  162. unsigned int
  163. ffe_cplane_iterate(
  164.     struct complexn c,
  165.     struct complexn j,
  166.     unsigned int imax
  167. ){
  168.     unsigned int i = 0;
  169.     unsigned int imod = 0;
  170.  
  171.     double dmin = 999999999999.0;
  172.  
  173.     struct complexn z = c;
  174.     c = j; /* Julia Mode */
  175.  
  176.     for (i = 0; i < imax; ++i)
  177.     {
  178.         double zd = 0.0;
  179.  
  180.         z = complexn_mul(z, z);
  181.         z = complexn_add(z, c);
  182.  
  183.         zd = SQ(z.x) + SQ(z.y);
  184.  
  185.         dmin = CT_MIN(dmin, zd);
  186.  
  187.         if (zd > 4.0)
  188.         {
  189.             imod = (unsigned int)((20371 + i) *
  190.                 fabs(cos(i * i * dmin * 252307))) % FFE_ABETN;
  191.  
  192.             return imod;
  193.         }
  194.     }
  195.  
  196.     imod = (unsigned int)(40377 *
  197.         fabs(sin(dmin * 152307))) % FFE_ABETN;
  198.  
  199.     return imod;
  200. }
  201.  
  202.  
  203.  
  204.  
  205. size_t
  206. ffe_buf_read(
  207.     struct ffe_buf* const self,
  208.     FILE* const file
  209. ){
  210.     self->bufsz = fread(self->buf, 1, FFE_BUF_NMAX, file);
  211.  
  212.     if (self->bufsz == FFE_BUF_NMAX)
  213.     {
  214.         return self->bufsz;
  215.     }
  216.  
  217.     if (! ferror(file))
  218.     {
  219.         if (feof(file))
  220.         {
  221.             return self->bufsz;
  222.         }
  223.     }
  224.  
  225.     printf("fread error!" CT_LB);
  226.  
  227.     return 0;
  228. }
  229.  
  230.  
  231. size_t
  232. ffe_buf_write(
  233.     struct ffe_buf const* const self,
  234.     FILE* const file
  235. ){
  236.     size_t bufsz = fwrite(self->buf, 1, self->bufsz, file);
  237.  
  238.     if (bufsz == FFE_BUF_NMAX)
  239.     {
  240.         return bufsz;
  241.     }
  242.  
  243.     if (! ferror(file))
  244.     {
  245.         return bufsz;
  246.     }
  247.  
  248.     printf("fwrite error!" CT_LB);
  249.  
  250.     return 0;
  251. }
  252.  
  253.  
  254.  
  255.  
  256.  
  257. /*-------------------------*/
  258.  
  259. struct ffe_encrypt
  260. {
  261.     struct ffe_cplane cplane;      /* private key: plane */
  262.     struct complexn jplane;        /* private key: Julia */
  263.     struct ffe_cplane pub_cplane;  /* public key: plane */
  264.  
  265.     struct ffe_buf* ebuf;
  266.     unsigned int flags;
  267.     size_t uwidth;
  268.     size_t fbytes;
  269.  
  270.     FILE* fctext;  /* clear text file */
  271.     FILE* fcipher; /* cipher text file */
  272. };
  273.  
  274.  
  275. int
  276. ffe_encrypt_main_args_fctext_parse(
  277.     struct ffe_encrypt* const self,
  278.     struct main_args* const margs
  279. ){
  280.     if (margs->argc == FFE_MARGS_NMIN + 1)
  281.     {
  282.         unsigned int base = 1;
  283.  
  284.         /* Try to open the cleartext [in] */
  285.         self->fctext = fopen(margs->argv[base], "rb");
  286.  
  287.         if (self->fctext)
  288.         {
  289.             printf("Opened the cleartext [in]! <%s>" CT_LB, margs->argv[1]);
  290.  
  291.             /* Try to open the ciphertext [out] */
  292.             self->fcipher = fopen(margs->argv[base + 1], "wb");
  293.  
  294.             if (self->fcipher)
  295.             {
  296.                 printf("Opened the ciphertext [out]! <%s>" CT_LB, margs->argv[base + 1]);
  297.  
  298.                 return 1;
  299.             }
  300.  
  301.             else
  302.             {
  303.                 printf("Cannot open the ciphertext [out]! <%s>" CT_LB, margs->argv[base + 1]);
  304.             }
  305.  
  306.             fclose(self->fctext); /* check for errors!?!? */
  307.         }
  308.  
  309.         printf("Cannot open the cleartext [int]! <%s>" CT_LB, margs->argv[base]);
  310.     }
  311.  
  312.     return 0;
  313. }
  314.  
  315.  
  316. int
  317. ffe_encrypt_main_args_cplane_parse(
  318.     struct ffe_encrypt* const self,
  319.     struct main_args* const margs
  320. ){
  321.     if (margs->argc == FFE_MARGS_NMIN + 1)
  322.     {
  323.         /* Parse the complex plane dimensions (2d for now) */
  324.         unsigned int const icpbase = 3;
  325.  
  326.         self->flags = string_to_uint(margs->argv[icpbase + 0]);
  327.  
  328.         self->cplane.xmin = string_to_double(margs->argv[icpbase + 1]);
  329.         self->cplane.xmax = string_to_double(margs->argv[icpbase + 2]);
  330.  
  331.         self->cplane.ymin = string_to_double(margs->argv[icpbase + 3]);
  332.         self->cplane.ymax = string_to_double(margs->argv[icpbase + 4]);
  333.  
  334.         return 1;
  335.     }
  336.  
  337.     return 0;
  338. }
  339.  
  340.  
  341. int
  342. ffe_encrypt_parse(
  343.     struct ffe_encrypt* const self,
  344.     struct main_args* const margs
  345. ){
  346.     if (ffe_encrypt_main_args_cplane_parse(self, margs))
  347.     {
  348.         printf(
  349.             "xmin = " CT_PREC CT_LB "xmax = " CT_PREC CT_LB
  350.             "ymin = " CT_PREC CT_LB "ymax = " CT_PREC CT_LB,
  351.             self->cplane.xmin, self->cplane.xmax,
  352.             self->cplane.ymin, self->cplane.ymax
  353.         );
  354.  
  355.         if (ffe_encrypt_main_args_fctext_parse(self, margs))
  356.         {
  357.             return 1;
  358.         }
  359.     }
  360.  
  361.     return 0;
  362. }
  363.  
  364.  
  365. void
  366. ffe_encrypt_first_pass(
  367.     struct ffe_encrypt* const self,
  368.     struct main_args const* const margs
  369. ){
  370.     self->fbytes = 0;
  371.     double cmean_sum = 0.0;
  372.     size_t cpack_msum = 0;
  373.     size_t cxor = 0;
  374.  
  375.     for (;;)
  376.     {
  377.         size_t rbytes = ffe_buf_read(self->ebuf, self->fctext);
  378.  
  379.         if (rbytes != 0)
  380.         {
  381.             size_t i = 0;
  382.  
  383.             for (i = 0; i < rbytes; i += 2)
  384.             {
  385.                 double cavg_0 = self->ebuf->buf[i] / ((double)UCHAR_MAX);
  386.                 double cavg_1 = self->ebuf->buf[i + 1] / ((double)UCHAR_MAX);
  387.  
  388.                 cmean_sum = cmean_sum + cavg_0 + cavg_1;
  389.  
  390.                 cpack_msum =
  391.                     (cpack_msum + cantor_packing(
  392.                     self->ebuf->buf[i], self->ebuf->buf[i + 1])) % 1073741823;
  393.  
  394.                 cxor = cxor ^ self->ebuf->buf[i];
  395.                 cxor = cxor ^ self->ebuf->buf[i + 1];
  396.                 cxor = cxor ^ cpack_msum;
  397.  
  398.                 cmean_sum += ((cxor) % (UCHAR_MAX)) / ((double)UCHAR_MAX);
  399.             }
  400.  
  401.             printf("ffe_encrypt_first_pass processed: %lu bytes" CT_LB, (unsigned long)rbytes);
  402.  
  403.             self->fbytes = self->fbytes + rbytes;
  404.  
  405.             continue;
  406.         }
  407.  
  408.         break;
  409.     }
  410.  
  411.     printf(CT_LB);
  412.  
  413.     {
  414.         /* build new location/offset (Public Key) into (Private Key) */
  415.         double cavg = cmean_sum / ((double)self->fbytes);
  416.         double cfmod = ((cavg + (cpack_msum / cxor)) * cxor) / ((cxor + cpack_msum) % 1237);
  417.         cfmod = fmod(cfmod, 1.0);
  418.  
  419.         /* try to avoid divide-by-zero conditions */
  420.         if (! cxor) cxor = 1;
  421.         if (! cpack_msum) cpack_msum = 1;
  422.  
  423.         {
  424.             /* Generate Public Key Scale */
  425.             unsigned int sum_mod = 5;
  426.             double fmod_scale = ((1.0 + sqrt(5.0)) / 2.0) / 2.0;
  427.             double pub_xmin_scale = fmod(cavg * cfmod + (cxor / (double)cpack_msum), fmod_scale);
  428.             double pub_xmax_scale = fmod((cavg * cfmod) * cxor + (cxor / (double)cpack_msum), fmod_scale);
  429.             double pub_ymin_scale = fmod(cavg + cfmod + (cxor / (double)cpack_msum), fmod_scale);
  430.             double pub_ymax_scale = fmod((cavg + cfmod) * cxor + (cxor / (double)cpack_msum), fmod_scale);
  431.  
  432.             pub_xmin_scale = pub_xmin_scale / ((cxor % 7) + 3);
  433.             pub_xmax_scale = pub_xmax_scale / (((cxor + cpack_msum) % 7) + 1);
  434.             pub_ymin_scale = pub_ymin_scale / (((cxor * cpack_msum) % 7) + 1);
  435.             pub_ymax_scale = pub_ymax_scale / (((((cxor * 3 + cpack_msum) + 7)) % 7) + 1);
  436.  
  437.             pub_xmin_scale = (fmod(pub_xmin_scale + (cavg / 7), fmod_scale)) + self->cplane.xmin;
  438.             pub_xmax_scale = (fmod(pub_xmax_scale + (cavg / 7), fmod_scale)) + self->cplane.xmax;
  439.             pub_ymin_scale = (fmod(pub_ymin_scale + (cavg / 7), fmod_scale)) + self->cplane.ymin;
  440.             pub_ymax_scale = (fmod(pub_ymax_scale + (cavg / 7), fmod_scale)) + self->cplane.ymax;
  441.  
  442.             cavg = fmod(cavg, fmod_scale);
  443.             cfmod = fmod(cfmod, fmod_scale);
  444.             cavg = 1.0 - fmod(cavg + cfmod, fmod_scale);
  445.  
  446.             cpack_msum = cpack_msum * cavg + (cpack_msum % sum_mod) + 1;
  447.             cxor = cxor + (cxor % sum_mod) + 1;
  448.  
  449.  
  450.             self->pub_cplane.xmin = double_to_string_to_double(pub_xmin_scale);
  451.             self->pub_cplane.xmax = double_to_string_to_double(pub_xmax_scale);
  452.             self->pub_cplane.ymin = double_to_string_to_double(pub_ymin_scale);
  453.             self->pub_cplane.ymax = double_to_string_to_double(pub_ymax_scale);
  454.  
  455.  
  456.             /* decrypt setting 0 */
  457.             if (self->flags == 1)
  458.             {
  459.                 self->pub_cplane = self->cplane;
  460.             }
  461.  
  462.  
  463.             printf("pub_xmin_scale = " CT_PREC CT_LB, self->pub_cplane.xmin);
  464.             printf("pub_xmax_scale = " CT_PREC CT_LB, self->pub_cplane.xmax);
  465.             printf("pub_ymin_scale = " CT_PREC CT_LB, self->pub_cplane.ymin);
  466.             printf("pub_ymax_scale = " CT_PREC CT_LB, self->pub_cplane.ymax);
  467.         }
  468.     }
  469. }
  470.  
  471.  
  472. int
  473. ffe_encrypt_second_pass(
  474.     struct ffe_encrypt* const self,
  475.     struct main_args const* const margs
  476. ){
  477.     double xwidth = self->pub_cplane.xmax - self->pub_cplane.xmin;
  478.     double xheight = self->pub_cplane.ymax - self->pub_cplane.ymin;
  479.  
  480.     double xstep = xwidth / self->uwidth;
  481.     double ystep = xheight / self->uwidth;
  482.  
  483.     struct complexn c = COMPLEXN_SINIT();
  484.  
  485.     {
  486.         size_t i = 0;
  487.  
  488.         rewind(self->fctext);
  489.  
  490.         printf(CT_LB);
  491.  
  492.         for (i = 0; i < self->fbytes;)
  493.         {
  494.             size_t rbytes = ffe_buf_read(self->ebuf, self->fctext);
  495.  
  496.             if (rbytes != 0)
  497.             {
  498.                 size_t ic = 0;
  499.  
  500.                 for (ic = 0; ic < rbytes; ++ic, ++i)
  501.                 {
  502.                     /* Apply the cipher */
  503.                     unsigned int imod = 0;
  504.                     unsigned int cmod = self->ebuf->buf[ic];
  505.  
  506.                     unsigned int yi = i / self->uwidth;
  507.                     unsigned int xi = i - yi * self->uwidth;
  508.  
  509.                     c.x = self->pub_cplane.xmin + xi * xstep;
  510.                     c.y = self->pub_cplane.ymin + yi * ystep;
  511.  
  512.                     imod = ffe_cplane_iterate(c, self->jplane, 503);
  513.  
  514.  
  515.  
  516.                     /* encrypt/decrypt */
  517.                     if (self->flags == 0)
  518.                     {
  519.                         imod = (imod + cmod) % FFE_ABETN;
  520.                     }
  521.  
  522.                     else
  523.                     {
  524.                         int ec = cmod - imod;
  525.  
  526.                         if (ec < 0)
  527.                         {
  528.                             ec = cmod + FFE_ABETN;
  529.                             imod = abs(imod - ec);
  530.                         }
  531.  
  532.                         else
  533.                         {
  534.                             imod = ec;
  535.                         }
  536.                     }
  537.  
  538.                     /* commit cipher char to buffer */
  539.                     self->ebuf->buf[ic] = imod;
  540.  
  541.                     if (! (i % 1000))
  542.                     {
  543.                         printf("i = %lu\r", (unsigned long)i);
  544.                     }
  545.                 }
  546.  
  547.                 /* write buffer to cipher file */
  548.                 {
  549.                     size_t wbytes = ffe_buf_write(self->ebuf, self->fcipher);
  550.  
  551.                     if (wbytes < rbytes)
  552.                     {
  553.                         /* need to loop until the buffer is commited! */
  554.                         printf("SINGLE WRITE IS FUC%%ED!" CT_LB);
  555.                         getchar();
  556.                     }
  557.                 }
  558.  
  559.                 continue;
  560.             }
  561.  
  562.             break;
  563.         }
  564.  
  565.         if (i == self->fbytes)
  566.         {
  567.             printf("Passed at %lu bytes!" CT_LB CT_LB, (unsigned long)i);
  568.  
  569.             return 1;
  570.         }
  571.     }
  572.  
  573.     return 0;
  574. }
  575.  
  576.  
  577.  
  578.  
  579. /*-------------------------*/
  580. int
  581. main(
  582.     int argc,
  583.     char* argv[]
  584. ){
  585.     struct main_args self = { argc, argv };
  586.  
  587.     {
  588.         struct ffe_buf ebuf = { { '\0' } };
  589.  
  590.         struct ffe_encrypt fenc = {
  591.             { -2.0, 2.0, -2.0, 2.0, },      /* private key: plane */
  592.  
  593.             /* need to put this in the cmd line damn it! */
  594.             { -0.7, 0.25 },                 /* private key: Julia */
  595.  
  596.             { 0.0, 0.0, 0.0, 0.0, },        /* public key */
  597.             &ebuf,
  598.             0,
  599.             0,
  600.             0,
  601.             NULL,
  602.             NULL
  603.         };
  604.  
  605.         if (ffe_encrypt_parse(&fenc, &self))
  606.         {
  607.             ffe_encrypt_first_pass(&fenc, &self);
  608.  
  609.             printf(CT_LB);
  610.             printf("file size: %lu" CT_LB, (unsigned long)fenc.fbytes);
  611.  
  612.             /* gain dims of fractal pad */
  613.             fenc.uwidth = (sqrt(fenc.fbytes) + 1.681) * 1.0;
  614.  
  615.             printf("fractal dims: %lu x %lu" CT_LB,
  616.                 (unsigned long)fenc.uwidth,
  617.                 (unsigned long)fenc.uwidth
  618.             );
  619.  
  620.             if (ffe_encrypt_second_pass(&fenc, &self))
  621.             {
  622.                 if (fenc.flags == 0)
  623.                 {
  624.                     unsigned int base = 1;
  625.  
  626.                     printf(CT_LB CT_LB "Decrypt Command Line:" CT_LB CT_LB);
  627.                     printf("%s %s_dc 1 " CT_PREC " " CT_PREC " " CT_PREC " " CT_PREC CT_LB CT_LB,
  628.                         self.argv[base + 1],
  629.                         self.argv[base],
  630.                         fenc.pub_cplane.xmin,
  631.                         fenc.pub_cplane.xmax,
  632.                         fenc.pub_cplane.ymin,
  633.                         fenc.pub_cplane.ymax);
  634.                 }
  635.  
  636.                 printf(CT_LB "No Errors Reported!" CT_LB CT_LB);
  637.             }
  638.  
  639.             /* Check for ERRORS! */
  640.             fclose(fenc.fcipher);
  641.             printf("Closed the ciphertext [out]! <%s>" CT_LB, self.argv[2]);
  642.  
  643.             fclose(fenc.fctext);
  644.             printf("Closed the cleartext [in]! <%s>" CT_LB, self.argv[1]);
  645.         }
  646.  
  647.         else
  648.         {
  649.             printf("failed to parse!" CT_LB);
  650.         }
  651.     }
  652.  
  653.     puts(CT_LB "Complete!" CT_LB);
  654.     getchar();
  655.  
  656.     return 0;
  657. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement