Chris_M_Thomasson

Funny Fractal Encryption (FFE) pre-alpha (0.0.0.2)

Aug 25th, 2016
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.66 KB | None | 0 0
  1. # Chris M. Thomasson 8/25/2016
  2. # Funny Fractal Encryption (FFE) pre-alpha (0.0.0.2)
  3.  
  4. import math;
  5. import random;
  6. import hmac;
  7.  
  8.  
  9. # Some Conversion Utilities
  10. #____________________________________________________________
  11. def ct_bytes_to_hex(origin, offset):
  12.     hex = "";
  13.     n = len(origin);
  14.     t = "0123456789ABCDEF";
  15.  
  16.     for i in range(offset, n):
  17.         c = ord(origin[i]);
  18.  
  19.         nibl = c & 0x0F;
  20.         nibh = (c & 0xF0) >> 4;
  21.  
  22.         hex = hex + t[nibh];
  23.         hex = hex + t[nibl];
  24.  
  25.         hex = hex + " ";
  26.  
  27.         if (not ((i + 1) % 16)): hex = hex + "\r\n";
  28.  
  29.     return hex;
  30.  
  31.  
  32. def ct_hex_to_bytes(origin, offset):
  33.     bytes = "";
  34.     n = len(origin);
  35.     t = "0123456789ABCDEF";
  36.  
  37.     i = offset + 1;
  38.     while (i < n):
  39.  
  40.         nibh = 0;
  41.         nibl = 0;
  42.  
  43.         try:
  44.             nibh = t.index(origin[i - 1]);
  45.             nibl = t.index(origin[i]);
  46.         except ValueError:
  47.             i = i + 1;
  48.             continue;
  49.  
  50.         if (nibh > -1 and nibl > -1):
  51.             c = nibh * 16 + nibl;
  52.             bytes = bytes + chr(c);
  53.  
  54.         i = i + 2;
  55.  
  56.     return bytes;
  57.  
  58.  
  59.  
  60. # Some Math Utilities
  61. #____________________________________________________________
  62.  
  63. # Cantor pair
  64. def ct_cpair(n):
  65.     return int(((n[0] + n[1]) * ((n[0] + n[1]) + 1)) / 2) + n[1];
  66.  
  67. def ct_cpairi(n):
  68.     n0 = 8 * n + 1;
  69.     x = int((math.sqrt(n0) - 1) / 2);
  70.     y = n - int(((x + 1) * x) / 2);
  71.     return (x - 1, y);
  72.  
  73. # Complex Absolute Value
  74. def ct_cabs(z):
  75.     return math.sqrt(z.real**2 + z.imag**2);
  76.  
  77. # Complex Argument
  78. def ct_carg(z):
  79.     return math.atan2(z.imag, z.real);
  80.  
  81. # n from pow
  82. def ct_npow(p):
  83.     return int(math.ceil(math.fabs(p)));
  84.  
  85. # Complex Roots
  86. def ct_croots(z, p):
  87.     l = ct_cabs(z);
  88.     s = l**(1.0 / p);
  89.     a = ct_carg(z) / p;
  90.     n = ct_npow(p);
  91.     astep = (math.pi * 2.0) / p;
  92.     result = [];
  93.  
  94.     for i in range(n):
  95.         r = complex(math.cos(a + astep * i) * s,
  96.                     math.sin(a + astep * i) * s);
  97.         result.append(r);
  98.  
  99.     return result;
  100.  
  101.  
  102. # Find Root
  103. def ct_froot(z, r):
  104.     n = 0;
  105.     for i in r:
  106.         d = z - i;
  107.         l = math.sqrt(d.real**2 + d.imag**2);
  108.         if l < 0.000001: return n;
  109.         n = n + 1;
  110.     return -1;
  111.  
  112.  
  113. def ct_range_number(n):
  114.     while (n > 10000000000):
  115.         n /= 1723.13327;
  116.     return n;
  117.  
  118.  
  119. # Funny Fractal Encryption, low-level
  120. #____________________________________________________________
  121.  
  122. def ct_ffe_obtain_number_fwd(n):
  123.     rn = math.modf(ct_range_number(math.fabs(n)));
  124.     v0 = rn[0] * 10000000000;
  125.     rn = math.modf(v0);
  126.     v1 = rn[0] * 10000000000;
  127.     v2 = int(math.fabs(math.floor(v1)));
  128.     return v2;
  129.  
  130.  
  131. def ct_ffe_gen_rand_axes(dims):
  132.     d = dims[1] - dims[0];
  133.     return (-random.random() * d, random.random() * d,
  134.             -random.random() * d, random.random() * d);
  135.  
  136. def ct_ffe_axes_combine(a0, a1):
  137.     return (-(math.fabs(a0[0] * a1[0] * 7.13263) % 3.0),
  138.             (math.fabs(a0[1] * a1[1] * 7.13263) % 3.0),
  139.             -(math.fabs(a0[2] * a1[2] * 7.13263) % 3.0),
  140.             (math.fabs(a0[3] * a1[3] * 7.13263) % 3.0));
  141.  
  142. # Forward Iterate
  143. def ct_ffe_fwd_iterate(z, c, p, s, e, n):
  144.     for i in range(n):
  145.         if (z.real == 0 and z.imag == 0):
  146.             z += (0.00000001+0.0000000013);
  147.         f = i % 2;
  148.         z = z**p[f] + c[f];
  149.         z = z * s;
  150.         d = ct_cabs(z);
  151.         if (d > e):
  152.             return (z, i);
  153.     return (z, n);
  154.  
  155. def ct_ffe_forward(z, c, p, s, e, n, m):
  156.     fwd = ct_ffe_fwd_iterate(z, c, p, s, e, n);
  157.     fwd_mod_0 = ct_ffe_obtain_number_fwd(fwd[0].real * (fwd[1] + 1));
  158.     fwd_mod_1 = ct_ffe_obtain_number_fwd(fwd[0].imag * (fwd[1] + 1));
  159.     fwd_mod = ct_cpair((fwd_mod_0, fwd_mod_1)) * ((fwd_mod_0 + fwd_mod_1) % 65536);
  160.     return fwd_mod % m;
  161.  
  162. def ct_ffe_cipher(a, c, p, s, e, n, ptxt, encrypt):
  163.     ctxt = "";
  164.     n = len(ptxt);
  165.     dims = math.ceil(math.sqrt(n));
  166.     pn = int(math.ceil(math.fabs(p[0])));
  167.  
  168.     xstep = (a[1] - a[0]) / dims;
  169.     ystep = (a[3] - a[2]) / dims;
  170.  
  171.     for i in range(n):
  172.         x = i % dims;
  173.         y = int(i / dims);
  174.  
  175.         z = complex(a[0] + x * xstep, a[3] - y * ystep);
  176.  
  177.         cmod = ct_ffe_forward(z, c, p, s, e, n, 256);
  178.         pchr = ord(ptxt[i]);
  179.        
  180.         if (encrypt):
  181.             cchr = (pchr + cmod) % 256;
  182.         else:
  183.             cchr = pchr - cmod;
  184.             if (cchr < 0):
  185.                 cchr = int(math.fabs((pchr + 256) - cmod));
  186.  
  187.         ctxt = ctxt + chr(cchr);
  188.  
  189.     return ctxt;
  190.  
  191.  
  192. # Funny Fractal Encryption, high-level
  193. #____________________________________________________________
  194. def ct_ffe_encrypt(axes, adims, hmack, c, p, s, e, n, ptxt):
  195.     ffe_raxes = ct_ffe_gen_rand_axes(adims);
  196.     ffe_axes = ct_ffe_axes_combine(axes, ffe_raxes);
  197.  
  198.     print("AXES:%s" % (str(ffe_axes)));
  199.  
  200.     ffe_ctxt = ct_ffe_cipher(ffe_axes, c, p, s, e, n, ptxt, True);
  201.  
  202.     ffe_hmac = hmac.new(hmack.encode());
  203.     ffe_hmac.update(str(ffe_raxes[0]).encode());
  204.     ffe_hmac.update(str(ffe_raxes[1]).encode());
  205.     ffe_hmac.update(str(ffe_raxes[2]).encode());
  206.     ffe_hmac.update(str(ffe_raxes[3]).encode());
  207.     for i in ffe_ctxt:
  208.         ffe_hmac.update(str(i).encode());
  209.  
  210.     ffe_hmac_hex = ffe_hmac.hexdigest();
  211.  
  212.     return [ffe_raxes, ffe_hmac_hex, ffe_ctxt];
  213.  
  214.  
  215. def ct_ffe_decrypt(axes, hmack, c, p, s, e, n, ctxt):
  216.  
  217.     ffe_hmac = hmac.new(hmack.encode());
  218.     ffe_hmac.update(str(ctxt[0][0]).encode());
  219.     ffe_hmac.update(str(ctxt[0][1]).encode());
  220.     ffe_hmac.update(str(ctxt[0][2]).encode());
  221.     ffe_hmac.update(str(ctxt[0][3]).encode());
  222.     for i in ctxt[2]:
  223.         ffe_hmac.update(str(i).encode());
  224.  
  225.     ffe_hmac_hex = ffe_hmac.hexdigest();
  226.  
  227.     if (not hmac.compare_digest(ctxt[1], ffe_hmac_hex)):
  228.         print("\n\n************** HMAC VIOLATION ***************\n\n");
  229.         return "Bob is 0xDEADBEEF!";
  230.  
  231.     ffe_axes = ct_ffe_axes_combine(axes, ctxt[0]);
  232.     ptxt = ct_ffe_cipher(ffe_axes, c, p, s, e, n, ctxt[2], False);
  233.  
  234.     return ptxt;
  235.  
  236.  
  237. # The Main Program
  238. #____________________________________________________________
  239.  
  240. print("Funny Fractal Encryption, pre-alpha 0.0.0.2");
  241. print("by Chris M. Thomasson");
  242. print("==========================================================\n\n");
  243.  
  244. # The Secret Key
  245. axes = (-0.75, .09, -.5, .3);
  246. adims = (-3.25, 3.25);
  247. hmack = "hmac_secret_key";
  248. c = ((-.75+.09j), (.0 + 1j), (-.4+.5j));
  249. p = (2.13, 3.14, 4);
  250. s = 1.0;
  251. e = 2.75;
  252. n = 103;
  253.  
  254.  
  255. # The Plaintext
  256. ptxt = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
  257. #ptxt = "Hello there, welcome to Funny Fractal Encryption!\nBy: Chris M. Thomasson, :^)";
  258.  
  259. print("\nOriginal Plaintext, len:%s" % (len(ptxt)));
  260. print("________________________________________");
  261. print("%s" % (ptxt));
  262. print("----------------------------------------");
  263. print("%s" % (ct_bytes_to_hex(ptxt, 0)));
  264.  
  265.  
  266. # Encryption
  267. ctxt = ct_ffe_encrypt(axes, adims, hmack, c, p, s, e, n, ptxt);
  268. print("\n\n\nThe Ciphertext:");
  269. print("________________________________________");
  270. print("(%s, %s)\n(%s, %s)" % (ctxt[0][0], ctxt[0][1], ctxt[0][2], ctxt[0][3]));
  271. print("----------------------------------------");
  272. print("%s" % (ctxt[1]));
  273. print("----------------------------------------");
  274. print("%s" % (ct_bytes_to_hex(ctxt[2], 0)));
  275.  
  276.  
  277. # Decryption
  278. dtxt = ct_ffe_decrypt(axes, hmack, c, p, s, e, n, ctxt);
  279. print("\n\nDecrypted Plaintext, len:%s" % (len(dtxt)));
  280. print("________________________________________");
  281. try:
  282.     print("%s" % (dtxt));
  283. except:
  284.     print("<print error>");
  285. print("----------------------------------------");
  286. print("%s" % (ct_bytes_to_hex(dtxt, 0)));
  287.  
  288.  
  289. if (ptxt != dtxt):
  290.     print("Data Corrupted!");
  291.  
  292.  
  293. #eof
Advertisement
Add Comment
Please, Sign In to add comment