Advertisement
Chris_M_Thomasson

RIFC N-Ary Encoding with Threat Detection...

Jul 31st, 2016
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1. # Chris M. Thomasson 7/31/2016
  2. # Reverse Fractal Iteration N-Ary Encoding with Random Offset, and Binary Threat Detection...
  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 < .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, z];
  87.  
  88. # Estimate Threat Level of a CipherPoint
  89. # Boolean for now...
  90. def cipherpoint_assert(z0, z1, eps):
  91.     d = cabs(z1 - z0);
  92.     if d > eps:
  93.         ed = (d - eps) / eps;
  94.         return True;
  95.     return False;
  96.        
  97.          
  98.  
  99.  
  100. # The Secret Key
  101. c = (-.75+.09j);
  102. z = (0+0j);
  103. p = 5;
  104. s = (0+0j); #randc(-9999999, 9999999); # this alters epsilon.
  105.  
  106. # The Plaintext
  107. pt0 = "0123443210";
  108. n = len(pt0);
  109.  
  110. # Display our environment.
  111. print("c:" + str(c));
  112. print("z:" + str(z));
  113. print("p:" + str(p));
  114. print("s:" + str(s));
  115. print("n:" + str(n));
  116. print("pt0:" + str(pt0));
  117. print("");print("");
  118.  
  119.  
  120.  
  121. print("Encrypt " + str(p) + "-Ary Stream: " + str(n) + " Iterations");
  122. print("_________________________________________");
  123.  
  124. # The Ciphertext
  125. ct = encrypt_nary(z, c, p, s, pt0);
  126.  
  127. print("_________________________________________");
  128. print("\n");
  129.  
  130.  
  131. print("Decrypt " + str(p) + "-Ary Stream: " + str(n) + " Iterations");
  132. print("_________________________________________");
  133.  
  134. # The Decrypted Plaintext
  135.  
  136. # Just try to alter the cipherpoint Eve! It will alert Bob...
  137. ct += (0.00000000+.000000000j);
  138. pt1 = decrypt_nary(ct, c, p, s, n);
  139.  
  140. print("_________________________________________");
  141. print(""); print("");
  142.  
  143. # Caculate our threat level...
  144. threat_level = cipherpoint_assert(pt1[1], z, .000001);
  145. print("*** threat_level *** = " + str(threat_level));
  146. print(""); print("");
  147.  
  148.  
  149. # Verify Data Integrity
  150. print("pt1[0]:" + pt1[0]);
  151. if pt0 != pt1[0] or threat_level: print("DATA CORRUPTED!!! ;^o");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement