Chris_M_Thomasson

RIFC Cipherpoint Plotter 8/13/2016 by Chris M. Thomasson

Aug 13th, 2016
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.41 KB | None | 0 0
  1. # RIFC CipherPoint Plotter 8/13/2016 by Chris M. Thomasson
  2.  
  3.  
  4. import math;
  5. import random;
  6. import copy; # for non-mutable aspects of classes
  7. #import os;
  8.  
  9. from PIL import Image;
  10.  
  11.  
  12.  
  13.  
  14. # Complex Absolute Value
  15. def cabs(z):
  16.     return math.sqrt(z.real**2 + z.imag**2);
  17.  
  18.  
  19. # Complex Argument
  20. def carg(z):
  21.     return math.atan2(z.imag, z.real);
  22.  
  23.  
  24. # Complex Argument Range [0...PI2]
  25. def cargr(z):
  26.     a = math.atan2(z.imag, z.real);
  27.     if (a < 0): a += math.pi * 2;
  28.     return a;
  29.  
  30.  
  31. # Complex Roots
  32. def croots(z, p):
  33.     l = cabs(z);
  34.     s = l**(1.0 / p);
  35.     a = carg(z) / p;
  36.     n = int(math.ceil(math.fabs(p)));
  37.     astep = (math.pi * 2.0) / p;
  38.     result = [];
  39.  
  40.     for i in range(n):
  41.         r = complex(math.cos(a + astep * i) * s,
  42.                     math.sin(a + astep * i) * s);
  43.         result.append(r);
  44.  
  45.     return result;
  46.  
  47.  
  48. # 2d Axes
  49. class ct_axes_2d:
  50.     def __init__(self, xmin, xmax, ymin, ymax):
  51.         self.xmin = xmin;
  52.         self.xmax = xmax;
  53.         self.ymin = ymin;
  54.         self.ymax = ymax;
  55.  
  56.     def __repr__(self):
  57.         return "(%s, %s, %s, %s)" % (self.xmin, self.xmax, self.ymin, self.ymax)
  58.  
  59.     def __str__(self): return self.__repr__();
  60.  
  61.     def width(self): return  self.xmax - self.xmin;
  62.     def height(self): return  self.ymax - self.ymin;
  63.  
  64.  
  65. # 2d Plane
  66. class ct_plane_2d:
  67.     def __init__(self, width, height, axes):
  68.         self.axes = copy.copy(axes);
  69.         self.width = width;
  70.         self.height = height;
  71.         self.xstep = self.axes.width() / width;
  72.         self.ystep = self.axes.height() / height;
  73.         self.aratio = height / width;
  74.  
  75.     def __repr__(self):
  76.         return "(%s, %s, %s, %s, %s, %s)" % (self.axes,
  77.                                              self.width, self.height,
  78.                                              self.xstep, self.ystep,
  79.                                              self.aratio);
  80.  
  81.     def __str__(self): return self.__repr__();
  82.  
  83.     def project_to(self, p):
  84.         x = math.floor((p.real - self.axes.xmin) / self.xstep * self.aratio);
  85.         y = math.floor((self.axes.ymax - p.imag) / self.ystep);
  86.         return complex(x, y);
  87.  
  88.  
  89. # RIFC Plotter
  90. class ct_rjulia:
  91.     def __init__(self, plane):
  92.         self.plane = copy.copy(plane);
  93.         self.image = Image.new("RGB", (plane.width, plane.height));
  94.         self.pixels = self.image.load();
  95.  
  96.     def __repr__(self):
  97.         return "(%s)" % (self.plane);
  98.  
  99.     def __str__(self): return self.__repr__();
  100.  
  101.     def save(self, fname):
  102.         self.image.save(fname, "BMP");
  103.  
  104.     def set_pixel(self, p, c):
  105.         if (p.real < 0 or p.real >= self.plane.width or
  106.             p.imag < 0 or p.imag >= self.plane.height):
  107.             return False;
  108.         self.pixels[p.real, p.imag] = c;
  109.         return True;
  110.  
  111.     def set_point(self, p, c):
  112.         pp = self.plane.project_to(p);
  113.         return self.set_pixel(pp, c);
  114.  
  115.     def compute(self, z, c, p, n):
  116.         for i in range(n):
  117.             r = croots(z - c, p);
  118.             z = r[random.randint(0, len(r) - 1)];
  119.         return z;
  120.  
  121.     def plot(self, skey, n, ptn, rndn):
  122.         z = skey.z;
  123.         for i in range(n):
  124.             # encrypt ptn plaintext nits
  125.             z = self.compute(z, skey.c_0, skey.p_0, ptn);
  126.  
  127.             # encrypt rndn random nits using z from plaintext encryption...
  128.             z = self.compute(z, skey.c_1, skey.p_1, rndn);
  129.  
  130.             # plot the cipherpoint!
  131.             self.set_point(z, (255, 255, 255));
  132.  
  133.             print("ct_rjulia::plot(%s of %s)\r" % (i, n), end="");
  134.         print("");
  135.  
  136.  
  137.  
  138.  
  139. # Secret Key
  140. class ct_rifc_skey:
  141.     def __init__(self, z, c_0, c_1, p_0, p_1, n, hmkey):
  142.         self.z = z;
  143.         self.c_0 = c_0;
  144.         self.c_1 = c_1;
  145.         self.p_0 = p_0;
  146.         self.p_1 = p_1;
  147.         self.n = n;
  148.         self.hmack = copy.copy(hmkey);
  149.  
  150.     def __repr__(self):
  151.         return "(%s, %s, %s, %s, %s, %s, %s)" % (self.z, self.c_0, self.c_1, self.p_0, self.p_1, self.n, self.hmack);
  152.  
  153.     def __str__(self): return self.__repr__();
  154.  
  155.  
  156.  
  157.  
  158. # Main Program
  159. rfrac = ct_rjulia(ct_plane_2d(640, 640, ct_axes_2d(-2, 2, -2, 2)));
  160. skey = ct_rifc_skey((0+0j), (-.75+.09j), (.1+.8j), 2, 3, 8, "hmac_secret_key");
  161.  
  162. print("RIFC Cipherpoint Plotter 8/13/2016 by Chris M. Thomasson\n");
  163. print("rfrac:%s" % (rfrac));
  164. print("skey:%s" % (skey));
  165.  
  166. rfrac.plot(skey, 12048, 8, 8);
  167. rfrac.save("output.bmp");
  168.  
  169.  
  170. #os.system("mspaint output.bmp");
Advertisement
Add Comment
Please, Sign In to add comment