Chris_M_Thomasson

Reverse Fractal Iteration N-Ary Encoding with Random Offset

Jul 22nd, 2016
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. # Chris M. Thomasson 7/22/2016
  2. # Reverse Fractal Iteration N-Ary Encoding with Random Offset
  3. # Does NOT work with fractional powers yet!
  4. # Thats coming...
  5.  
  6.  
  7. import math;
  8. import random;
  9.  
  10.  
  11. # Complex Absolute Value
  12. def cabs(z):
  13.     return math.sqrt(z.real**2 + z.imag**2);
  14.  
  15.  
  16. # Complex Argument
  17. def carg(z):
  18.     return math.atan2(z.imag, z.real);
  19.  
  20.  
  21. # Generate Random Complex Number
  22. def randc(nmin, nmax):
  23.     d = nmax - nmin;
  24.     x = nmin + random.random() * d;
  25.     y = nmin + random.random() * d;
  26.     return complex(x, y);
  27.  
  28.  
  29. # Complex Roots
  30. def croots(z, p):
  31.     l = cabs(z);
  32.     s = l**(1.0 / p);
  33.     a = carg(z) / p;
  34.     n = math.ceil(math.fabs(p));
  35.     astep = (math.pi * 2.0) / p;
  36.     result = [];
  37.  
  38.     for i in range(n):
  39.         r = complex(math.cos(a + astep * i) * s,
  40.                     math.sin(a + astep * i) * s);
  41.         result.append(r);
  42.  
  43.     return result;
  44.  
  45.  
  46. # Find Root
  47. def froot(z, r):
  48.     n = 0;
  49.     for i in r:
  50.         d = z - i;
  51.         l = math.sqrt(d.real**2 + d.imag**2);
  52.         if l < 0.000001: return n;
  53.         n = n + 1;
  54.     return -1;
  55.  
  56.  
  57. # Encrypt N-Ary Stream
  58. def encrypt_nary(z, c, p, s, pt):
  59.     n = 0;
  60.     for i in pt:
  61.         r = croots(z - c, p);
  62.         z = r[int(i)];
  63.         a = carg(z);
  64.         print("z[" + str(n) + "]:(" + i + "):" + str(z) + ", a:" + str(a));
  65.         n = n + 1;
  66.     z += s;
  67.     print("----------------------");
  68.     print("Ciphertext:" + str(z));
  69.     return z;
  70.  
  71. # Decrypt N-Ary Stream
  72. def decrypt_nary(z, c, p, s, n):
  73.     pt = "";
  74.     print("Ciphertext:" + str(ct));
  75.     print("----------------------");
  76.     z -= s;
  77.     for i in range(n):
  78.         f = z**p + c;
  79.         r = croots(f - c, p);
  80.         b = froot(z, r);
  81.         pt += str(b);
  82.         a = carg(z);
  83.         print("z[" + str(n - i - 1) + "]:(" + str(b) + "):" + str(z) + ", a:" + str(a));
  84.         z = f;
  85.     pt = pt[::-1]; # reverse symbols
  86.     return pt;
  87.        
  88.          
  89.  
  90.  
  91. # The Secret Key
  92. c = (-.75+.09j);
  93. z = (0+0j);
  94. p = 5;
  95. s = randc(-9999999, 9999999);
  96.  
  97. # The Plaintext
  98. pt0 = "0123443210";
  99. n = len(pt0);
  100.  
  101. # Display our environment.
  102. print("c:" + str(c));
  103. print("z:" + str(z));
  104. print("p:" + str(p));
  105. print("s:" + str(s));
  106. print("n:" + str(n));
  107. print("pt0:" + str(pt0));
  108. print("");print("");
  109.  
  110.  
  111.  
  112. print("Encrypt " + str(p) + "-Ary Stream: " + str(n) + " Iterations");
  113. print("_________________________________________");
  114.  
  115. # The Ciphertext
  116. ct = encrypt_nary(z, c, p, s, pt0);
  117.  
  118. print("_________________________________________");
  119. print("\n");
  120.  
  121.  
  122. print("Decrypt " + str(p) + "-Ary Stream: " + str(n) + " Iterations");
  123. print("_________________________________________");
  124.  
  125. # The Decrypted Plaintext
  126. pt1 = decrypt_nary(ct, c, p, s, n);
  127.  
  128. print("_________________________________________");
  129. print("");
  130.  
  131.  
  132.  
  133. # Verify Data Integrity
  134. print("pt1:" + pt1);
  135. if pt0 != pt1: print("DATA CORRUPTED!!! ;^o");
Advertisement
Add Comment
Please, Sign In to add comment