Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Chris M. Thomasson 7/31/2016
- # Reverse Fractal Iteration N-Ary Encoding with Random Offset, and Binary Threat Detection...
- # Does NOT work with fractional powers yet!
- # Thats coming...
- import math;
- import random;
- # Complex Absolute Value
- def cabs(z):
- return math.sqrt(z.real**2 + z.imag**2);
- # Complex Argument
- def carg(z):
- return math.atan2(z.imag, z.real);
- # Generate Random Complex Number
- def randc(nmin, nmax):
- d = nmax - nmin;
- x = nmin + random.random() * d;
- y = nmin + random.random() * d;
- return complex(x, y);
- # Complex Roots
- def croots(z, p):
- l = cabs(z);
- s = l**(1.0 / p);
- a = carg(z) / p;
- n = math.ceil(math.fabs(p));
- astep = (math.pi * 2.0) / p;
- result = [];
- for i in range(n):
- r = complex(math.cos(a + astep * i) * s,
- math.sin(a + astep * i) * s);
- result.append(r);
- return result;
- # Find Root
- def froot(z, r):
- n = 0;
- for i in r:
- d = z - i;
- l = math.sqrt(d.real**2 + d.imag**2);
- if l < .000001: return n;
- n = n + 1;
- return -1;
- # Encrypt N-Ary Stream
- def encrypt_nary(z, c, p, s, pt):
- n = 0;
- for i in pt:
- r = croots(z - c, p);
- z = r[int(i)];
- a = carg(z);
- print("z[" + str(n) + "]:(" + i + "):" + str(z) + ", a:" + str(a));
- n = n + 1;
- z += s;
- print("----------------------");
- print("Ciphertext:" + str(z));
- return z;
- # Decrypt N-Ary Stream
- def decrypt_nary(z, c, p, s, n):
- pt = "";
- print("Ciphertext:" + str(ct));
- print("----------------------");
- z -= s;
- for i in range(n):
- f = z**p + c;
- r = croots(f - c, p);
- b = froot(z, r);
- pt += str(b);
- a = carg(z);
- print("z[" + str(n - i - 1) + "]:(" + str(b) + "):" + str(z) + ", a:" + str(a));
- z = f;
- pt = pt[::-1]; # reverse symbols
- return [pt, z];
- # Estimate Threat Level of a CipherPoint
- # Boolean for now...
- def cipherpoint_assert(z0, z1, eps):
- d = cabs(z1 - z0);
- if d > eps:
- ed = (d - eps) / eps;
- return True;
- return False;
- # The Secret Key
- c = (-.75+.09j);
- z = (0+0j);
- p = 5;
- s = (0+0j); #randc(-9999999, 9999999); # this alters epsilon.
- # The Plaintext
- pt0 = "0123443210";
- n = len(pt0);
- # Display our environment.
- print("c:" + str(c));
- print("z:" + str(z));
- print("p:" + str(p));
- print("s:" + str(s));
- print("n:" + str(n));
- print("pt0:" + str(pt0));
- print("");print("");
- print("Encrypt " + str(p) + "-Ary Stream: " + str(n) + " Iterations");
- print("_________________________________________");
- # The Ciphertext
- ct = encrypt_nary(z, c, p, s, pt0);
- print("_________________________________________");
- print("\n");
- print("Decrypt " + str(p) + "-Ary Stream: " + str(n) + " Iterations");
- print("_________________________________________");
- # The Decrypted Plaintext
- # Just try to alter the cipherpoint Eve! It will alert Bob...
- ct += (0.00000000+.000000000j);
- pt1 = decrypt_nary(ct, c, p, s, n);
- print("_________________________________________");
- print(""); print("");
- # Caculate our threat level...
- threat_level = cipherpoint_assert(pt1[1], z, .000001);
- print("*** threat_level *** = " + str(threat_level));
- print(""); print("");
- # Verify Data Integrity
- print("pt1[0]:" + pt1[0]);
- if pt0 != pt1[0] or threat_level: print("DATA CORRUPTED!!! ;^o");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement