Advertisement
Chris_M_Thomasson

Reverse Iteration Fractal Cipher (RIFC) w/ HMAC

Aug 11th, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.29 KB | None | 0 0
  1. # Chris M. Thomasson 8/11/2016
  2. # Reverse Iteration Fractal Cipher (RIFC) w/ HMAC
  3.  
  4.  
  5. import math;
  6. import random;
  7. import hmac;
  8. import copy;
  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. # Complex Argument Range [0...PI2]
  22. def cargr(z):
  23.     a = math.atan2(z.imag, z.real);
  24.     if (a < 0): a += math.pi * 2;
  25.     return a;
  26.  
  27.  
  28. # Get n from a power
  29. def npow(p):
  30.     return int(math.ceil(math.fabs(p)));
  31.  
  32.  
  33. # Complex Roots
  34. def croots(z, p):
  35.     l = cabs(z);
  36.     s = l**(1.0 / p);
  37.     a = carg(z) / p;
  38.     n = npow(p);
  39.     astep = (math.pi * 2.0) / p;
  40.     result = [];
  41.  
  42.     for i in range(n):
  43.         r = complex(math.cos(a + astep * i) * s,
  44.                     math.sin(a + astep * i) * s);
  45.         result.append(r);
  46.  
  47.     return result;
  48.  
  49.  
  50. # Find Root
  51. def froot(z, r):
  52.     n = 0;
  53.     for i in r:
  54.         d = z - i;
  55.         l = math.sqrt(d.real**2 + d.imag**2);
  56.         if l < 0.000001: return n;
  57.         n = n + 1;
  58.     return -1;
  59.  
  60.  
  61. # Secret Key
  62. class ct_rifc_skey:
  63.     def __init__(self, z, c_0, c_1, p_0, p_1, n, hmkey):
  64.         self.z = z;
  65.         self.c_0 = c_0;
  66.         self.c_1 = c_1;
  67.         self.p_0 = p_0;
  68.         self.p_1 = p_1;
  69.         self.n = n;
  70.         self.hmack = copy.copy(hmkey);
  71.  
  72.     def __repr__(self):
  73.         return "(%s, %s, %s, %s, %s, %s)" % (self.z, self.c_0, self.c_1, self.p_0, self.p_1, self.n);
  74.  
  75.     def __str__(self): return self.__repr__();
  76.  
  77.  
  78. # Reverse Iteration Fractal Cipher (RIFC)
  79. class ct_rifc:
  80.     def __init__(self):
  81.         self.foo = 1;
  82.  
  83.     def __repr__(self):
  84.         return "(%s)" % ("ct_rifc");
  85.  
  86.     def __str__(self): return self.__repr__();
  87.  
  88.     def encrypt_ptxt(self, z, c, p, ptxt):
  89.         n = 0;
  90.         for i in ptxt:
  91.             r = croots(z - c, p);
  92.             z = r[int(i)];
  93.             print("(encrypt_ptxt(z[%s]:(%s):%s)" % (n, i, z));
  94.             n = n + 1;
  95.         return z;
  96.  
  97.     def encrypt_rand(self, z, c, p, n):
  98.         np = npow(p);
  99.         for i in range(n):
  100.             r = croots(z - c, p);
  101.             z = r[random.randint(0, np - 1)];
  102.             print("(encrypt_rand(z[%s]:%s)" % (i, z));
  103.         return z;
  104.  
  105.     def decrypt_rand(self, z, c, p, n):
  106.         for i in range(n):
  107.             f = z**p + c;
  108.             print("(decrypt_rand(z[%s]:%s)" % (n - i - 1, z));
  109.             z = f;
  110.         return z;
  111.  
  112.     def decrypt_ptxt(self, z, c, p, n):
  113.         ptxt = "";
  114.         for i in range(n):
  115.             f = z**p + c;
  116.             r = croots(f - c, p);
  117.             b = froot(z, r);
  118.             ptxt += str(b);
  119.             print("(decrypt_ptxt(z[%s]:(%s):%s)" % (n - i - 1, b, z));
  120.             z = f;
  121.         ptxt = ptxt[::-1];
  122.         return [ptxt, z];
  123.  
  124.     def encrypt(self, z, skey, ptxt):
  125.         z = self.encrypt_ptxt(z, skey.c_0, skey.p_0, ptxt);
  126.         print("----------------------------");
  127.         return self.encrypt_rand(z, skey.c_1, skey.p_1, skey.n);
  128.  
  129.     def decrypt(self, z, skey, n):
  130.         z = self.decrypt_rand(z, skey.c_1, skey.p_1, skey.n);
  131.         print("----------------------------");
  132.         return self.decrypt_ptxt(z, skey.c_0, skey.p_0, n);
  133.  
  134.  
  135.  
  136.  
  137. # Main Program
  138. skey = ct_rifc_skey((0+0j), (-.75+.09j), (.1+.8j), 2, 3, 8, "hmac_secret_key");
  139. rifc = ct_rifc();
  140.  
  141. ptxt = "01000001";
  142. n = len(ptxt);
  143.  
  144. # Display environment
  145. print("secret key: %s" % (skey));
  146. print("plaintext: %s" % (ptxt));
  147. print("__________________________________________");
  148.  
  149. # Encrypt
  150. cp = rifc.encrypt(skey.z, skey, ptxt);
  151. cpassert = hmac.new(skey.hmack.encode());
  152. cpassert.update(str(cp).encode());
  153. cphash = cpassert.hexdigest();
  154. print("__________________________________________");
  155.  
  156. print("\ncipherhash:%s" % (cphash));
  157. print("cipherpoint:%s\n" % (cp));
  158. print("__________________________________________");
  159.  
  160. # Decrypt
  161. # Eve can try to alter the cipherpoint, but it will alert Bob!  :^)
  162. cp += (0+0j);
  163. d = rifc.decrypt(cp, skey, n);
  164. print("__________________________________________\n");
  165.  
  166. dcpassert = hmac.new(skey.hmack.encode());
  167. dcpassert.update(str(cp).encode());
  168. dcphash = dcpassert.hexdigest();
  169.  
  170. print("cipherhash: %s" % (dcphash));
  171. print("decrypted: %s" % (d));
  172.  
  173. if (d[0] != ptxt or not hmac.compare_digest(cphash, dcphash)):
  174.     print("Data Corrupted!");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement