Chris_M_Thomasson

Funny Fractal Encryption in Python, v:0.0.0.0

Aug 24th, 2016
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.35 KB | None | 0 0
  1. # Chris M. Thomasson 8/24/2016
  2. # Funny Fractal Encryption pre-alpha (0.0.0.0)
  3.  
  4.  
  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.  
  76. # Complex Absolute Value
  77. def ct_cabs(z):
  78.     return math.sqrt(z.real**2 + z.imag**2);
  79.  
  80.  
  81. def ct_range_number(n):
  82.     while (n > 10000000000):
  83.         n /= 1723.13327;
  84.     return n;
  85.  
  86.  
  87. def ct_obtain_number_fwd(n):
  88.     rn = math.modf(ct_range_number(math.fabs(n)));
  89.     v0 = rn[0] * 10000000000;
  90.     rn = math.modf(v0);
  91.     v1 = rn[0] * 10000000000;
  92.     v2 = int(math.fabs(math.floor(v1)));
  93.     return v2;
  94.  
  95.  
  96. def ct_ffe_gen_rand_axes(dims):
  97.     d = dims[1] - dims[0];
  98.     return (-random.random() * d, random.random() * d,
  99.             -random.random() * d, random.random() * d);
  100.  
  101. def ct_ffe_axes_combine(a0, a1):
  102.     return (-(math.fabs(a0[0] * a1[0] * 7.13263) % 3.0),
  103.             (math.fabs(a0[1] * a1[1] * 7.13263) % 3.0),
  104.             -(math.fabs(a0[2] * a1[2] * 7.13263) % 3.0),
  105.             (math.fabs(a0[3] * a1[3] * 7.13263) % 3.0));
  106.  
  107. def ct_ffe_gen_rand_bytes(n):
  108.     rtxt = "";
  109.     for i in range(n):
  110.         rtxt = rtxt + chr(random.randint(0, 255));
  111.     return rtxt;
  112.  
  113.  
  114.  
  115. # Funny Fractal Encryption, low-level
  116. #____________________________________________________________
  117.  
  118. # Forward Iterate
  119. def ct_ffe_fwd_iterate(z, c, p, s, e, n):
  120.     for i in range(n):
  121.         if (z.real == 0 and z.imag == 0):
  122.             z += (0.00000001+0.0000000013);
  123.         f = i % 2;
  124.         z = z**p[f] + c[f];
  125.         z = z * s;
  126.         d = ct_cabs(z);
  127.         if (d > e):
  128.             return (z, i);
  129.     return (z, n);
  130.  
  131.  
  132. def ct_ffe_cipher(a, c, p, s, e, n, ptxt, encrypt):
  133.     ctxt = "";
  134.     n = len(ptxt);
  135.     dims = math.ceil(math.sqrt(n));
  136.     pn = int(math.ceil(math.fabs(p[0])));
  137.  
  138.     xstep = (a[1] - a[0]) / dims;
  139.     ystep = (a[3] - a[2]) / dims;
  140.  
  141.     for i in range(n):
  142.         x = i % dims;
  143.         y = int(i / dims);
  144.  
  145.         z = complex(a[0] + x * xstep, a[3] - y * ystep);
  146.  
  147.         fwd = ct_ffe_fwd_iterate(z, c, p, s, e, n);
  148.  
  149.         fwd_mod_0 = ct_obtain_number_fwd(fwd[0].real * (fwd[1] + 1));
  150.         fwd_mod_1 = ct_obtain_number_fwd(fwd[0].imag * (fwd[1] + 1));
  151.         fwd_mod = ct_cpair((fwd_mod_0, fwd_mod_1)) * ((fwd_mod_0 + fwd_mod_1) % 65536);
  152.  
  153.         cmod = fwd_mod % 256;
  154.         pchr = ord(ptxt[i]);
  155.        
  156.         if (encrypt):
  157.             cchr = (pchr + cmod) % 256;
  158.         else:
  159.             cchr = pchr - cmod;
  160.             if (cchr < 0):
  161.                 cchr = int(math.fabs((pchr + 256) - cmod));
  162.  
  163.         ctxt = ctxt + chr(cchr);
  164.  
  165.     return ctxt;
  166.  
  167.  
  168. # Random Encryption
  169. #____________________________________________________________
  170. def ct_rand_encrypt(ptxt):
  171.     n = len(ptxt);
  172.     ctxt = "";
  173.     rtxt = ct_ffe_gen_rand_bytes(n);
  174.  
  175.     for i in range(n):
  176.         pchr = ord(ptxt[i]);
  177.         cmod = ord(rtxt[i]);
  178.  
  179.         cchr = (pchr + cmod) % 256;
  180.  
  181.         ctxt = ctxt + chr(cchr);
  182.  
  183.     return ctxt + rtxt;
  184.  
  185.  
  186. def ct_rand_decrypt(ctxt):
  187.     n = int(len(ctxt) / 2);
  188.  
  189.     ptxt = "";
  190.  
  191.     for i in range(n):
  192.         pchr = ord(ctxt[i]);
  193.         cmod = ord(ctxt[i + n]);
  194.  
  195.         cchr = pchr - cmod;
  196.         if (cchr < 0):
  197.             cchr = int(math.fabs((pchr + 256) - cmod));
  198.  
  199.         ptxt = ptxt + chr(cchr);
  200.  
  201.     return ptxt;
  202.  
  203.  
  204.  
  205. # Funny Fractal Encryption, high-level
  206. #____________________________________________________________
  207. def ct_ffe_encrypt(axes, adims, hmack, c, p, s, e, n, ptxt):
  208.     ffe_raxes = ct_ffe_gen_rand_axes(adims);
  209.     ffe_axes = ct_ffe_axes_combine(axes, ffe_raxes);
  210.  
  211.     rand_ctxt = ct_rand_encrypt(ptxt);
  212.     ffe_ctxt = ct_ffe_cipher(ffe_axes, c, p, s, e, n, rand_ctxt, True);
  213.  
  214.     ffe_hmac = hmac.new(hmack.encode());
  215.     for i in ffe_ctxt:
  216.         ffe_hmac.update(str(i).encode());
  217.  
  218.     ffe_hmac_hex = ffe_hmac.hexdigest();
  219.  
  220.     return [ffe_raxes, ffe_hmac_hex, ffe_ctxt];
  221.  
  222.  
  223. def ct_ffe_decrypt(axes, hmack, c, p, s, e, n, ctxt):
  224.     ffe_hmac = hmac.new(hmack.encode());
  225.     for i in ctxt[2]:
  226.         ffe_hmac.update(str(i).encode());
  227.  
  228.     ffe_hmac_hex = ffe_hmac.hexdigest();
  229.  
  230.     if (not hmac.compare_digest(ctxt[1], ffe_hmac_hex)):
  231.         print("\n\n************** HMAC VIOLATION ***************\n\n");
  232.         return "Bob is 0xDEADBEEF!";
  233.  
  234.     ffe_axes = ct_ffe_axes_combine(axes, ctxt[0]);
  235.     ffe_dtxt = ct_ffe_cipher(ffe_axes, c, p, s, e, n, ctxt[2], False);
  236.     ptxt = ct_rand_decrypt(ffe_dtxt);
  237.  
  238.     return ptxt;
  239.  
  240.  
  241.  
  242.  
  243. # The Main Program
  244. #____________________________________________________________
  245.  
  246. print("Funny Fractal Encryption, pre-alpha 0.0.0.0");
  247. print("by Chris M. Thomasson");
  248. print("==========================================================\n\n");
  249.  
  250. # The Secret Key
  251. axes = (-0.75, .09, -.5, .3);
  252. adims = (-1.25, 1.25);
  253. hmack = "hmac_secret_key";
  254. c = ((-.75+.09j), (.0 + 1j));
  255. p = (2.13, 3.14);
  256. s = 1.0;
  257. e = 2.0;
  258. n = 103;
  259.  
  260.  
  261. # The Plaintext
  262. ptxt = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
  263. #ptxt = "Hello there, welcome to Funny Fractal Encryption!\nBy: Chris M. Thomasson";
  264.  
  265. print("\nOriginal Plaintext, len:%s" % (len(ptxt)));
  266. print("________________________________________");
  267. print("%s" % (ptxt));
  268. print("----------------------------------------");
  269. print("%s" % (ct_bytes_to_hex(ptxt, 0)));
  270.  
  271.  
  272. # Encryption
  273. ctxt = ct_ffe_encrypt(axes, adims, hmack, c, p, s, e, n, ptxt);
  274. print("\n\nCiphertext:");
  275. print("________________________________________");
  276. print("(%s, %s)\n(%s, %s)" % (ctxt[0][0], ctxt[0][1], ctxt[0][2], ctxt[0][3]));
  277. print("----------------------------------------");
  278. print("%s" % (ctxt[1]));
  279. print("----------------------------------------");
  280. print("%s" % (ct_bytes_to_hex(ctxt[2], 0)));
  281.  
  282.  
  283. # Decryption
  284. dtxt = ct_ffe_decrypt(axes, hmack, c, p, s, e, n, ctxt);
  285. print("\nDecrypted Plaintext, len:%s" % (len(dtxt)));
  286. print("________________________________________");
  287. try:
  288.     print("%s" % (dtxt));
  289. except:
  290.     print("<print error>");
  291. print("----------------------------------------");
  292. print("%s" % (ct_bytes_to_hex(dtxt, 0)));
  293.  
  294.  
  295. if (ptxt != dtxt):
  296.     print("Data Corrupted!");
  297.  
  298.  
  299. #eof
Advertisement
Add Comment
Please, Sign In to add comment