Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Chris M. Thomasson 8/24/2016
- # Funny Fractal Encryption (FFE) pre-alpha (0.0.0.1)
- # Reverse Iteraton Fractal Cipher (RIFC) pre-alpha (0.0.0.1)
- # with Random Offsets...
- import math;
- import random;
- import hmac;
- # Some Conversion Utilities
- #____________________________________________________________
- def ct_bytes_to_hex(origin, offset):
- hex = "";
- n = len(origin);
- t = "0123456789ABCDEF";
- for i in range(offset, n):
- c = ord(origin[i]);
- nibl = c & 0x0F;
- nibh = (c & 0xF0) >> 4;
- hex = hex + t[nibh];
- hex = hex + t[nibl];
- hex = hex + " ";
- if (not ((i + 1) % 16)): hex = hex + "\r\n";
- return hex;
- def ct_hex_to_bytes(origin, offset):
- bytes = "";
- n = len(origin);
- t = "0123456789ABCDEF";
- i = offset + 1;
- while (i < n):
- nibh = 0;
- nibl = 0;
- try:
- nibh = t.index(origin[i - 1]);
- nibl = t.index(origin[i]);
- except ValueError:
- i = i + 1;
- continue;
- if (nibh > -1 and nibl > -1):
- c = nibh * 16 + nibl;
- bytes = bytes + chr(c);
- i = i + 2;
- return bytes;
- # Some Math Utilities
- #____________________________________________________________
- # Cantor pair
- def ct_cpair(n):
- return int(((n[0] + n[1]) * ((n[0] + n[1]) + 1)) / 2) + n[1];
- def ct_cpairi(n):
- n0 = 8 * n + 1;
- x = int((math.sqrt(n0) - 1) / 2);
- y = n - int(((x + 1) * x) / 2);
- return (x - 1, y);
- # Complex Absolute Value
- def ct_cabs(z):
- return math.sqrt(z.real**2 + z.imag**2);
- # Complex Argument
- def ct_carg(z):
- return math.atan2(z.imag, z.real);
- # n from pow
- def ct_npow(p):
- return int(math.ceil(math.fabs(p)));
- # Complex Roots
- def ct_croots(z, p):
- l = ct_cabs(z);
- s = l**(1.0 / p);
- a = ct_carg(z) / p;
- n = ct_npow(p);
- astep = (math.pi * 2.0) / p;
- result = [];
- for i in range(n):
- r = complex(math.cos(a + astep * i) * s,
- math.sin(a + astep * i) * s);
- result.append(r);
- return result;
- # Find Root
- def ct_froot(z, r):
- n = 0;
- for i in r:
- d = z - i;
- l = math.sqrt(d.real**2 + d.imag**2);
- if l < 0.000001: return n;
- n = n + 1;
- return -1;
- def ct_range_number(n):
- while (n > 10000000000):
- n /= 1723.13327;
- return n;
- # Reverse Iteration Fractal Cipher
- #____________________________________________________________
- def ct_rifc_encrypt_nits(z, c, p, nits):
- t = "0123456789ABCDEF";
- for nit in nits:
- r = ct_croots(z - c, p);
- z = r[t.index(nit)];
- return z;
- def ct_rifc_decrypt_nits(z, c, p, n):
- t = "0123456789ABCDEF";
- nits = "";
- for i in range(n):
- f = z**p + c;
- r = ct_croots(f - c, p);
- nit = ct_froot(z, r);
- nits = nits + t[nit];
- z = f;
- nits = nits[::-1];
- return (nits, z);
- # Funny Fractal Encryption, low-level
- #____________________________________________________________
- def ct_ffe_obtain_number_fwd(n):
- rn = math.modf(ct_range_number(math.fabs(n)));
- v0 = rn[0] * 10000000000;
- rn = math.modf(v0);
- v1 = rn[0] * 10000000000;
- v2 = int(math.fabs(math.floor(v1)));
- return v2;
- def ct_ffe_gen_rand_axes(dims):
- d = dims[1] - dims[0];
- return (-random.random() * d, random.random() * d,
- -random.random() * d, random.random() * d);
- def ct_ffe_axes_combine(a0, a1):
- return (-(math.fabs(a0[0] * a1[0] * 7.13263) % 3.0),
- (math.fabs(a0[1] * a1[1] * 7.13263) % 3.0),
- -(math.fabs(a0[2] * a1[2] * 7.13263) % 3.0),
- (math.fabs(a0[3] * a1[3] * 7.13263) % 3.0));
- def ct_ffe_gen_rand_bytes(n):
- rtxt = "";
- for i in range(n):
- rtxt = rtxt + chr(random.randint(0, 255));
- return rtxt;
- # Forward Iterate
- def ct_ffe_fwd_iterate(z, c, p, s, e, n):
- for i in range(n):
- if (z.real == 0 and z.imag == 0):
- z += (0.00000001+0.0000000013);
- f = i % 2;
- z = z**p[f] + c[f];
- z = z * s;
- d = ct_cabs(z);
- if (d > e):
- return (z, i);
- return (z, n);
- def ct_ffe_forward(z, c, p, s, e, n, m):
- fwd = ct_ffe_fwd_iterate(z, c, p, s, e, n);
- fwd_mod_0 = ct_ffe_obtain_number_fwd(fwd[0].real * (fwd[1] + 1));
- fwd_mod_1 = ct_ffe_obtain_number_fwd(fwd[0].imag * (fwd[1] + 1));
- fwd_mod = ct_cpair((fwd_mod_0, fwd_mod_1)) * ((fwd_mod_0 + fwd_mod_1) % 65536);
- return fwd_mod % m;
- def ct_ffe_cipher(a, c, p, s, e, n, ptxt, encrypt):
- ctxt = "";
- n = len(ptxt);
- dims = math.ceil(math.sqrt(n));
- pn = int(math.ceil(math.fabs(p[0])));
- xstep = (a[1] - a[0]) / dims;
- ystep = (a[3] - a[2]) / dims;
- for i in range(n):
- x = i % dims;
- y = int(i / dims);
- z = complex(a[0] + x * xstep, a[3] - y * ystep);
- cmod = ct_ffe_forward(z, c, p, s, e, n, 256);
- pchr = ord(ptxt[i]);
- if (encrypt):
- cchr = (pchr + cmod) % 256;
- else:
- cchr = pchr - cmod;
- if (cchr < 0):
- cchr = int(math.fabs((pchr + 256) - cmod));
- ctxt = ctxt + chr(cchr);
- return ctxt;
- # FFE Random Encryption
- #____________________________________________________________
- def ct_ffe_rand_gen_bitmap(a, c, p, s, e, n, cn):
- bmap = "";
- pcn = cn / 2;
- dims = math.ceil(math.sqrt(cn));
- xstep = (a[1] - a[0]) / dims;
- ystep = (a[3] - a[2]) / dims;
- ir = 0;
- ic = 0;
- i = 0;
- while (ir < pcn or ic < pcn):
- x = i % dims;
- y = int(i / dims);
- z = complex(a[0] + x * xstep, a[3] - y * ystep);
- cmod = ct_ffe_forward(z, c, p, s, e, n, 1037);
- cbit = cmod & 1;
- if ((cbit and ic < pcn) or ir == pcn):
- if (ic < pcn):
- bmap = bmap + "1";
- ic = ic + 1;
- elif (ir < pcn or ic == pcn):
- if (ir < pcn):
- bmap = bmap + "0";
- ir = ir + 1;
- i = i + 1;
- if (ir != pcn or ic != pcn):
- print("*********** BITMAP ALERT ***********");
- #print("ir:%s, ic:%s" % (ir, ic));
- return bmap;
- def ct_ffe_bitmap_display(bmap):
- dtxt = "";
- n = len(bmap);
- for i in range(n):
- dtxt = dtxt + bmap[i];
- if (not ((i + 1) % 48)):
- dtxt = dtxt + "\n";
- print(dtxt);
- def ct_ffe_rand_encrypt(a, c, p, s, e, n, ptxt):
- n = len(ptxt);
- cn = n * 2;
- rtxt = ct_ffe_gen_rand_bytes(n);
- rctxt = "";
- bmap = ct_ffe_rand_gen_bitmap(a, c, p, s, e, n, cn);
- print("\n\n\nEncrypt Fractal Bitmap, len:%s" % (len(bmap)));
- print("________________________________________");
- ct_ffe_bitmap_display(bmap);
- for i in range(n):
- pchr = ord(ptxt[i]);
- cmod = ord(rtxt[i]);
- cchr = (pchr + cmod) % 256;
- rctxt = rctxt + chr(cchr);
- ctxt = "";
- ic = 0;
- ir = 0;
- for i in range(cn):
- if (int(bmap[i])):
- ctxt = ctxt + rctxt[ic];
- ic = ic + 1;
- else:
- ctxt = ctxt + rtxt[ir];
- ir = ir + 1;
- return ctxt;
- def ct_ffe_rand_decrypt(a, c, p, s, e, n, ctxt):
- cn = len(ctxt);
- n = int(cn / 2);
- rctxt = "";
- rtxt = "";
- bmap = ct_ffe_rand_gen_bitmap(a, c, p, s, e, n, cn);
- print("\n\n\nDecrypt Fractal Bitmap, len:%s" % (len(bmap)));
- print("________________________________________");
- ct_ffe_bitmap_display(bmap);
- for i in range(cn):
- if (int(bmap[i])):
- rctxt = rctxt + ctxt[i];
- else:
- rtxt = rtxt + ctxt[i];
- ptxt = "";
- for i in range(n):
- pchr = ord(rctxt[i]);
- cmod = ord(rtxt[i]);
- cchr = pchr - cmod;
- if (cchr < 0):
- cchr = int(math.fabs((pchr + 256) - cmod));
- ptxt = ptxt + chr(cchr);
- return ptxt;
- # Funny Fractal Encryption, high-level
- #____________________________________________________________
- def ct_ffe_encrypt(axes, adims, hmack, c, p, s, e, n, ptxt):
- ffe_raxes = ct_ffe_gen_rand_axes(adims);
- ffe_axes = ct_ffe_axes_combine(axes, ffe_raxes);
- ffe_rnd_axes = ct_ffe_axes_combine(axes, ffe_axes);
- #print("AXES:%s" % (str(ffe_axes)));
- rand_ctxt = ct_ffe_rand_encrypt(ffe_rnd_axes, c, p, s, e, n + 13, ptxt);
- ffe_ctxt = ct_ffe_cipher(ffe_axes, c, p, s, e, n, rand_ctxt, True);
- ffe_hmac = hmac.new(hmack.encode());
- for i in ffe_ctxt:
- ffe_hmac.update(str(i).encode());
- ffe_hmac_hex = ffe_hmac.hexdigest();
- return [ffe_raxes, ffe_hmac_hex, ffe_ctxt];
- def ct_ffe_decrypt(axes, hmack, c, p, s, e, n, ctxt):
- ffe_hmac = hmac.new(hmack.encode());
- for i in ctxt[2]:
- ffe_hmac.update(str(i).encode());
- ffe_hmac_hex = ffe_hmac.hexdigest();
- if (not hmac.compare_digest(ctxt[1], ffe_hmac_hex)):
- print("\n\n************** HMAC VIOLATION ***************\n\n");
- return "Bob is 0xDEADBEEF!";
- ffe_axes = ct_ffe_axes_combine(axes, ctxt[0]);
- ffe_rnd_axes = ct_ffe_axes_combine(axes, ffe_axes);
- ffe_dtxt = ct_ffe_cipher(ffe_axes, c, p, s, e, n, ctxt[2], False);
- ptxt = ct_ffe_rand_decrypt(ffe_rnd_axes, c, p, s, e, n + 13, ffe_dtxt);
- return ptxt;
- # The Main Program
- #____________________________________________________________
- print("Funny Fractal Encryption, pre-alpha 0.0.0.1");
- print("by Chris M. Thomasson");
- print("==========================================================\n\n");
- # The Secret Key
- axes = (-0.75, .09, -.5, .3);
- adims = (-3.25, 3.25);
- hmack = "hmac_secret_key";
- c = ((-.75+.09j), (.0 + 1j), (-.4+.5j));
- p = (2.13, 3.14, 4);
- s = 1.0;
- e = 2.0;
- n = 103;
- # The Plaintext
- ptxt = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
- #ptxt = "Hello there, welcome to Funny Fractal Encryption!\nBy: Chris M. Thomasson, :^)";
- print("\nOriginal Plaintext, len:%s" % (len(ptxt)));
- print("________________________________________");
- print("%s" % (ptxt));
- print("----------------------------------------");
- print("%s" % (ct_bytes_to_hex(ptxt, 0)));
- # Encryption
- ctxt = ct_ffe_encrypt(axes, adims, hmack, c, p, s, e, n, ptxt);
- print("\n\n\nThe Ciphertext:");
- print("________________________________________");
- print("(%s, %s)\n(%s, %s)" % (ctxt[0][0], ctxt[0][1], ctxt[0][2], ctxt[0][3]));
- print("----------------------------------------");
- print("%s" % (ctxt[1]));
- print("----------------------------------------");
- print("%s" % (ct_bytes_to_hex(ctxt[2], 0)));
- # Decryption
- dtxt = ct_ffe_decrypt(axes, hmack, c, p, s, e, n, ctxt);
- print("\n\nDecrypted Plaintext, len:%s" % (len(dtxt)));
- print("________________________________________");
- try:
- print("%s" % (dtxt));
- except:
- print("<print error>");
- print("----------------------------------------");
- print("%s" % (ct_bytes_to_hex(dtxt, 0)));
- if (ptxt != dtxt):
- print("Data Corrupted!");
- #eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement