Advertisement
Chris_M_Thomasson

Funny Fractal Encryption in Python, v:0.0.0.1

Aug 24th, 2016
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.06 KB | None | 0 0
  1. # Chris M. Thomasson 8/24/2016
  2. # Funny Fractal Encryption (FFE) pre-alpha (0.0.0.1)
  3. # Reverse Iteraton Fractal Cipher (RIFC) pre-alpha (0.0.0.1)
  4. # with Random Offsets...
  5.  
  6. import math;
  7. import random;
  8. import hmac;
  9.  
  10.  
  11. # Some Conversion Utilities
  12. #____________________________________________________________
  13. def ct_bytes_to_hex(origin, offset):
  14.     hex = "";
  15.     n = len(origin);
  16.     t = "0123456789ABCDEF";
  17.  
  18.     for i in range(offset, n):
  19.         c = ord(origin[i]);
  20.  
  21.         nibl = c & 0x0F;
  22.         nibh = (c & 0xF0) >> 4;
  23.  
  24.         hex = hex + t[nibh];
  25.         hex = hex + t[nibl];
  26.  
  27.         hex = hex + " ";
  28.  
  29.         if (not ((i + 1) % 16)): hex = hex + "\r\n";
  30.  
  31.     return hex;
  32.  
  33.  
  34. def ct_hex_to_bytes(origin, offset):
  35.     bytes = "";
  36.     n = len(origin);
  37.     t = "0123456789ABCDEF";
  38.  
  39.     i = offset + 1;
  40.     while (i < n):
  41.  
  42.         nibh = 0;
  43.         nibl = 0;
  44.  
  45.         try:
  46.             nibh = t.index(origin[i - 1]);
  47.             nibl = t.index(origin[i]);
  48.         except ValueError:
  49.             i = i + 1;
  50.             continue;
  51.  
  52.         if (nibh > -1 and nibl > -1):
  53.             c = nibh * 16 + nibl;
  54.             bytes = bytes + chr(c);
  55.  
  56.         i = i + 2;
  57.  
  58.     return bytes;
  59.  
  60.  
  61.  
  62. # Some Math Utilities
  63. #____________________________________________________________
  64.  
  65. # Cantor pair
  66. def ct_cpair(n):
  67.     return int(((n[0] + n[1]) * ((n[0] + n[1]) + 1)) / 2) + n[1];
  68.  
  69. def ct_cpairi(n):
  70.     n0 = 8 * n + 1;
  71.     x = int((math.sqrt(n0) - 1) / 2);
  72.     y = n - int(((x + 1) * x) / 2);
  73.     return (x - 1, y);
  74.  
  75. # Complex Absolute Value
  76. def ct_cabs(z):
  77.     return math.sqrt(z.real**2 + z.imag**2);
  78.  
  79. # Complex Argument
  80. def ct_carg(z):
  81.     return math.atan2(z.imag, z.real);
  82.  
  83. # n from pow
  84. def ct_npow(p):
  85.     return int(math.ceil(math.fabs(p)));
  86.  
  87. # Complex Roots
  88. def ct_croots(z, p):
  89.     l = ct_cabs(z);
  90.     s = l**(1.0 / p);
  91.     a = ct_carg(z) / p;
  92.     n = ct_npow(p);
  93.     astep = (math.pi * 2.0) / p;
  94.     result = [];
  95.  
  96.     for i in range(n):
  97.         r = complex(math.cos(a + astep * i) * s,
  98.                     math.sin(a + astep * i) * s);
  99.         result.append(r);
  100.  
  101.     return result;
  102.  
  103.  
  104. # Find Root
  105. def ct_froot(z, r):
  106.     n = 0;
  107.     for i in r:
  108.         d = z - i;
  109.         l = math.sqrt(d.real**2 + d.imag**2);
  110.         if l < 0.000001: return n;
  111.         n = n + 1;
  112.     return -1;
  113.  
  114.  
  115. def ct_range_number(n):
  116.     while (n > 10000000000):
  117.         n /= 1723.13327;
  118.     return n;
  119.  
  120.  
  121.  
  122. # Reverse Iteration Fractal Cipher
  123. #____________________________________________________________
  124. def ct_rifc_encrypt_nits(z, c, p, nits):
  125.     t = "0123456789ABCDEF";
  126.     for nit in nits:
  127.         r = ct_croots(z - c, p);
  128.         z = r[t.index(nit)];
  129.     return z;
  130.  
  131. def ct_rifc_decrypt_nits(z, c, p, n):
  132.     t = "0123456789ABCDEF";
  133.     nits = "";
  134.     for i in range(n):
  135.         f = z**p + c;
  136.         r = ct_croots(f - c, p);
  137.         nit = ct_froot(z, r);
  138.         nits = nits + t[nit];
  139.         z = f;
  140.     nits = nits[::-1];
  141.     return (nits, z);
  142.  
  143.  
  144. # Funny Fractal Encryption, low-level
  145. #____________________________________________________________
  146.  
  147. def ct_ffe_obtain_number_fwd(n):
  148.     rn = math.modf(ct_range_number(math.fabs(n)));
  149.     v0 = rn[0] * 10000000000;
  150.     rn = math.modf(v0);
  151.     v1 = rn[0] * 10000000000;
  152.     v2 = int(math.fabs(math.floor(v1)));
  153.     return v2;
  154.  
  155.  
  156. def ct_ffe_gen_rand_axes(dims):
  157.     d = dims[1] - dims[0];
  158.     return (-random.random() * d, random.random() * d,
  159.             -random.random() * d, random.random() * d);
  160.  
  161. def ct_ffe_axes_combine(a0, a1):
  162.     return (-(math.fabs(a0[0] * a1[0] * 7.13263) % 3.0),
  163.             (math.fabs(a0[1] * a1[1] * 7.13263) % 3.0),
  164.             -(math.fabs(a0[2] * a1[2] * 7.13263) % 3.0),
  165.             (math.fabs(a0[3] * a1[3] * 7.13263) % 3.0));
  166.  
  167. def ct_ffe_gen_rand_bytes(n):
  168.     rtxt = "";
  169.     for i in range(n):
  170.         rtxt = rtxt + chr(random.randint(0, 255));
  171.     return rtxt;
  172.  
  173. # Forward Iterate
  174. def ct_ffe_fwd_iterate(z, c, p, s, e, n):
  175.     for i in range(n):
  176.         if (z.real == 0 and z.imag == 0):
  177.             z += (0.00000001+0.0000000013);
  178.         f = i % 2;
  179.         z = z**p[f] + c[f];
  180.         z = z * s;
  181.         d = ct_cabs(z);
  182.         if (d > e):
  183.             return (z, i);
  184.     return (z, n);
  185.  
  186. def ct_ffe_forward(z, c, p, s, e, n, m):
  187.     fwd = ct_ffe_fwd_iterate(z, c, p, s, e, n);
  188.     fwd_mod_0 = ct_ffe_obtain_number_fwd(fwd[0].real * (fwd[1] + 1));
  189.     fwd_mod_1 = ct_ffe_obtain_number_fwd(fwd[0].imag * (fwd[1] + 1));
  190.     fwd_mod = ct_cpair((fwd_mod_0, fwd_mod_1)) * ((fwd_mod_0 + fwd_mod_1) % 65536);
  191.     return fwd_mod % m;
  192.  
  193. def ct_ffe_cipher(a, c, p, s, e, n, ptxt, encrypt):
  194.     ctxt = "";
  195.     n = len(ptxt);
  196.     dims = math.ceil(math.sqrt(n));
  197.     pn = int(math.ceil(math.fabs(p[0])));
  198.  
  199.     xstep = (a[1] - a[0]) / dims;
  200.     ystep = (a[3] - a[2]) / dims;
  201.  
  202.     for i in range(n):
  203.         x = i % dims;
  204.         y = int(i / dims);
  205.  
  206.         z = complex(a[0] + x * xstep, a[3] - y * ystep);
  207.  
  208.         cmod = ct_ffe_forward(z, c, p, s, e, n, 256);
  209.         pchr = ord(ptxt[i]);
  210.        
  211.         if (encrypt):
  212.             cchr = (pchr + cmod) % 256;
  213.         else:
  214.             cchr = pchr - cmod;
  215.             if (cchr < 0):
  216.                 cchr = int(math.fabs((pchr + 256) - cmod));
  217.  
  218.         ctxt = ctxt + chr(cchr);
  219.  
  220.     return ctxt;
  221.  
  222.  
  223. # FFE Random Encryption
  224. #____________________________________________________________
  225.  
  226. def ct_ffe_rand_gen_bitmap(a, c, p, s, e, n, cn):
  227.  
  228.     bmap = "";
  229.     pcn = cn / 2;
  230.  
  231.     dims = math.ceil(math.sqrt(cn));
  232.  
  233.     xstep = (a[1] - a[0]) / dims;
  234.     ystep = (a[3] - a[2]) / dims;
  235.  
  236.  
  237.     ir = 0;
  238.     ic = 0;
  239.     i = 0;
  240.     while (ir < pcn or ic < pcn):
  241.         x = i % dims;
  242.         y = int(i / dims);
  243.  
  244.         z = complex(a[0] + x * xstep, a[3] - y * ystep);
  245.  
  246.         cmod = ct_ffe_forward(z, c, p, s, e, n, 1037);
  247.         cbit = cmod & 1;
  248.  
  249.         if ((cbit and ic < pcn) or ir == pcn):
  250.             if (ic < pcn):
  251.                 bmap = bmap + "1";
  252.                 ic = ic + 1;
  253.         elif (ir < pcn or ic == pcn):
  254.             if (ir < pcn):
  255.                 bmap = bmap + "0";
  256.                 ir = ir + 1;
  257.  
  258.         i = i + 1;
  259.  
  260.     if (ir != pcn or ic != pcn):
  261.         print("*********** BITMAP ALERT ***********");
  262.  
  263.     #print("ir:%s, ic:%s" % (ir, ic));
  264.  
  265.     return bmap;
  266.  
  267.  
  268. def ct_ffe_bitmap_display(bmap):
  269.     dtxt = "";
  270.     n = len(bmap);
  271.     for i in range(n):
  272.         dtxt = dtxt + bmap[i];
  273.         if (not ((i + 1) % 48)):
  274.             dtxt = dtxt + "\n";
  275.     print(dtxt);
  276.  
  277.  
  278. def ct_ffe_rand_encrypt(a, c, p, s, e, n, ptxt):
  279.     n = len(ptxt);
  280.     cn = n * 2;
  281.  
  282.     rtxt = ct_ffe_gen_rand_bytes(n);
  283.     rctxt = "";
  284.     bmap = ct_ffe_rand_gen_bitmap(a, c, p, s, e, n, cn);
  285.    
  286.     print("\n\n\nEncrypt Fractal Bitmap, len:%s" % (len(bmap)));
  287.     print("________________________________________");
  288.     ct_ffe_bitmap_display(bmap);
  289.  
  290.     for i in range(n):
  291.         pchr = ord(ptxt[i]);
  292.         cmod = ord(rtxt[i]);
  293.         cchr = (pchr + cmod) % 256;
  294.         rctxt = rctxt + chr(cchr);
  295.  
  296.     ctxt = "";
  297.     ic = 0;
  298.     ir = 0;
  299.     for i in range(cn):
  300.         if (int(bmap[i])):
  301.             ctxt = ctxt + rctxt[ic];
  302.             ic = ic + 1;
  303.         else:
  304.             ctxt = ctxt + rtxt[ir];
  305.             ir = ir + 1;
  306.  
  307.     return ctxt;
  308.  
  309.  
  310. def ct_ffe_rand_decrypt(a, c, p, s, e, n, ctxt):
  311.     cn = len(ctxt);
  312.     n = int(cn / 2);
  313.  
  314.     rctxt = "";
  315.     rtxt = "";
  316.     bmap = ct_ffe_rand_gen_bitmap(a, c, p, s, e, n, cn);
  317.    
  318.     print("\n\n\nDecrypt Fractal Bitmap, len:%s" % (len(bmap)));
  319.     print("________________________________________");
  320.     ct_ffe_bitmap_display(bmap);
  321.  
  322.     for i in range(cn):
  323.         if (int(bmap[i])):
  324.             rctxt = rctxt + ctxt[i];
  325.         else:
  326.             rtxt = rtxt + ctxt[i];
  327.  
  328.     ptxt = "";
  329.  
  330.     for i in range(n):
  331.         pchr = ord(rctxt[i]);
  332.         cmod = ord(rtxt[i]);
  333.  
  334.         cchr = pchr - cmod;
  335.         if (cchr < 0):
  336.             cchr = int(math.fabs((pchr + 256) - cmod));
  337.  
  338.         ptxt = ptxt + chr(cchr);
  339.  
  340.     return ptxt;
  341.  
  342.  
  343. # Funny Fractal Encryption, high-level
  344. #____________________________________________________________
  345. def ct_ffe_encrypt(axes, adims, hmack, c, p, s, e, n, ptxt):
  346.     ffe_raxes = ct_ffe_gen_rand_axes(adims);
  347.     ffe_axes = ct_ffe_axes_combine(axes, ffe_raxes);
  348.     ffe_rnd_axes = ct_ffe_axes_combine(axes, ffe_axes);
  349.  
  350.     #print("AXES:%s" % (str(ffe_axes)));
  351.  
  352.     rand_ctxt = ct_ffe_rand_encrypt(ffe_rnd_axes, c, p, s, e, n + 13, ptxt);
  353.  
  354.     ffe_ctxt = ct_ffe_cipher(ffe_axes, c, p, s, e, n, rand_ctxt, True);
  355.  
  356.     ffe_hmac = hmac.new(hmack.encode());
  357.     for i in ffe_ctxt:
  358.         ffe_hmac.update(str(i).encode());
  359.  
  360.     ffe_hmac_hex = ffe_hmac.hexdigest();
  361.  
  362.     return [ffe_raxes, ffe_hmac_hex, ffe_ctxt];
  363.  
  364.  
  365. def ct_ffe_decrypt(axes, hmack, c, p, s, e, n, ctxt):
  366.     ffe_hmac = hmac.new(hmack.encode());
  367.     for i in ctxt[2]:
  368.         ffe_hmac.update(str(i).encode());
  369.  
  370.     ffe_hmac_hex = ffe_hmac.hexdigest();
  371.  
  372.     if (not hmac.compare_digest(ctxt[1], ffe_hmac_hex)):
  373.         print("\n\n************** HMAC VIOLATION ***************\n\n");
  374.         return "Bob is 0xDEADBEEF!";
  375.  
  376.     ffe_axes = ct_ffe_axes_combine(axes, ctxt[0]);
  377.     ffe_rnd_axes = ct_ffe_axes_combine(axes, ffe_axes);
  378.     ffe_dtxt = ct_ffe_cipher(ffe_axes, c, p, s, e, n, ctxt[2], False);
  379.     ptxt = ct_ffe_rand_decrypt(ffe_rnd_axes, c, p, s, e, n + 13, ffe_dtxt);
  380.  
  381.     return ptxt;
  382.  
  383.  
  384.  
  385.  
  386. # The Main Program
  387. #____________________________________________________________
  388.  
  389. print("Funny Fractal Encryption, pre-alpha 0.0.0.1");
  390. print("by Chris M. Thomasson");
  391. print("==========================================================\n\n");
  392.  
  393. # The Secret Key
  394. axes = (-0.75, .09, -.5, .3);
  395. adims = (-3.25, 3.25);
  396. hmack = "hmac_secret_key";
  397. c = ((-.75+.09j), (.0 + 1j), (-.4+.5j));
  398. p = (2.13, 3.14, 4);
  399. s = 1.0;
  400. e = 2.0;
  401. n = 103;
  402.  
  403.  
  404. # The Plaintext
  405. ptxt = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
  406. #ptxt = "Hello there, welcome to Funny Fractal Encryption!\nBy: Chris M. Thomasson, :^)";
  407.  
  408. print("\nOriginal Plaintext, len:%s" % (len(ptxt)));
  409. print("________________________________________");
  410. print("%s" % (ptxt));
  411. print("----------------------------------------");
  412. print("%s" % (ct_bytes_to_hex(ptxt, 0)));
  413.  
  414.  
  415. # Encryption
  416. ctxt = ct_ffe_encrypt(axes, adims, hmack, c, p, s, e, n, ptxt);
  417. print("\n\n\nThe Ciphertext:");
  418. print("________________________________________");
  419. print("(%s, %s)\n(%s, %s)" % (ctxt[0][0], ctxt[0][1], ctxt[0][2], ctxt[0][3]));
  420. print("----------------------------------------");
  421. print("%s" % (ctxt[1]));
  422. print("----------------------------------------");
  423. print("%s" % (ct_bytes_to_hex(ctxt[2], 0)));
  424.  
  425.  
  426. # Decryption
  427. dtxt = ct_ffe_decrypt(axes, hmack, c, p, s, e, n, ctxt);
  428. print("\n\nDecrypted Plaintext, len:%s" % (len(dtxt)));
  429. print("________________________________________");
  430. try:
  431.     print("%s" % (dtxt));
  432. except:
  433.     print("<print error>");
  434. print("----------------------------------------");
  435. print("%s" % (ct_bytes_to_hex(dtxt, 0)));
  436.  
  437.  
  438. if (ptxt != dtxt):
  439.     print("Data Corrupted!");
  440.  
  441.  
  442. #eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement