Chris_M_Thomasson

Reverse Iteration Fractal Cipher (RIFC) w/ Random Nits

Aug 11th, 2016
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.77 KB | None | 0 0
  1. # Chris M. Thomasson 8/11/2016
  2. # Reverse Iteration Fractal Cipher (RIFC)
  3.  
  4.  
  5. import math;
  6. import random;
  7.  
  8.  
  9. # Complex Absolute Value
  10. def cabs(z):
  11.     return math.sqrt(z.real**2 + z.imag**2);
  12.  
  13.  
  14. # Complex Argument
  15. def carg(z):
  16.     return math.atan2(z.imag, z.real);
  17.  
  18.  
  19. # Complex Argument Range [0...PI2]
  20. def cargr(z):
  21.     a = math.atan2(z.imag, z.real);
  22.     if (a < 0): a += math.pi * 2;
  23.     return a;
  24.  
  25.  
  26. # Get n from a power
  27. def npow(p):
  28.     return int(math.ceil(math.fabs(p)));
  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 = npow(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. # Find Root
  49. def froot(z, r):
  50.     n = 0;
  51.     for i in r:
  52.         d = z - i;
  53.         l = math.sqrt(d.real**2 + d.imag**2);
  54.         if l < 0.000001: return n;
  55.         n = n + 1;
  56.     return -1;
  57.  
  58.  
  59. # Secret Key
  60. class ct_rifc_skey:
  61.     def __init__(self, z, c_0, c_1, p_0, p_1, n):
  62.         self.z = z;
  63.         self.c_0 = c_0;
  64.         self.c_1 = c_1;
  65.         self.p_0 = p_0;
  66.         self.p_1 = p_1;
  67.         self.n = n;
  68.  
  69.     def __repr__(self):
  70.         return "(%s, %s, %s, %s, %s, %s)" % (self.z, self.c_0, self.c_1, self.p_0, self.p_1, self.n);
  71.  
  72.     def __str__(self): return self.__repr__();
  73.  
  74.  
  75. # Reverse Iteration Fractal Cipher (RIFC)
  76. class ct_rifc:
  77.     def __init__(self):
  78.         self.foo = 1;
  79.  
  80.     def __repr__(self):
  81.         return "(%s)" % ("ct_rifc");
  82.  
  83.     def __str__(self): return self.__repr__();
  84.  
  85.     def encrypt_ptxt(self, z, c, p, ptxt):
  86.         n = 0;
  87.         for i in ptxt:
  88.             r = croots(z - c, p);
  89.             z = r[int(i)];
  90.             print("(encrypt_ptxt(z[%s]:(%s):%s)" % (n, i, z));
  91.             n = n + 1;
  92.         return z;
  93.  
  94.     def encrypt_rand(self, z, c, p, n):
  95.         np = npow(p);
  96.         for i in range(n):
  97.             r = croots(z - c, p);
  98.             z = r[random.randint(0, np - 1)];
  99.             print("(encrypt_rand(z[%s]:%s)" % (i, z));
  100.         return z;
  101.  
  102.     def decrypt_rand(self, z, c, p, n):
  103.         for i in range(n):
  104.             f = z**p + c;
  105.             print("(decrypt_rand(z[%s]:%s)" % (n - i - 1, z));
  106.             z = f;
  107.         return z;
  108.  
  109.     def decrypt_ptxt(self, z, c, p, n):
  110.         ptxt = "";
  111.         for i in range(n):
  112.             f = z**p + c;
  113.             r = croots(f - c, p);
  114.             b = froot(z, r);
  115.             ptxt += str(b);
  116.             print("(decrypt_ptxt(z[%s]:(%s):%s)" % (n - i - 1, b, z));
  117.             z = f;
  118.         ptxt = ptxt[::-1];
  119.         return [ptxt, z];
  120.  
  121.     def encrypt(self, z, skey, ptxt):
  122.         z = self.encrypt_ptxt(z, skey.c_0, skey.p_0, ptxt);
  123.         print("----------------------------");
  124.         return self.encrypt_rand(z, skey.c_1, skey.p_1, skey.n);
  125.  
  126.     def decrypt(self, z, skey, n):
  127.         z = self.decrypt_rand(z, skey.c_1, skey.p_1, skey.n);
  128.         print("----------------------------");
  129.         return self.decrypt_ptxt(z, skey.c_0, skey.p_0, n);
  130.  
  131.  
  132.  
  133.  
  134. # Main Program
  135. skey = ct_rifc_skey((0+0j), (-.75+.09j), (.1+.8j), 2, 3, 8);
  136. rifc = ct_rifc();
  137. ptxt = "01000001";
  138. n = len(ptxt);
  139.  
  140. # Display environment
  141. print("secret key: %s" % (skey));
  142. print("plaintext: %s" % (ptxt));
  143. print("__________________________________________");
  144.  
  145. # Encrypt
  146. cp = rifc.encrypt(skey.z, skey, ptxt);
  147. print("__________________________________________");
  148.  
  149. print("\ncipherpoint:%s\n" % (cp));
  150. print("__________________________________________");
  151.  
  152. # Decrypt
  153. d = rifc.decrypt(cp, skey, n);
  154. print("__________________________________________\n");
  155.  
  156. print("decrypted: %s" % (d));
  157.  
  158. if (d[0] != ptxt):
  159.     print("Data Corrupted!");
Advertisement
Add Comment
Please, Sign In to add comment