Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Chris M. Thomasson 8/11/2016
- # Reverse Iteration Fractal Cipher (RIFC)
- 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);
- # Complex Argument Range [0...PI2]
- def cargr(z):
- a = math.atan2(z.imag, z.real);
- if (a < 0): a += math.pi * 2;
- return a;
- # Get n from a power
- def npow(p):
- return int(math.ceil(math.fabs(p)));
- # Complex Roots
- def croots(z, p):
- l = cabs(z);
- s = l**(1.0 / p);
- a = carg(z) / p;
- n = npow(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 < 0.000001: return n;
- n = n + 1;
- return -1;
- # Secret Key
- class ct_rifc_skey:
- def __init__(self, z, c_0, c_1, p_0, p_1, n):
- self.z = z;
- self.c_0 = c_0;
- self.c_1 = c_1;
- self.p_0 = p_0;
- self.p_1 = p_1;
- self.n = n;
- def __repr__(self):
- return "(%s, %s, %s, %s, %s, %s)" % (self.z, self.c_0, self.c_1, self.p_0, self.p_1, self.n);
- def __str__(self): return self.__repr__();
- # Reverse Iteration Fractal Cipher (RIFC)
- class ct_rifc:
- def __init__(self):
- self.foo = 1;
- def __repr__(self):
- return "(%s)" % ("ct_rifc");
- def __str__(self): return self.__repr__();
- def encrypt_ptxt(self, z, c, p, ptxt):
- n = 0;
- for i in ptxt:
- r = croots(z - c, p);
- z = r[int(i)];
- print("(encrypt_ptxt(z[%s]:(%s):%s)" % (n, i, z));
- n = n + 1;
- return z;
- def encrypt_rand(self, z, c, p, n):
- np = npow(p);
- for i in range(n):
- r = croots(z - c, p);
- z = r[random.randint(0, np - 1)];
- print("(encrypt_rand(z[%s]:%s)" % (i, z));
- return z;
- def decrypt_rand(self, z, c, p, n):
- for i in range(n):
- f = z**p + c;
- print("(decrypt_rand(z[%s]:%s)" % (n - i - 1, z));
- z = f;
- return z;
- def decrypt_ptxt(self, z, c, p, n):
- ptxt = "";
- for i in range(n):
- f = z**p + c;
- r = croots(f - c, p);
- b = froot(z, r);
- ptxt += str(b);
- print("(decrypt_ptxt(z[%s]:(%s):%s)" % (n - i - 1, b, z));
- z = f;
- ptxt = ptxt[::-1];
- return [ptxt, z];
- def encrypt(self, z, skey, ptxt):
- z = self.encrypt_ptxt(z, skey.c_0, skey.p_0, ptxt);
- print("----------------------------");
- return self.encrypt_rand(z, skey.c_1, skey.p_1, skey.n);
- def decrypt(self, z, skey, n):
- z = self.decrypt_rand(z, skey.c_1, skey.p_1, skey.n);
- print("----------------------------");
- return self.decrypt_ptxt(z, skey.c_0, skey.p_0, n);
- # Main Program
- skey = ct_rifc_skey((0+0j), (-.75+.09j), (.1+.8j), 2, 3, 8);
- rifc = ct_rifc();
- ptxt = "01000001";
- n = len(ptxt);
- # Display environment
- print("secret key: %s" % (skey));
- print("plaintext: %s" % (ptxt));
- print("__________________________________________");
- # Encrypt
- cp = rifc.encrypt(skey.z, skey, ptxt);
- print("__________________________________________");
- print("\ncipherpoint:%s\n" % (cp));
- print("__________________________________________");
- # Decrypt
- d = rifc.decrypt(cp, skey, n);
- print("__________________________________________\n");
- print("decrypted: %s" % (d));
- if (d[0] != ptxt):
- print("Data Corrupted!");
Advertisement
Add Comment
Please, Sign In to add comment