Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Mandelbrot Set ASCII by Chris M. Thomasson :^)
- ___________________________________________________________________*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <string.h>
- #define CT_MIN(mp_0, mp_1) (((mp_0) < (mp_1)) ? (mp_0) : (mp_1))
- #define CT_SQ(mp_n) ((mp_n) * (mp_n))
- #define CT_MBROT_ABET_PUNC \
- " ,./\\;'[]-=<>?:""{}_+`~"
- #define CT_MBROT_ABET_NUM_SHIFT \
- "!@#$%^&*()"
- #define CT_MBROT_ABET_NUM \
- "0123456789"
- #define CT_MBROT_ABET_UPPER \
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- #define CT_MBROT_ABET_LOWER \
- "abcdefghijklmnopqrstuvwxyz"
- #define CT_MBROT_ABET \
- CT_MBROT_ABET_UPPER \
- CT_MBROT_ABET_LOWER \
- CT_MBROT_ABET_NUM \
- CT_MBROT_ABET_NUM_SHIFT \
- CT_MBROT_ABET_PUNC
- #define CT_MBROT_ABET_MOD (sizeof(CT_MBROT_ABET) - 1U)
- #define CT_MBROT_ABET_LOOKUP(mp_char) \
- ((strchr(CT_MBROT_ABET, (mp_char))) - CT_MBROT_ABET)
- #define CT_MBROT_ABET_SCALE(mp_idx) \
- ((mp_idx) / (CT_MBROT_ABET_MOD - 1.0))
- struct ct_mbrotcp_pixel
- {
- double x;
- double y;
- };
- struct ct_mbrotcp_plane
- {
- double xmin;
- double xmax;
- double ymin;
- double ymax;
- };
- struct ct_mbrotvp_plane
- {
- unsigned int width;
- unsigned int height;
- };
- unsigned int
- ct_mbrotpixel(
- struct ct_mbrotcp_pixel const* pixel_c,
- struct ct_mbrotcp_pixel const* pixel_cj,
- unsigned int const imax
- ){
- unsigned int i = 0;
- unsigned int ret = 0;
- struct ct_mbrotcp_pixel pixel_z = *pixel_c;
- struct ct_mbrotcp_pixel pixel_z_sq = {
- CT_SQ(pixel_z.x), CT_SQ(pixel_z.y)
- };
- //struct ct_mbrotcp_pixel pixel_cj = { -0.7, 0.3 };
- double cmin = 9999999999999.0;
- for (i = 0; i < imax; ++i)
- {
- pixel_z.y = pixel_z.x * pixel_z.y * 2.0 + pixel_cj->y;
- pixel_z.x = pixel_z_sq.x - pixel_z_sq.y + pixel_cj->x;
- pixel_z_sq.x = CT_SQ(pixel_z.x);
- pixel_z_sq.y = CT_SQ(pixel_z.y);
- cmin = CT_MIN(cmin, CT_SQ(pixel_z_sq.x) + CT_SQ(pixel_z_sq.y));
- if (pixel_z_sq.x + pixel_z_sq.y > 2357)
- {
- ret = (unsigned int)(113747 * fabs(cos(i * cmin * 573)));
- putchar(CT_MBROT_ABET[ret % CT_MBROT_ABET_MOD]);
- return ret;
- }
- }
- /* point did NOT escape! */
- {
- unsigned int mc = 0;
- ret = (unsigned int)(234777 * fabs(sin(i * cmin * 3233)));
- mc = ret % CT_MBROT_ABET_MOD;
- putchar(CT_MBROT_ABET[mc]);
- }
- return ret;
- }
- void
- ct_mbrotplane(
- struct ct_mbrotcp_plane const* cp_plane,
- struct ct_mbrotvp_plane const* vp_plane,
- struct ct_mbrotcp_pixel const* pixel_cj,
- unsigned int const imax,
- unsigned int* icounts
- ){
- unsigned int vpyi = 0;
- struct ct_mbrotcp_pixel cp_pixel = { 0.0, 0.0 };
- double const cp_width = cp_plane->xmax - cp_plane->xmin;
- double const cp_height = cp_plane->ymax - cp_plane->ymin;
- double const xstep = cp_width / vp_plane->width;
- double const ystep = cp_height / vp_plane->height;
- for (vpyi = 0; vpyi < vp_plane->height; ++vpyi)
- {
- unsigned int vpxi = 0;
- cp_pixel.y = cp_plane->ymax - vpyi * ystep;
- for (vpxi = 0; vpxi < vp_plane->width; ++vpxi)
- {
- cp_pixel.x = cp_plane->xmin + vpxi * xstep;
- unsigned int icount = ct_mbrotpixel(&cp_pixel, pixel_cj, imax);
- if (icounts)
- {
- unsigned int i = (vpyi * vp_plane->width) + vpxi;
- icounts[i] = icount;
- }
- }
- putchar('\n');
- }
- }
- /* Funny Fractal Encryption
- ___________________________________________________________________*/
- #define CT_FFE_ABET CT_MBROT_ABET
- #define CT_FFE_ABET_SCALE CT_MBROT_ABET_SCALE
- #define CT_FFE_ABET_MOD CT_MBROT_ABET_MOD
- #define CT_FFE_ABET_LOOKUP CT_MBROT_ABET_LOOKUP
- struct ct_ffe
- {
- struct ct_mbrotcp_plane cp_plane;
- struct ct_mbrotvp_plane vp_plane;
- unsigned int* icounts;
- };
- int
- ct_ffe_create(
- struct ct_ffe* const self
- ){
- unsigned int n = self->vp_plane.width * self->vp_plane.height;
- self->icounts = calloc(n, sizeof(*self->icounts));
- if (self->icounts)
- {
- return 1;
- }
- return 0;
- }
- void
- ct_ffe_destroy(
- struct ct_ffe* const self
- ){
- free(self->icounts);
- }
- void
- ct_ffe_encrypt_prepare(
- struct ct_ffe* const self,
- struct ct_mbrotcp_pixel const* pixel_cj,
- unsigned int const imax
- ){
- ct_mbrotplane(
- &self->cp_plane,
- &self->vp_plane,
- pixel_cj,
- imax,
- self->icounts
- );
- }
- int
- ct_ffe_encrypt(
- struct ct_ffe* const self,
- char const* omsg,
- unsigned int omsg_size,
- char* emsg,
- unsigned int emsg_size
- ){
- unsigned int i = 0;
- if (emsg_size < omsg_size)
- {
- return 0;
- }
- for (i = 0; i < omsg_size; ++i)
- {
- unsigned int icount = self->icounts[i] % CT_FFE_ABET_MOD;
- unsigned int cx = CT_FFE_ABET_LOOKUP(omsg[i]);
- unsigned int ec = (cx + icount) % CT_FFE_ABET_MOD;
- emsg[i] = CT_FFE_ABET[ec];
- }
- return 1;
- }
- double
- ct_ffe_average(
- char const* msg,
- unsigned int msg_size
- ){
- unsigned int i = 0;
- unsigned int a = 0;
- for (i = 0; i < msg_size; ++i)
- {
- unsigned int mchar = CT_FFE_ABET_LOOKUP(msg[i]);
- a = a + mchar;
- }
- return (a / (msg_size - 0.0)) / CT_FFE_ABET_MOD;
- }
- int
- ct_ffe_decrypt(
- struct ct_ffe* const self,
- char const* emsg,
- unsigned int emsg_size,
- char* dmsg,
- unsigned int dmsg_size
- ){
- unsigned int i = 0;
- if (dmsg_size < emsg_size)
- {
- return 0;
- }
- for (i = 0; i < emsg_size; ++i)
- {
- unsigned int icount = self->icounts[i] % CT_FFE_ABET_MOD;
- unsigned int cx = CT_FFE_ABET_LOOKUP(emsg[i]);
- int ec = cx - icount;
- if (ec < 0)
- {
- ec = cx + CT_FFE_ABET_MOD;
- ec = abs(icount - ec);
- }
- dmsg[i] = CT_FFE_ABET[ec];
- }
- return 1;
- }
- unsigned long
- cantor_packing(
- unsigned int n0,
- unsigned int n1
- ){
- unsigned long ret = 0.5 * (n0 + n1) * (n0 + n1 + 1) + n1;
- return ret;
- }
- double
- ct_ffe_gen_private_key(
- char const* msg,
- unsigned int msg_size
- ){
- unsigned int i = 0;
- double avg = ct_ffe_average(msg, msg_size);
- double ret = 0.0;
- unsigned int long cpack = 0;
- unsigned int cpack_max_0 =
- cantor_packing(CT_FFE_ABET_MOD, CT_FFE_ABET_MOD);
- unsigned int cpack_max =
- cantor_packing(cpack_max_0, CT_FFE_ABET_MOD);
- unsigned int XOR = 0;
- for (i = 0; i < msg_size - 1; ++i)
- {
- unsigned int mchar0 = CT_FFE_ABET_LOOKUP(msg[i]);
- unsigned int mchar1 = CT_FFE_ABET_LOOKUP(msg[i + 1]);
- unsigned long char_cpack = cantor_packing(mchar0, mchar1);
- XOR = (XOR ^ (mchar0 + mchar1)) ^ char_cpack;
- cpack = (cpack + cantor_packing(cpack, char_cpack)) % cpack_max;
- }
- printf("XOR = %u\r\n", XOR);
- printf("cpack = %u\r\n", cpack);
- ret = (cpack + XOR) / (cpack_max - 0.0);
- return fmod(ret + avg, 1.0);
- }
- /* Sample FFE Usage Program
- ___________________________________________________________________*/
- int
- main(void)
- {
- #define S "Chris Thomasson, "
- 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;
- char emsg[sizeof(omsg)] = { '\0' };
- char dmsg[sizeof(omsg)] = { '\0' };
- unsigned int n = sizeof(omsg) - 1;
- unsigned int dim = (((unsigned int)sqrt(n)) + 1);
- double average = ct_ffe_average(omsg, n);
- double dim_cj_scale = average * (3.14159 / 10.0);
- unsigned int dim_scale = 3;
- unsigned int gwidth = dim * dim_scale;
- unsigned int gheight = gwidth;
- dim_cj_scale = ct_ffe_gen_private_key(omsg, n) * (3.14159 / 10.0);
- struct ct_mbrotcp_pixel pixel_cj = {
- -0.7 - dim_cj_scale / 2.0,
- 0.3 + dim_cj_scale
- };
- struct ct_ffe fe = {
- { -1.1 + dim_cj_scale, 1.0 - dim_cj_scale / 2.0,
- -1.0 - dim_cj_scale / 2.0, 1.413 + dim_cj_scale / 2.0 },
- { gwidth, gheight },
- NULL
- };
- if (ct_ffe_create(&fe))
- {
- printf(
- "CT_FFE_ABET_MOD = %u\r\n"
- "________________________________\r\n"
- "Fractal Pad: z^2 + c (Julia)\r\n"
- "Grid Dims: %u x %u\r\n"
- "Dim JS Scale: %f\r\n"
- "--------------------------------\r\n",
- (unsigned int)CT_FFE_ABET_MOD,
- gwidth,
- gheight,
- dim_cj_scale
- );
- ct_ffe_encrypt_prepare(&fe, &pixel_cj, CT_FFE_ABET_MOD * dim_scale);
- printf("________________________________\r\n");
- if (ct_ffe_encrypt(&fe, omsg, n, emsg, n))
- {
- if (ct_ffe_decrypt(&fe, emsg, n, dmsg, n))
- {
- printf("\r\noriginal = %s\r\n\r\n", omsg);
- printf("encrypted = %s\r\n\r\n", emsg);
- printf("decrypted = %s\r\n", dmsg);
- }
- }
- ct_ffe_destroy(&fe);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement