Advertisement
Chris_M_Thomasson

Funny Fractal Encryption

Jul 7th, 2015
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.18 KB | None | 0 0
  1. /* Mandelbrot Set ASCII by Chris M. Thomasson   :^)
  2. ___________________________________________________________________*/
  3.  
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <string.h>
  9.  
  10.  
  11. #define CT_MIN(mp_0, mp_1) (((mp_0) < (mp_1)) ? (mp_0) : (mp_1))
  12. #define CT_SQ(mp_n) ((mp_n) * (mp_n))
  13.  
  14.  
  15. #define CT_MBROT_ABET_PUNC \
  16.     " ,./\\;'[]-=<>?:""{}_+`~"
  17.  
  18. #define CT_MBROT_ABET_NUM_SHIFT \
  19.     "!@#$%^&*()"
  20.  
  21. #define CT_MBROT_ABET_NUM \
  22.     "0123456789"
  23.  
  24. #define CT_MBROT_ABET_UPPER \
  25.     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  26.  
  27. #define CT_MBROT_ABET_LOWER \
  28.     "abcdefghijklmnopqrstuvwxyz"
  29.  
  30.  
  31. #define CT_MBROT_ABET       \
  32.     CT_MBROT_ABET_UPPER     \
  33.     CT_MBROT_ABET_LOWER     \
  34.     CT_MBROT_ABET_NUM       \
  35.     CT_MBROT_ABET_NUM_SHIFT \
  36.     CT_MBROT_ABET_PUNC
  37.  
  38. #define CT_MBROT_ABET_MOD (sizeof(CT_MBROT_ABET) - 1U)
  39.  
  40. #define CT_MBROT_ABET_LOOKUP(mp_char) \
  41.     ((strchr(CT_MBROT_ABET, (mp_char))) - CT_MBROT_ABET)
  42.  
  43.  
  44. struct ct_mbrotcp_pixel
  45. {
  46.     double x;
  47.     double y;
  48. };
  49.  
  50.  
  51. struct ct_mbrotcp_plane
  52. {
  53.     double xmin;
  54.     double xmax;
  55.     double ymin;
  56.     double ymax;
  57. };
  58.  
  59.  
  60. struct ct_mbrotvp_plane
  61. {
  62.     unsigned int width;
  63.     unsigned int height;
  64. };
  65.  
  66.  
  67.  
  68.  
  69. unsigned int
  70. ct_mbrotpixel(
  71.     struct ct_mbrotcp_pixel const* pixel_c,
  72.     unsigned int const imax
  73. ){
  74.     unsigned int i = 0;
  75.     unsigned int ret = 0;
  76.  
  77.     struct ct_mbrotcp_pixel pixel_z = *pixel_c;
  78.  
  79.     struct ct_mbrotcp_pixel pixel_z_sq = {
  80.         CT_SQ(pixel_z.x), CT_SQ(pixel_z.y)
  81.     };
  82.  
  83.     struct ct_mbrotcp_pixel pixel_cj = { -0.7, 0.0 };
  84.  
  85.     double cmin = 9999999999999.0;
  86.  
  87.     for (i = 0; i < imax; ++i)
  88.     {
  89.         pixel_z.y = pixel_z.x * pixel_z.y * 2.0 + pixel_cj.y;
  90.         pixel_z.x = pixel_z_sq.x - pixel_z_sq.y + pixel_cj.x;
  91.  
  92.         pixel_z_sq.x = CT_SQ(pixel_z.x);
  93.         pixel_z_sq.y = CT_SQ(pixel_z.y);
  94.  
  95.         cmin = CT_MIN(cmin, CT_SQ(pixel_z_sq.x) + CT_SQ(pixel_z_sq.y));
  96.  
  97.         if (pixel_z_sq.x + pixel_z_sq.y > 2357)
  98.         {
  99.             ret = (unsigned int)(113747 * fabs(cos(i * cmin * 573)));
  100.  
  101.             putchar(CT_MBROT_ABET[ret % CT_MBROT_ABET_MOD]);
  102.  
  103.             return ret;
  104.         }
  105.     }
  106.  
  107.     {
  108.         unsigned int mc = 0;
  109.  
  110.         ret = (unsigned int)(234777 * fabs(sin(i * cmin * 3233)));
  111.         mc = ret % CT_MBROT_ABET_MOD;
  112.  
  113.         putchar(CT_MBROT_ABET[mc]);
  114.  
  115.         /* uncomment to display points that escape as "blanks" */
  116.         /* putchar(' '); */
  117.     }
  118.  
  119.     return ret;
  120. }
  121.  
  122.  
  123. void
  124. ct_mbrotplane(
  125.     struct ct_mbrotcp_plane const* cp_plane,
  126.     struct ct_mbrotvp_plane const* vp_plane,
  127.     unsigned int const imax,
  128.     unsigned int* icounts
  129. ){
  130.     unsigned int vpyi = 0;
  131.  
  132.     struct ct_mbrotcp_pixel cp_pixel = { 0.0, 0.0 };
  133.  
  134.     double const cp_width = cp_plane->xmax - cp_plane->xmin;
  135.     double const cp_height = cp_plane->ymax - cp_plane->ymin;
  136.  
  137.     double const xstep = cp_width / vp_plane->width;
  138.     double const ystep = cp_height / vp_plane->height;
  139.  
  140.     for (vpyi = 0; vpyi < vp_plane->height; ++vpyi)
  141.     {
  142.         unsigned int vpxi = 0;
  143.  
  144.         cp_pixel.y = cp_plane->ymax - vpyi * ystep;
  145.  
  146.         for (vpxi = 0; vpxi < vp_plane->width; ++vpxi)
  147.         {
  148.             cp_pixel.x = cp_plane->xmin + vpxi * xstep;
  149.  
  150.             unsigned int icount = ct_mbrotpixel(&cp_pixel, imax);
  151.  
  152.             if (icounts)
  153.             {
  154.                 unsigned int i = (vpyi * vp_plane->width) + vpxi;
  155.  
  156.                 icounts[i] = icount;
  157.             }
  158.         }
  159.  
  160.         putchar('\n');
  161.     }
  162. }
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. /* Funny Fractal Encryption
  171. ___________________________________________________________________*/
  172.  
  173. #define CT_FFE_ABET_NUM     CT_MBROT_ABET_PUNC
  174. #define CT_FFE_ABET_UPPER   CT_MBROT_ABET_UPPER
  175. #define CT_FFE_ABET_LOWER   CT_MBROT_ABET_LOWER
  176. #define CT_FFE_ABET         CT_MBROT_ABET
  177. #define CT_FFE_ABET_MOD     CT_MBROT_ABET_MOD
  178. #define CT_FFE_ABET_LOOKUP  CT_MBROT_ABET_LOOKUP
  179.  
  180.  
  181.  
  182. struct ct_ffe
  183. {
  184.     struct ct_mbrotcp_plane cp_plane;
  185.     struct ct_mbrotvp_plane vp_plane;
  186.     unsigned int* icounts;
  187. };
  188.  
  189.  
  190.  
  191. int
  192. ct_ffe_create(
  193.     struct ct_ffe* const self
  194. ){
  195.     unsigned int n = self->vp_plane.width * self->vp_plane.height;
  196.  
  197.     self->icounts = calloc(n, sizeof(*self->icounts));
  198.  
  199.     if (self->icounts)
  200.     {
  201.         return 1;
  202.     }
  203.  
  204.     return 0;
  205. }
  206.  
  207.  
  208. void
  209. ct_ffe_destroy(
  210.     struct ct_ffe* const self
  211. ){
  212.     free(self->icounts);
  213. }
  214.  
  215.  
  216. void
  217. ct_ffe_encrypt_prepare(
  218.     struct ct_ffe* const self,
  219.     unsigned int const imax
  220. ){
  221.     ct_mbrotplane(&self->cp_plane, &self->vp_plane, imax, self->icounts);
  222. }
  223.  
  224.  
  225. int
  226. ct_ffe_encrypt(
  227.     struct ct_ffe* const self,
  228.     char const* omsg,
  229.     unsigned int omsg_size,
  230.     char* emsg,
  231.     unsigned int emsg_size
  232. ){
  233.     unsigned int i = 0;
  234.  
  235.     if (emsg_size < omsg_size)
  236.     {
  237.         return 0;
  238.     }
  239.  
  240.     for (i = 0; i < omsg_size; ++i)
  241.     {
  242.         unsigned int icount = self->icounts[i] % CT_FFE_ABET_MOD;
  243.         unsigned int cx = CT_FFE_ABET_LOOKUP(omsg[i]);
  244.         unsigned int ec = (cx + icount) % CT_FFE_ABET_MOD;
  245.  
  246.         emsg[i] = CT_FFE_ABET[ec];
  247.     }
  248.  
  249.     return 1;
  250. }
  251.  
  252.  
  253. int
  254. ct_ffe_decrypt(
  255.     struct ct_ffe* const self,
  256.     char const* emsg,
  257.     unsigned int emsg_size,
  258.     char* dmsg,
  259.     unsigned int dmsg_size
  260. ){
  261.     unsigned int i = 0;
  262.  
  263.     if (dmsg_size < emsg_size)
  264.     {
  265.         return 0;
  266.     }
  267.  
  268.     for (i = 0; i < emsg_size; ++i)
  269.     {
  270.         unsigned int icount = self->icounts[i] % CT_FFE_ABET_MOD;
  271.         unsigned int cx = CT_FFE_ABET_LOOKUP(emsg[i]);
  272.  
  273.         int ec = cx - icount;
  274.  
  275.         if (ec < 0)
  276.         {
  277.             ec = cx + CT_FFE_ABET_MOD;
  278.             ec = abs(icount - ec);
  279.         }
  280.  
  281.         dmsg[i] = CT_FFE_ABET[ec];
  282.     }
  283.  
  284.     return 1;
  285. }
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293. /* Sample FFE Usage Program
  294. ___________________________________________________________________*/
  295. int
  296. main(void)
  297. {
  298.     char omsg[] = "/Fun\\ <with> [Fractals]! (tm) `Hello'";
  299.     char emsg[sizeof(omsg)] = { '\0' };
  300.     char dmsg[sizeof(omsg)] = { '\0' };
  301.  
  302.     unsigned int n = sizeof(omsg) - 1;
  303.     unsigned int dim = (((unsigned int)sqrt(n)) + 1);
  304.     unsigned int dim_scale = 3;
  305.     unsigned int gwidth = dim * dim_scale;
  306.     unsigned int gheight = gwidth;
  307.  
  308.  
  309.     struct ct_ffe fe = {
  310.         { -1.1, 1.0, -1.0, 1.413 },
  311.         { gwidth, gheight },
  312.         NULL
  313.     };
  314.  
  315.     if (ct_ffe_create(&fe))
  316.     {
  317.         printf(
  318.             "CT_FFE_ABET_MOD = %u\r\n"
  319.             "________________________________\r\n"
  320.             "Fractal Pad: z^2 + c (Julia Set)\r\n"
  321.             "Grid Dims:   %u x %u\r\n"
  322.             "--------------------------------\r\n",
  323.             (unsigned int)CT_FFE_ABET_MOD,
  324.             gwidth,
  325.             gheight
  326.         );
  327.  
  328.         ct_ffe_encrypt_prepare(&fe, CT_FFE_ABET_MOD * dim_scale);
  329.  
  330.         printf("________________________________\r\n");
  331.  
  332.         if (ct_ffe_encrypt(&fe, omsg, n, emsg, n))
  333.         {
  334.             if (ct_ffe_decrypt(&fe, emsg, n, dmsg, n))
  335.             {
  336.                 printf("\r\noriginal  = %s\r\n\r\n", omsg);
  337.                 printf("encrypted = %s\r\n\r\n", emsg);
  338.                 printf("decrypted = %s\r\n", dmsg);
  339.             }
  340.         }
  341.  
  342.         ct_ffe_destroy(&fe);
  343.     }
  344.  
  345.     return 0;
  346. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement