Chris_M_Thomasson

RIFC Pre-Alpha 0.0.0.1... ;^)

Aug 21st, 2016
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 20.18 KB | None | 0 0
  1.  
  2.  
  3. # Chris M. Thomasson 8/21/2016
  4. # Reverse Iteration Fractal Cipher (RIFC)
  5. # Pre-Alpha Version: 0.0.0.1
  6.  
  7. #    This program is free software: you can redistribute it and/or modify
  8. #    it under the terms of the GNU General Public License as published by
  9. #    the Free Software Foundation, either version 3 of the License, or
  10. #    (at your option) any later version.
  11. #
  12. #    This program is distributed in the hope that it will be useful,
  13. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #    GNU General Public License for more details.
  16. #
  17. #   You should have received a copy of the GNU General Public License
  18. #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.  
  20.  
  21. import math;
  22. import random;
  23. import hmac;
  24. import copy;
  25. import os;
  26.  
  27.  
  28.  
  29. # RIFC General Complex Math Utilities
  30. #____________________________________________________________
  31.  
  32. # Complex Absolute Value
  33. def ct_cabs(z):
  34.     return math.sqrt(z.real**2 + z.imag**2);
  35.  
  36.  
  37. # Complex Argument
  38. def ct_carg(z):
  39.     return math.atan2(z.imag, z.real);
  40.  
  41.  
  42. # Complex Argument Range [0...PI2]
  43. def ct_cargr(z):
  44.     a = math.atan2(z.imag, z.real);
  45.     if (a < 0): a = a + math.pi * 2;
  46.     return a;
  47.  
  48.  
  49. # Get n from a power
  50. def ct_npow(p):
  51.     return int(math.ceil(math.fabs(p)));
  52.  
  53.  
  54. # Complex Roots
  55. def ct_croots(z, p):
  56.     l = ct_cabs(z);
  57.     s = l**(1.0 / p);
  58.     a = ct_carg(z) / p;
  59.     n = ct_npow(p);
  60.     astep = (math.pi * 2.0) / p;
  61.     result = [];
  62.  
  63.     for i in range(n):
  64.         r = complex(math.cos(a + astep * i) * s,
  65.                     math.sin(a + astep * i) * s);
  66.         result.append(r);
  67.  
  68.     return result;
  69.  
  70.  
  71. # Find Root
  72. def ct_froot(z, r):
  73.     n = 0;
  74.     for i in r:
  75.         d = z - i;
  76.         l = math.sqrt(d.real**2 + d.imag**2);
  77.         if l < 0.000001: return n;
  78.         n = n + 1;
  79.     return -1;
  80.  
  81.  
  82.  
  83. # RIFC Number Conversions and Nit Utilities
  84. #____________________________________________________________
  85.  
  86. # Convert base ten 10 to nits, pad to n
  87. def ct_base_10_to_nits_pad(v, b, n):
  88.     nits = "";
  89.     r = 0;
  90.     for i in range(n):
  91.         d = math.floor(v / b);
  92.         r = v - (d * b);
  93.         nits = nits + str(r);
  94.         v = d;
  95.     nits = nits[::-1];
  96.     return nits;
  97.  
  98. # Convert base ten 10 to nits
  99. def ct_base_10_to_nits(v, b):
  100.     nits = "";
  101.     r = 0;
  102.     while 1:
  103.         d = math.floor(v / b);
  104.         r = v - (d * b);
  105.         nits = nits + str(r);
  106.         v = d;
  107.         if (d == 0): break;
  108.     nits = nits[::-1];
  109.     return nits;
  110.  
  111.  
  112. # Convert nits to base 10
  113. def ct_nits_to_base_10(nits, b):
  114.     p = len(nits) - 1;
  115.     r = 0;
  116.     for nit in nits:
  117.         r = r + int(nit)*b**p;
  118.         p = p - 1;
  119.     return r;
  120.  
  121.  
  122. # Generate n random nits in range [0...(b-1)]
  123. def ct_gen_rand_nits(n, b):
  124.     nits = "";
  125.     for i in range(n):
  126.         nits = nits + str(random.randint(0, b - 1));
  127.     return nits;
  128.  
  129. def ct_number_from_ptxt_bytes_n(ptxt, o, n):
  130.     r = 0;
  131.     imax = len(ptxt);
  132.     p = 0;
  133.     for i in range(o, n + 1):
  134.         b = ord(ptxt[i]);
  135.         a = b * 256**p;
  136.         r = r + a;
  137.         #print("ni[%s]:(b:%s, r:%s)" % (i, b, r));
  138.         p = p + 1;
  139.     return r;
  140.  
  141. def ct_ptxt_bytes_from_number(n, b, e):
  142.     r = 0;
  143.     ni = 0;
  144.     ptxt = "";
  145.  
  146.     # trail zero...
  147.     if (n > b):
  148.         if (n == e):
  149.             ptxt = ptxt + chr(0);
  150.             ptxt = ptxt + chr(0);
  151.             return ptxt;
  152.  
  153.         xn = n - b;
  154.         ptxt = ptxt + chr(xn);
  155.         ptxt = ptxt + chr(0);
  156.         return ptxt;
  157.  
  158.     while 1:
  159.         d = math.floor(n / 256);
  160.         r = n - (d * 256);
  161.         ptxt = ptxt + chr(r);
  162.         n = d;
  163.         if (d == 0): break;
  164.  
  165.     return ptxt;
  166.  
  167.  
  168.  
  169. # RIFC 2d Axes Class
  170. #___________________________________________________________
  171.  
  172. class ct_rifc_axes_2d:
  173.     def __init__(self, xmin, xmax, ymin, ymax):
  174.         self.xmin = xmin;
  175.         self.xmax = xmax;
  176.         self.ymin = ymin;
  177.         self.ymax = ymax;
  178.  
  179.     def __repr__(self):
  180.         return "(xmin:%s, xmax:%s, ymin:%s, ymax:%s)" % (self.xmin, self.xmax, self.ymin, self.ymax)
  181.  
  182.     def __str__(self): return self.__repr__();
  183.  
  184.     def width(self): return  self.xmax - self.xmin;
  185.     def height(self): return  self.ymax - self.ymin;
  186.  
  187.     def offset(self, axes):
  188.         a = ct_rifc_axes_2d(
  189.             self.xmin + axes.xmin,
  190.             self.xmax + axes.xmax,
  191.             self.ymin + axes.ymax,
  192.             self.ymax + axes.ymax
  193.         );
  194.         return a;
  195.  
  196.  
  197. # RIFC 2d Plane Class
  198. #___________________________________________________________
  199. class ct_rifc_plane_2d:
  200.     def __init__(self, width, height, axes):
  201.         self.axes = copy.copy(axes);
  202.         self.width = width;
  203.         self.height = height;
  204.         self.xstep = self.axes.width() / width;
  205.         self.ystep = self.axes.height() / height;
  206.         self.aratio = height / width;
  207.  
  208.     def __repr__(self):
  209.         return "(axes:%s, width:%s, height:%s, xstep:%s, ystep:%s, aratio:%s)" % (self.axes,
  210.                                              self.width, self.height,
  211.                                              self.xstep, self.ystep,
  212.                                              self.aratio);
  213.  
  214.     def __str__(self): return self.__repr__();
  215.  
  216.     def project_to(self, p):
  217.         x = math.floor((p.real - self.axes.xmin) / self.xstep * self.aratio);
  218.         y = math.floor((self.axes.ymax - p.imag) / self.ystep);
  219.         return complex(x, y);
  220.  
  221.     def project_int_to(self, p):
  222.         return complex(self.axes.xmin + p.real * self.xstep,
  223.                        self.axes.ymax - p.imag * self.ystep);
  224.  
  225.  
  226. # RIFC Secret Key Class
  227. #____________________________________________________________
  228. class ct_rifc_skey:
  229.     def __init__(self, axes, z, c, p, r_n, s, hmkey):
  230.         self.axes = copy.copy(axes);
  231.         self.z = z;
  232.         self.c = copy.copy(c);
  233.         self.p = copy.copy(p);
  234.         self.r_n = r_n;
  235.         a = 256**2;
  236.  
  237.         nits = ct_base_10_to_nits((a + 256 + 1), ct_npow(p[0]));
  238.         self.n_n = len(nits) + 1;
  239.  
  240.         self.s = s;
  241.         self.hmack = copy.copy(hmkey);
  242.  
  243.     def __repr__(self):
  244.         return "(axes:%s, z:%s, c:%s, p:%s, r_n:%s, n_n:%s, s:%s, hmack:%s)" % (self.axes, self.z, self.c, self.p, self.r_n, self.n_n, self.s, self.hmack);
  245.  
  246.     def __str__(self): return self.__repr__();
  247.  
  248.  
  249. # RIFC Native Encrypt Class
  250. #____________________________________________________________
  251. class ct_rifc_encrypt:
  252.     def __init__(self, skey):
  253.         self.skey = skey; # mutable reference
  254.  
  255.     def __repr__(self):
  256.         return "(skey:%s)" % (self.skey);
  257.  
  258.     def __str__(self): return self.__repr__();
  259.  
  260.     def process(self, z, c, p, ptxt):
  261.         i = 0;
  262.         for nit in ptxt:
  263.             r = ct_croots(z - c, p);
  264.             z = r[int(nit)];
  265.             #print("encrypt_process:z[%s]:(%s):(%s)" % (i, nit, z));
  266.             i = i + 1;
  267.         return z;
  268.  
  269.  
  270. # RIFC Native Decrypt Class
  271. #____________________________________________________________
  272. class ct_rifc_decrypt:
  273.     def __init__(self, skey):
  274.         self.skey = skey; # mutable reference
  275.  
  276.     def __repr__(self):
  277.         return "(skey:%s)" % (self.skey);
  278.  
  279.     def __str__(self): return self.__repr__();
  280.  
  281.     def process(self, z, c, p, n):
  282.         ptxt = "";
  283.         for i in range(n):
  284.             f = z**p + c;
  285.             r = ct_croots(f - c, p);
  286.             nit = ct_froot(z, r);
  287.             ptxt = ptxt + str(nit);
  288.             #print("decrypt_process:z[%s]:(%s):(%s)" % (n - i - 1, nit, z));
  289.             z = f;
  290.             i = i + 1;
  291.         ptxt = ptxt[::-1]; # reverse nits
  292.         return [ptxt, z];
  293.  
  294.  
  295. # RIFC Block Compatible Nit Class
  296. #____________________________________________________________
  297. class ct_rifc_nitblock:
  298.     def __init__(self, z, ptxt, ptxt_nits, rand_nits):
  299.         self.z = z;
  300.         self.ptxt = ptxt;
  301.         self.ptxt_nits = ptxt_nits;
  302.         self.rand_nits = rand_nits;
  303.  
  304.     def __repr__(self):
  305.         return "(z:%s, ptxt:%s, ptxt_nits:%s, ptxt_n_nits:%s, rand_nits:%s)" % (self.z, self.ptxt, self.ptxt_nits, self.ptxt_n_nits, self.rand_nits);
  306.  
  307.     def __str__(self): return self.__repr__();
  308.  
  309.     def compare(self, rhs):
  310.         if (self.ptxt_nits != rhs.ptxt_nits or
  311.             self.rand_nits != rhs.rand_nits):
  312.             return False;
  313.         return True;
  314.  
  315.    
  316. def ct_rifc_nitblock_from_number(fskey, z, n):
  317.     ptxt_nits = ct_base_10_to_nits_pad(n, ct_npow(fskey.p[0]), fskey.n_n);
  318.     rand_nits = ct_gen_rand_nits(fskey.r_n, ct_npow(fskey.p[1]));
  319.     return ct_rifc_nitblock(z, n, ptxt_nits, rand_nits);
  320.  
  321.  
  322.  
  323. # RIFC Block Cipher Class
  324. #____________________________________________________________
  325. class ct_rifc_block:
  326.     def __init__(self, fskey):
  327.         self.fskey = fskey;
  328.  
  329.     def __repr__(self):
  330.         return "(fskey:%s)" % (self.fskey);
  331.  
  332.     def __str__(self): return self.__repr__();
  333.  
  334.     def encrypt(self, nblk):
  335.         fencrypt = ct_rifc_encrypt(self.fskey);
  336.  
  337.         #print("\nBlock Encryption Process");
  338.         #print("___________________________________________________");
  339.  
  340.         #print("nblk:%s" % (nblk));
  341.         #print("--------------------------------------");
  342.  
  343.         #print("\nplaintext");
  344.         #print("--------------------------------------");
  345.         z = nblk.z;
  346.         z = fencrypt.process(z, fskey.c[0], fskey.p[0], nblk.ptxt_nits);
  347.  
  348.         #print("\nrandom plaintext");
  349.         #print("--------------------------------------");
  350.         z = fencrypt.process(z, fskey.c[1], fskey.p[1], nblk.rand_nits);
  351.  
  352.         #print("___________________________________________________\n");
  353.  
  354.         return z;
  355.  
  356.     def decrypt(self, z):
  357.         fdecrypt = ct_rifc_decrypt(self.fskey);
  358.  
  359.         #print("\nBlock Decryption Process");
  360.         #print("___________________________________________________");
  361.  
  362.         #print("\nrandom plaintext");
  363.         #print("--------------------------------------");
  364.         z = fdecrypt.process(z, self.fskey.c[1], self.fskey.p[1], self.fskey.r_n);
  365.         rand_nits = z[0];
  366.  
  367.         #print("\nplaintext");
  368.         #print("--------------------------------------");
  369.         z = fdecrypt.process(z[1], self.fskey.c[0], self.fskey.p[0], self.fskey.n_n);
  370.         ptxt_nits = z[0];
  371.  
  372.         #print("___________________________________________________\n");
  373.  
  374.         nblk = ct_rifc_nitblock(z[1], ct_nits_to_base_10(ptxt_nits, ct_npow(self.fskey.p[0])), ptxt_nits, rand_nits);
  375.  
  376.         return nblk;
  377.  
  378.  
  379.  
  380. # RIFC Ciphertext Class
  381. #____________________________________________________________
  382. class ct_rifc_ciphertext:
  383.     def __init__(self, hmhash, axes, ctxt):
  384.         self.hmhash = hmhash;
  385.         self.axes = axes;
  386.         self.ctxt = ctxt;
  387.  
  388.     def __repr__(self):
  389.         s = ("(%s)\n" % (self.hmhash));
  390.         s = ("%s\n" % (self.axes));
  391.         for cp in self.ctxt: s = s + ("%s\n" % (cp));
  392.         return s;
  393.  
  394.     def __str__(self): return self.__repr__();
  395.  
  396.     def hmack_assert(self, hmhash_compare):
  397.         return hmac.compare_digest(self.hmhash, hmhash_compare);
  398.  
  399.  
  400.  
  401. # Main RIFC Cipher Class
  402. #____________________________________________________________
  403. class ct_rifc:
  404.     def __init__(self, fskey):
  405.         self.fskey = fskey;
  406.  
  407.     def __repr__(self):
  408.         return "(fskey:%s)" % (self.fskey);
  409.  
  410.     def __str__(self): return self.__repr__();
  411.  
  412.     def ffe_gen_rand_axes(self, nmin, nmax):
  413.         d = nmax - nmin;
  414.         r = d / 2;
  415.         xmin = -random.random() * r;
  416.         xmax = random.random() * r;
  417.         ymin = -random.random() * r;
  418.         ymax = random.random() * r;
  419.         return ct_rifc_axes_2d(xmin, xmax, ymin, ymax);
  420.  
  421.     def ffe_forward_iterate(self, z, c, p, s, n, e):
  422.         for i in range(n):
  423.             f = i % 3;
  424.             z = z**self.fskey.p[f] + self.fskey.c[2];
  425.             z = z**p + c;
  426.             z = z * s;
  427.             d = ct_cabs(z);
  428.             if (d > e):
  429.                 return [z, i];
  430.  
  431.         return [z, n];
  432.  
  433.     def ffe_in_range(self, n, m):
  434.         while (ct_cabs(n) > m):
  435.             n = n * 0.413;
  436.         return n;
  437.  
  438.     # Apply a Funny Fractal Mutation to the Plaintext
  439.     def ffe_cipher_ptxt(self, ptxt, axes, encrypt):
  440.         ffe_ctxt = "";
  441.         n = len(ptxt);
  442.         dims = int(math.ceil(math.sqrt(n)));
  443.         plane = ct_rifc_plane_2d(dims, dims, axes);
  444.         for i in range(n):
  445.             vp = complex(i % dims, int(math.floor(i / dims)));
  446.             z = plane.project_int_to(vp);
  447.             ffe_fwd = self.ffe_forward_iterate(z, self.fskey.c[2], self.fskey.p[2], self.fskey.s, 64, 2.0);
  448.             dis = ct_cabs(z);
  449.             a = (ffe_fwd[0].real * 354125) + (ffe_fwd[0].imag * 234510) * (ffe_fwd[1] + dims / 2 + 1) * dis;
  450.             a = int(math.floor(math.fabs(a * 13))) % 256;
  451.  
  452.             if (encrypt):
  453.                 ffe_ctxt = ffe_ctxt + chr(ord(ptxt[i]) ^ a);
  454.             else:
  455.                 ffe_ctxt = ffe_ctxt + chr(a ^ ord(ptxt[i]));
  456.  
  457.             #print("ffe_fwd:%s" % (ffe_fwd));
  458.             i = i + 1;
  459.         return ffe_ctxt;
  460.  
  461.  
  462.  
  463.     # Apply a Funny Fractal Mutation to the Ciphertext
  464.     def ffe_cipher_ctxt(self, ctxt, axes, encrypt):
  465.         ffe_ctxt = [];
  466.         n = len(ctxt);
  467.         dims = int(math.ceil(math.sqrt(n)));
  468.         plane = ct_rifc_plane_2d(dims, dims, axes);
  469.  
  470.         for i in range(n):
  471.             vp = complex(i % dims, int(math.floor(i / dims)));
  472.             z = plane.project_int_to(vp);
  473.             ffe_fwd = self.ffe_forward_iterate(z, self.fskey.c[2], self.fskey.p[2], self.fskey.s, 64, 2.0);
  474.  
  475.             ffe_fwd[0] = self.ffe_in_range(ffe_fwd[0], 32);
  476.  
  477.             if (encrypt):
  478.                 mcp = ctxt[i] + ffe_fwd[0];
  479.             else:
  480.                 mcp = ctxt[i] - ffe_fwd[0];
  481.  
  482.            
  483.             ffe_ctxt.append(mcp);
  484.  
  485.         return ffe_ctxt;
  486.  
  487.  
  488.  
  489.     def encrypt(self, ptxt):
  490.         ffe_rand_axes = self.ffe_gen_rand_axes(-3.14159, 3.14159);
  491.         axes = self.fskey.axes.offset(ffe_rand_axes);
  492.  
  493.  
  494.         #xxxptxt = self.ffe_cipher_ptxt(ptxt, axes, True);
  495.         #yyyptxt = self.ffe_cipher_ptxt(xxxptxt, axes, False);
  496.  
  497.         ffe_ctxt = self.ffe_cipher_ptxt(ptxt, axes, True);
  498.  
  499.         #ffe_ctxt = ffe_ctxt + chr(0);
  500.         #ffe_ctxt = ffe_ctxt + chr(0);
  501.  
  502.         i = 0;
  503.         n = len(ffe_ctxt);
  504.         nsum = 0;
  505.  
  506.         ciphertext = [];
  507.         hmassert = hmac.new(self.fskey.hmack.encode());
  508.  
  509.         rifc_block = ct_rifc_block(self.fskey);
  510.         z = self.fskey.z;
  511.  
  512.         d = int(math.floor(n / 2));
  513.         r = n - d * 2;
  514.         c = 0;
  515.  
  516.  
  517.         for i in range(1, n, 2):
  518.             xtxtc = ffe_ctxt[i - 1] + ffe_ctxt[i];
  519.             a = len(xtxtc);
  520.             c0 = ord(ffe_ctxt[i - 1]);
  521.             c1 = ord(ffe_ctxt[i]);
  522.             v = c1*256 + c0;
  523.  
  524.             if (c1 == 0):
  525.                 v = v + 65536;
  526.                 if (c0 == 0):
  527.                     v = 65800;
  528.  
  529.             nblk = ct_rifc_nitblock_from_number(fskey, z, v);
  530.             cipherpoint = rifc_block.encrypt(nblk);
  531.  
  532.             # Crude Trial Encrypt of (cipherpoint)
  533.             td_nblk = rifc_block.decrypt(cipherpoint);
  534.             aa = ct_nits_to_base_10(td_nblk.ptxt_nits, ct_npow(self.fskey.p[0]));
  535.  
  536.             xtxt = ct_ptxt_bytes_from_number(td_nblk.ptxt, 65536, 65800);
  537.  
  538.             if (not nblk.compare(td_nblk) or v != aa or xtxtc != xtxt):
  539.                 print("****************** Trial Decrypt Engaged, encrypt side! ***********************");
  540.                 print("c-:%s, c1:%s" % (c0, c));
  541.                 xxi = 0;
  542.                 xtxt_0 = ct_ptxt_bytes_from_number(td_nblk.ptxt, 65536, 65800);
  543.                 xxa = xxi + 1;
  544.                 # Future work...
  545.  
  546.  
  547.             #hmassert.update(str(cipherpoint).encode());
  548.             ciphertext.append(cipherpoint);
  549.             z = cipherpoint;
  550.  
  551.         for i in range(n - r, n):
  552.             v = ord(ffe_ctxt[i]);
  553.             nblk = ct_rifc_nitblock_from_number(fskey, z, v);
  554.             cipherpoint = rifc_block.encrypt(nblk);
  555.  
  556.             # Crude Trial Encrypt of (cipherpoint)
  557.             td_nblk = rifc_block.decrypt(cipherpoint);
  558.  
  559.             if (not nblk.compare(td_nblk)):
  560.                 print("****************** Trial Decrypt Engaged! ***********************");
  561.                 xxi = 0;
  562.                 xxa = xxi + 1;
  563.                 # Future work...
  564.  
  565.  
  566.             #hmassert.update(str(cipherpoint).encode());
  567.             ciphertext.append(cipherpoint);
  568.             z = cipherpoint;
  569.  
  570.         # FFE Ciphertext Mutation
  571.         xctxt = self.ffe_cipher_ctxt(ciphertext, axes, True);
  572.  
  573.         for cp in xctxt:
  574.             hmassert.update(str(cp).encode());
  575.  
  576.         hmhash = hmassert.hexdigest();
  577.  
  578.         return ct_rifc_ciphertext(hmhash, axes, xctxt);
  579.  
  580.     def decrypt(self, ctxt):
  581.         ffe_ptxt = "";
  582.         rifc_block = ct_rifc_block(self.fskey);
  583.         axes = ctxt.axes;
  584.  
  585.         hmassert = hmac.new(self.fskey.hmack.encode());
  586.  
  587.         # check HMAC
  588.         for cp in ctxt.ctxt:
  589.             hmassert.update(str(cp).encode());
  590.  
  591.         hmhash = hmassert.hexdigest();
  592.  
  593.         if (not ctxt.hmack_assert(hmhash)):
  594.             print("\n\n************** !! HMAC VOILATION !! ****************\n\n");
  595.             return "Bob is 0xDEADBEEF!!!";
  596.  
  597.         ffe_ctxt = self.ffe_cipher_ctxt(ctxt.ctxt, axes, False);
  598.  
  599.         for cp in ffe_ctxt:
  600.             nblk = rifc_block.decrypt(cp);
  601.             ffe_ptxt = ffe_ptxt + ct_ptxt_bytes_from_number(nblk.ptxt, 65536, 65800);
  602.  
  603.         a = len(ffe_ptxt);
  604.  
  605.        
  606.         ptxt = self.ffe_cipher_ptxt(ffe_ptxt, axes, False);
  607.  
  608.         return ptxt;
  609.  
  610.  
  611.  
  612. # Main RIFC Cipher Test
  613. #____________________________________________________________
  614.  
  615.  
  616. # For future use in a ciphertext plotter
  617. def ct_rifc_plot_gen_rand_ptxt(n):
  618.     ptxt = "";
  619.     for i in range(n):
  620.         a = random.randint(0, 255);
  621.         ptxt = ptxt + chr(a);
  622.     return ptxt;
  623.  
  624. # For future use in a ciphertext plotter
  625. def ct_rifc_plot_rand_test(rifc, n):
  626.     for i in range(n):
  627.         ptxt = ct_rifc_plot_gen_rand_ptxt(random.randint(8, 128));
  628.         ciphertext = rifc.encrypt(ptxt);
  629.         dptxt = rifc.decrypt(ciphertext);
  630.  
  631.         if (ptxt != dptxt):
  632.             print("SHIT ************************ FAILED DECRYPT ********************* SHIT");
  633.  
  634.             n = len(ptxt);
  635.             for i in range(n):
  636.                 a = ord(ptxt[i]);
  637.                 b = ord(dptxt[i]);
  638.                 if (a != b):
  639.                     print("**** DOUBLE SHIT ****");
  640.  
  641.             return False;
  642.         print("i:%s\r" % (i), end="");
  643.     print("\r\n\n");
  644.     return True;
  645.  
  646.  
  647.  
  648. print("Chris M. Thomasson 8/21/2016");
  649. print("Reverse Iteration Fractal Cipher (RIFC)");
  650. print("Pre-Alpha Version: 0.0.0.1");
  651. print("In-Memory Byte Level Version");
  652. print("File based version is on its way");
  653. print("=======================================================\n\n\n");
  654.  
  655.  
  656. original_plaintext = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
  657.  
  658. fskey = ct_rifc_skey(ct_rifc_axes_2d(-0.5, 0.1, -0.87, .34),
  659.                      (-0.74543+0.11301j),
  660.                      [(.677+.2j), (-.0505+.64j), (-0.02+0.6j)],
  661.                      [3, 5, 2.314],
  662.                      2, (1.0+0.0j), "hmac_skey_123");
  663.  
  664. rifc = ct_rifc(fskey);
  665. #z^4+{.677;.2}
  666. #z^5+{-.0505;+.64}
  667.  
  668. print("rifc:%s\n\n" % (rifc));
  669.  
  670.  
  671. #ct_rifc_plot_rand_test(rifc, 20000);
  672.  
  673.  
  674. print("original_plaintext, length: %s" % (len(original_plaintext)));
  675. print("______________________________________________");
  676. print(original_plaintext);
  677. print("______________________________________________");
  678.  
  679. ciphertext = rifc.encrypt(original_plaintext);
  680. print("\n\nciphertext, length %s" % (len(ciphertext.ctxt)));
  681. print("______________________________________________");
  682. print(ciphertext);
  683. print("______________________________________________\n\n");
  684.  
  685.  
  686. # Eve can try to mess with the ciphertext,
  687. # but Bob will be alerted!  :^D
  688. #ciphertext.ctxt[0] = ciphertext.ctxt[0] + (.000000000-.000000000j);
  689. #ciphertext.ctxt[0] = (0.7530623063524057+0.5739172586889129j);
  690.  
  691. decrypted_plaintext = rifc.decrypt(ciphertext);
  692. print("decrypted_plaintext, length: %s" % (len(decrypted_plaintext)));
  693. print("______________________________________________");
  694. print(decrypted_plaintext);
  695. print("______________________________________________\n\n");
  696.  
  697.  
  698. # eof
Advertisement
Add Comment
Please, Sign In to add comment