Chris_M_Thomasson

Funny Fractal Encryption ver:0.0.1

Jul 9th, 2015
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.89 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. #define CT_MBROT_ABET_SCALE(mp_idx) \
  44.     ((mp_idx) / (CT_MBROT_ABET_MOD - 1.0))
  45.  
  46.  
  47. struct ct_mbrotcp_pixel
  48. {
  49.     double x;
  50.     double y;
  51. };
  52.  
  53.  
  54. struct ct_mbrotcp_plane
  55. {
  56.     double xmin;
  57.     double xmax;
  58.     double ymin;
  59.     double ymax;
  60. };
  61.  
  62.  
  63. struct ct_mbrotvp_plane
  64. {
  65.     unsigned int width;
  66.     unsigned int height;
  67. };
  68.  
  69.  
  70.  
  71.  
  72. unsigned int
  73. ct_mbrotpixel(
  74.     struct ct_mbrotcp_pixel const* pixel_c,
  75.     struct ct_mbrotcp_pixel const* pixel_cj,
  76.     unsigned int const imax
  77. ){
  78.     unsigned int i = 0;
  79.     unsigned int ret = 0;
  80.  
  81.     struct ct_mbrotcp_pixel pixel_z = *pixel_c;
  82.  
  83.     struct ct_mbrotcp_pixel pixel_z_sq = {
  84.         CT_SQ(pixel_z.x), CT_SQ(pixel_z.y)
  85.     };
  86.  
  87.     //struct ct_mbrotcp_pixel pixel_cj = { -0.7, 0.3 };
  88.  
  89.     double cmin = 9999999999999.0;
  90.  
  91.     for (i = 0; i < imax; ++i)
  92.     {
  93.         pixel_z.y = pixel_z.x * pixel_z.y * 2.0 + pixel_cj->y;
  94.         pixel_z.x = pixel_z_sq.x - pixel_z_sq.y + pixel_cj->x;
  95.  
  96.         pixel_z_sq.x = CT_SQ(pixel_z.x);
  97.         pixel_z_sq.y = CT_SQ(pixel_z.y);
  98.  
  99.         cmin = CT_MIN(cmin, CT_SQ(pixel_z_sq.x) + CT_SQ(pixel_z_sq.y));
  100.  
  101.         if (pixel_z_sq.x + pixel_z_sq.y > 2357)
  102.         {
  103.             ret = (unsigned int)(113747 * fabs(cos(i * cmin * 573)));
  104.  
  105.             putchar(CT_MBROT_ABET[ret % CT_MBROT_ABET_MOD]);
  106.  
  107.             return ret;
  108.         }
  109.     }
  110.  
  111.     {
  112.         unsigned int mc = 0;
  113.  
  114.         ret = (unsigned int)(234777 * fabs(sin(i * cmin * 3233)));
  115.         mc = ret % CT_MBROT_ABET_MOD;
  116.  
  117.         putchar(CT_MBROT_ABET[mc]);
  118.  
  119.         /* uncomment to display points that escape as "blanks" */
  120.         /* putchar(' '); */
  121.     }
  122.  
  123.     return ret;
  124. }
  125.  
  126.  
  127. void
  128. ct_mbrotplane(
  129.     struct ct_mbrotcp_plane const* cp_plane,
  130.     struct ct_mbrotvp_plane const* vp_plane,
  131.     struct ct_mbrotcp_pixel const* pixel_cj,
  132.     unsigned int const imax,
  133.     unsigned int* icounts
  134. ){
  135.     unsigned int vpyi = 0;
  136.  
  137.     struct ct_mbrotcp_pixel cp_pixel = { 0.0, 0.0 };
  138.  
  139.     double const cp_width = cp_plane->xmax - cp_plane->xmin;
  140.     double const cp_height = cp_plane->ymax - cp_plane->ymin;
  141.  
  142.     double const xstep = cp_width / vp_plane->width;
  143.     double const ystep = cp_height / vp_plane->height;
  144.  
  145.     for (vpyi = 0; vpyi < vp_plane->height; ++vpyi)
  146.     {
  147.         unsigned int vpxi = 0;
  148.  
  149.         cp_pixel.y = cp_plane->ymax - vpyi * ystep;
  150.  
  151.         for (vpxi = 0; vpxi < vp_plane->width; ++vpxi)
  152.         {
  153.             cp_pixel.x = cp_plane->xmin + vpxi * xstep;
  154.  
  155.             unsigned int icount = ct_mbrotpixel(&cp_pixel, pixel_cj, imax);
  156.  
  157.             if (icounts)
  158.             {
  159.                 unsigned int i = (vpyi * vp_plane->width) + vpxi;
  160.  
  161.                 icounts[i] = icount;
  162.             }
  163.         }
  164.  
  165.         putchar('\n');
  166.     }
  167. }
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175. /* Funny Fractal Encryption
  176. ___________________________________________________________________*/
  177.  
  178. #define CT_FFE_ABET         CT_MBROT_ABET
  179. #define CT_FFE_ABET_SCALE   CT_MBROT_ABET_SCALE
  180. #define CT_FFE_ABET_MOD     CT_MBROT_ABET_MOD
  181. #define CT_FFE_ABET_LOOKUP  CT_MBROT_ABET_LOOKUP
  182.  
  183.  
  184.  
  185. struct ct_ffe
  186. {
  187.     struct ct_mbrotcp_plane cp_plane;
  188.     struct ct_mbrotvp_plane vp_plane;
  189.     unsigned int* icounts;
  190. };
  191.  
  192.  
  193.  
  194. int
  195. ct_ffe_create(
  196.     struct ct_ffe* const self
  197. ){
  198.     unsigned int n = self->vp_plane.width * self->vp_plane.height;
  199.  
  200.     self->icounts = calloc(n, sizeof(*self->icounts));
  201.  
  202.     if (self->icounts)
  203.     {
  204.         return 1;
  205.     }
  206.  
  207.     return 0;
  208. }
  209.  
  210.  
  211. void
  212. ct_ffe_destroy(
  213.     struct ct_ffe* const self
  214. ){
  215.     free(self->icounts);
  216. }
  217.  
  218.  
  219. void
  220. ct_ffe_encrypt_prepare(
  221.     struct ct_ffe* const self,
  222.     struct ct_mbrotcp_pixel const* pixel_cj,
  223.     unsigned int const imax
  224. ){
  225.     ct_mbrotplane(&self->cp_plane, &self->vp_plane, pixel_cj, imax, self->icounts);
  226. }
  227.  
  228.  
  229. int
  230. ct_ffe_encrypt(
  231.     struct ct_ffe* const self,
  232.     char const* omsg,
  233.     unsigned int omsg_size,
  234.     char* emsg,
  235.     unsigned int emsg_size
  236. ){
  237.     unsigned int i = 0;
  238.  
  239.     if (emsg_size < omsg_size)
  240.     {
  241.         return 0;
  242.     }
  243.  
  244.     for (i = 0; i < omsg_size; ++i)
  245.     {
  246.         unsigned int icount = self->icounts[i] % CT_FFE_ABET_MOD;
  247.         unsigned int cx = CT_FFE_ABET_LOOKUP(omsg[i]);
  248.         unsigned int ec = (cx + icount) % CT_FFE_ABET_MOD;
  249.  
  250.         emsg[i] = CT_FFE_ABET[ec];
  251.     }
  252.  
  253.     return 1;
  254. }
  255.  
  256.  
  257. double
  258. ct_ffe_average(
  259.     char const* msg,
  260.     unsigned int msg_size
  261. ){
  262.     unsigned int i = 0;
  263.     unsigned int a = 0;
  264.  
  265.     for (i = 0; i < msg_size; ++i)
  266.     {
  267.         unsigned int mchar = CT_FFE_ABET_LOOKUP(msg[i]);
  268.  
  269.         a = a + mchar;
  270.     }
  271.  
  272.     return (a / (msg_size - 0.0)) / CT_FFE_ABET_MOD;
  273. }
  274.  
  275.  
  276. int
  277. ct_ffe_decrypt(
  278.     struct ct_ffe* const self,
  279.     char const* emsg,
  280.     unsigned int emsg_size,
  281.     char* dmsg,
  282.     unsigned int dmsg_size
  283. ){
  284.     unsigned int i = 0;
  285.  
  286.     if (dmsg_size < emsg_size)
  287.     {
  288.         return 0;
  289.     }
  290.  
  291.     for (i = 0; i < emsg_size; ++i)
  292.     {
  293.         unsigned int icount = self->icounts[i] % CT_FFE_ABET_MOD;
  294.         unsigned int cx = CT_FFE_ABET_LOOKUP(emsg[i]);
  295.  
  296.         int ec = cx - icount;
  297.  
  298.         if (ec < 0)
  299.         {
  300.             ec = cx + CT_FFE_ABET_MOD;
  301.             ec = abs(icount - ec);
  302.         }
  303.  
  304.         dmsg[i] = CT_FFE_ABET[ec];
  305.     }
  306.  
  307.     return 1;
  308. }
  309.  
  310.  
  311.  
  312. /* Sample FFE Usage Program
  313. ___________________________________________________________________*/
  314. int
  315. main(void)
  316. {
  317.     char omsg[] = "Christopher";
  318.     char emsg[sizeof(omsg)] = { '\0' };
  319.     char dmsg[sizeof(omsg)] = { '\0' };
  320.  
  321.     unsigned int n = sizeof(omsg) - 1;
  322.     unsigned int dim = (((unsigned int)sqrt(n)) + 1);
  323.  
  324.     double average = ct_ffe_average(omsg, n) * (3.14159 / 10.0);
  325.     double dim_cj_scale = average;
  326.     unsigned int dim_scale = 3;
  327.     unsigned int gwidth = dim * dim_scale;
  328.     unsigned int gheight = gwidth;
  329.  
  330.     struct ct_mbrotcp_pixel pixel_cj = {
  331.         -0.7 - dim_cj_scale / 2.0,
  332.         0.3 + dim_cj_scale
  333.     };
  334.  
  335.     struct ct_ffe fe = {
  336.         { -1.1, 1.0, -1.0, 1.413 },
  337.         { gwidth, gheight },
  338.         NULL
  339.     };
  340.  
  341.     if (ct_ffe_create(&fe))
  342.     {
  343.         printf(
  344.             "CT_FFE_ABET_MOD = %u\r\n"
  345.             "________________________________\r\n"
  346.             "Fractal Pad:  z^2 + c (Julia)\r\n"
  347.             "Grid Dims:    %u x %u\r\n"
  348.             "Dim JS Scale: %f\r\n"
  349.             "--------------------------------\r\n",
  350.             (unsigned int)CT_FFE_ABET_MOD,
  351.             gwidth,
  352.             gheight,
  353.             dim_cj_scale
  354.         );
  355.  
  356.         ct_ffe_encrypt_prepare(&fe, &pixel_cj, CT_FFE_ABET_MOD * dim_scale);
  357.  
  358.         printf("________________________________\r\n");
  359.  
  360.         if (ct_ffe_encrypt(&fe, omsg, n, emsg, n))
  361.         {
  362.             if (ct_ffe_decrypt(&fe, emsg, n, dmsg, n))
  363.             {
  364.                 printf("\r\noriginal  = %s\r\n\r\n", omsg);
  365.                 printf("encrypted = %s\r\n\r\n", emsg);
  366.                 printf("decrypted = %s\r\n", dmsg);
  367.             }
  368.         }
  369.  
  370.         ct_ffe_destroy(&fe);
  371.     }
  372.  
  373.     return 0;
  374. }
Advertisement
Add Comment
Please, Sign In to add comment