Advertisement
Chris_M_Thomasson

Funny Fractal Encryption ver:0.0.2

Jul 10th, 2015
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.25 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.     /* point did NOT escape! */
  112.     {
  113.         unsigned int mc = 0;
  114.  
  115.         ret = (unsigned int)(234777 * fabs(sin(i * cmin * 3233)));
  116.         mc = ret % CT_MBROT_ABET_MOD;
  117.  
  118.         putchar(CT_MBROT_ABET[mc]);
  119.     }
  120.  
  121.     return ret;
  122. }
  123.  
  124.  
  125. void
  126. ct_mbrotplane(
  127.     struct ct_mbrotcp_plane const* cp_plane,
  128.     struct ct_mbrotvp_plane const* vp_plane,
  129.     struct ct_mbrotcp_pixel const* pixel_cj,
  130.     unsigned int const imax,
  131.     unsigned int* icounts
  132. ){
  133.     unsigned int vpyi = 0;
  134.  
  135.     struct ct_mbrotcp_pixel cp_pixel = { 0.0, 0.0 };
  136.  
  137.     double const cp_width = cp_plane->xmax - cp_plane->xmin;
  138.     double const cp_height = cp_plane->ymax - cp_plane->ymin;
  139.  
  140.     double const xstep = cp_width / vp_plane->width;
  141.     double const ystep = cp_height / vp_plane->height;
  142.  
  143.     for (vpyi = 0; vpyi < vp_plane->height; ++vpyi)
  144.     {
  145.         unsigned int vpxi = 0;
  146.  
  147.         cp_pixel.y = cp_plane->ymax - vpyi * ystep;
  148.  
  149.         for (vpxi = 0; vpxi < vp_plane->width; ++vpxi)
  150.         {
  151.             cp_pixel.x = cp_plane->xmin + vpxi * xstep;
  152.  
  153.             unsigned int icount = ct_mbrotpixel(&cp_pixel, pixel_cj, imax);
  154.  
  155.             if (icounts)
  156.             {
  157.                 unsigned int i = (vpyi * vp_plane->width) + vpxi;
  158.  
  159.                 icounts[i] = icount;
  160.             }
  161.         }
  162.  
  163.         putchar('\n');
  164.     }
  165. }
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173. /* Funny Fractal Encryption
  174. ___________________________________________________________________*/
  175.  
  176. #define CT_FFE_ABET         CT_MBROT_ABET
  177. #define CT_FFE_ABET_SCALE   CT_MBROT_ABET_SCALE
  178. #define CT_FFE_ABET_MOD     CT_MBROT_ABET_MOD
  179. #define CT_FFE_ABET_LOOKUP  CT_MBROT_ABET_LOOKUP
  180.  
  181.  
  182.  
  183. struct ct_ffe
  184. {
  185.     struct ct_mbrotcp_plane cp_plane;
  186.     struct ct_mbrotvp_plane vp_plane;
  187.     unsigned int* icounts;
  188. };
  189.  
  190.  
  191.  
  192. int
  193. ct_ffe_create(
  194.     struct ct_ffe* const self
  195. ){
  196.     unsigned int n = self->vp_plane.width * self->vp_plane.height;
  197.  
  198.     self->icounts = calloc(n, sizeof(*self->icounts));
  199.  
  200.     if (self->icounts)
  201.     {
  202.         return 1;
  203.     }
  204.  
  205.     return 0;
  206. }
  207.  
  208.  
  209. void
  210. ct_ffe_destroy(
  211.     struct ct_ffe* const self
  212. ){
  213.     free(self->icounts);
  214. }
  215.  
  216.  
  217. void
  218. ct_ffe_encrypt_prepare(
  219.     struct ct_ffe* const self,
  220.     struct ct_mbrotcp_pixel const* pixel_cj,
  221.     unsigned int const imax
  222. ){
  223.     ct_mbrotplane(
  224.         &self->cp_plane,
  225.         &self->vp_plane,
  226.         pixel_cj,
  227.         imax,
  228.         self->icounts
  229.     );
  230. }
  231.  
  232.  
  233. int
  234. ct_ffe_encrypt(
  235.     struct ct_ffe* const self,
  236.     char const* omsg,
  237.     unsigned int omsg_size,
  238.     char* emsg,
  239.     unsigned int emsg_size
  240. ){
  241.     unsigned int i = 0;
  242.  
  243.     if (emsg_size < omsg_size)
  244.     {
  245.         return 0;
  246.     }
  247.  
  248.     for (i = 0; i < omsg_size; ++i)
  249.     {
  250.         unsigned int icount = self->icounts[i] % CT_FFE_ABET_MOD;
  251.         unsigned int cx = CT_FFE_ABET_LOOKUP(omsg[i]);
  252.         unsigned int ec = (cx + icount) % CT_FFE_ABET_MOD;
  253.  
  254.         emsg[i] = CT_FFE_ABET[ec];
  255.     }
  256.  
  257.     return 1;
  258. }
  259.  
  260.  
  261. double
  262. ct_ffe_average(
  263.     char const* msg,
  264.     unsigned int msg_size
  265. ){
  266.     unsigned int i = 0;
  267.     unsigned int a = 0;
  268.  
  269.     for (i = 0; i < msg_size; ++i)
  270.     {
  271.         unsigned int mchar = CT_FFE_ABET_LOOKUP(msg[i]);
  272.  
  273.         a = a + mchar;
  274.     }
  275.  
  276.     return (a / (msg_size - 0.0)) / CT_FFE_ABET_MOD;
  277. }
  278.  
  279.  
  280. int
  281. ct_ffe_decrypt(
  282.     struct ct_ffe* const self,
  283.     char const* emsg,
  284.     unsigned int emsg_size,
  285.     char* dmsg,
  286.     unsigned int dmsg_size
  287. ){
  288.     unsigned int i = 0;
  289.  
  290.     if (dmsg_size < emsg_size)
  291.     {
  292.         return 0;
  293.     }
  294.  
  295.     for (i = 0; i < emsg_size; ++i)
  296.     {
  297.         unsigned int icount = self->icounts[i] % CT_FFE_ABET_MOD;
  298.         unsigned int cx = CT_FFE_ABET_LOOKUP(emsg[i]);
  299.  
  300.         int ec = cx - icount;
  301.  
  302.         if (ec < 0)
  303.         {
  304.             ec = cx + CT_FFE_ABET_MOD;
  305.             ec = abs(icount - ec);
  306.         }
  307.  
  308.         dmsg[i] = CT_FFE_ABET[ec];
  309.     }
  310.  
  311.     return 1;
  312. }
  313.  
  314.  
  315. unsigned long
  316. cantor_packing(
  317.     unsigned int n0,
  318.     unsigned int n1
  319. ){
  320.     unsigned long ret = 0.5 * (n0 + n1) * (n0 + n1 + 1) + n1;
  321.  
  322.     return ret;
  323. }
  324.  
  325.  
  326. double
  327. ct_ffe_gen_private_key(
  328.     char const* msg,
  329.     unsigned int msg_size
  330. ){
  331.     unsigned int i = 0;
  332.     double avg = ct_ffe_average(msg, msg_size);
  333.     double ret = 0.0;
  334.  
  335.     unsigned int long cpack = 0;
  336.  
  337.     unsigned int cpack_max_0 =
  338.         cantor_packing(CT_FFE_ABET_MOD, CT_FFE_ABET_MOD);
  339.  
  340.     unsigned int cpack_max =
  341.         cantor_packing(cpack_max_0, CT_FFE_ABET_MOD);
  342.  
  343.     unsigned int XOR = 0;
  344.  
  345.     for (i = 0; i < msg_size - 1; ++i)
  346.     {
  347.         unsigned int mchar0 = CT_FFE_ABET_LOOKUP(msg[i]);
  348.         unsigned int mchar1 = CT_FFE_ABET_LOOKUP(msg[i + 1]);
  349.  
  350.         unsigned long char_cpack = cantor_packing(mchar0, mchar1);
  351.  
  352.         XOR = (XOR ^ (mchar0 + mchar1)) ^ char_cpack;
  353.  
  354.         cpack = (cpack + cantor_packing(cpack, char_cpack)) % cpack_max;
  355.     }
  356.  
  357.     printf("XOR = %u\r\n", XOR);
  358.     printf("cpack = %u\r\n", cpack);
  359.  
  360.     ret = (cpack + XOR) / (cpack_max - 0.0);
  361.  
  362.     return fmod(ret + avg, 1.0);
  363. }
  364.  
  365.  
  366.  
  367. /* Sample FFE Usage Program
  368. ___________________________________________________________________*/
  369. int
  370. main(void)
  371. {  
  372.     #define S "Chris Thomasson, "
  373.     char omsg[] = "Chris Thomasson, " S S S S S S S S S S S S S S S S S S S S S S;
  374.     char emsg[sizeof(omsg)] = { '\0' };
  375.     char dmsg[sizeof(omsg)] = { '\0' };
  376.  
  377.     unsigned int n = sizeof(omsg) - 1;
  378.     unsigned int dim = (((unsigned int)sqrt(n)) + 1);
  379.  
  380.     double average = ct_ffe_average(omsg, n);
  381.     double dim_cj_scale = average * (3.14159 / 10.0);
  382.     unsigned int dim_scale = 3;
  383.     unsigned int gwidth = dim * dim_scale;
  384.     unsigned int gheight = gwidth;
  385.  
  386.     dim_cj_scale = ct_ffe_gen_private_key(omsg, n) * (3.14159 / 10.0);
  387.  
  388.     struct ct_mbrotcp_pixel pixel_cj = {
  389.         -0.7 - dim_cj_scale / 2.0,
  390.         0.3 + dim_cj_scale
  391.     };
  392.  
  393.     struct ct_ffe fe = {
  394.         { -1.1 + dim_cj_scale, 1.0 - dim_cj_scale / 2.0,
  395.           -1.0 - dim_cj_scale / 2.0, 1.413 + dim_cj_scale / 2.0 },
  396.         { gwidth, gheight },
  397.         NULL
  398.     };
  399.  
  400.     if (ct_ffe_create(&fe))
  401.     {
  402.         printf(
  403.             "CT_FFE_ABET_MOD = %u\r\n"
  404.             "________________________________\r\n"
  405.             "Fractal Pad:  z^2 + c (Julia)\r\n"
  406.             "Grid Dims:    %u x %u\r\n"
  407.             "Dim JS Scale: %f\r\n"
  408.             "--------------------------------\r\n",
  409.             (unsigned int)CT_FFE_ABET_MOD,
  410.             gwidth,
  411.             gheight,
  412.             dim_cj_scale
  413.         );
  414.  
  415.         ct_ffe_encrypt_prepare(&fe, &pixel_cj, CT_FFE_ABET_MOD * dim_scale);
  416.  
  417.         printf("________________________________\r\n");
  418.  
  419.         if (ct_ffe_encrypt(&fe, omsg, n, emsg, n))
  420.         {
  421.             if (ct_ffe_decrypt(&fe, emsg, n, dmsg, n))
  422.             {
  423.                 printf("\r\noriginal  = %s\r\n\r\n", omsg);
  424.                 printf("encrypted = %s\r\n\r\n", emsg);
  425.                 printf("decrypted = %s\r\n", dmsg);
  426.             }
  427.         }
  428.  
  429.         ct_ffe_destroy(&fe);
  430.     }
  431.  
  432.  
  433.     return 0;
  434. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement