Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Chris M. Thomasson 8/20/2016
- # Reverse Iteration Fractal Cipher (RIFC)
- # Pre-Alpha Version: 0.0.0
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- import math;
- import random;
- import hmac;
- import copy;
- import os;
- # RIFC General Complex Math Utilities
- #____________________________________________________________
- # Complex Absolute Value
- def ct_cabs(z):
- return math.sqrt(z.real**2 + z.imag**2);
- # Complex Argument
- def ct_carg(z):
- return math.atan2(z.imag, z.real);
- # Complex Argument Range [0...PI2]
- def ct_cargr(z):
- a = math.atan2(z.imag, z.real);
- if (a < 0): a += math.pi * 2;
- return a;
- # Get n from a power
- def ct_npow(p):
- return int(math.ceil(math.fabs(p)));
- # Complex Roots
- def ct_croots(z, p):
- l = ct_cabs(z);
- s = l**(1.0 / p);
- a = ct_carg(z) / p;
- n = ct_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 ct_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;
- # RIFC Number Conversions and Nit Utilities
- #____________________________________________________________
- # Convert base ten 10 to nits, pad to n
- def ct_base_10_to_nits_pad(v, b, n):
- nits = "";
- r = 0;
- for i in range(n):
- d = math.floor(v / b);
- r = v - (d * b);
- nits = nits + str(r);
- v = d;
- nits = nits[::-1];
- return nits;
- # Convert base ten 10 to nits
- def ct_base_10_to_nits(v, b):
- nits = "";
- r = 0;
- while 1:
- d = math.floor(v / b);
- r = v - (d * b);
- nits = nits + str(r);
- v = d;
- if (d == 0): break;
- nits = nits[::-1];
- return nits;
- # Convert nits to base 10
- def ct_nits_to_base_10(nits, b):
- p = len(nits) - 1;
- r = 0;
- for nit in nits:
- r = r + int(nit)*b**p;
- p = p - 1;
- return r;
- # Generate n random nits in range [0...(b-1)]
- def ct_gen_rand_nits(n, b):
- nits = "";
- for i in range(n):
- nits = nits + str(random.randint(0, b - 1));
- return nits;
- def ct_number_from_ptxt_bytes_n(ptxt, o, n):
- r = 0;
- imax = len(ptxt);
- p = 0;
- for i in range(o, n + 1):
- b = ord(ptxt[i]);
- a = b * 256**p;
- r = r + a;
- #print("ni[%s]:(b:%s, r:%s)" % (i, b, r));
- p = p + 1;
- return r;
- def ct_ptxt_bytes_from_number(n):
- r = 0;
- ni = 0;
- ptxt = "";
- while 1:
- d = math.floor(n / 256);
- r = n - (d * 256);
- ptxt = ptxt + chr(r);
- n = d;
- if (d == 0): break;
- return ptxt;
- # RIFC Secret Key Class
- #____________________________________________________________
- class ct_rifc_skey:
- def __init__(self, z, c, p, r_n, n_n, hmkey):
- self.z = z;
- self.c = copy.copy(c);
- self.p = copy.copy(p);
- self.r_n = r_n;
- self.n_n = n_n;
- self.hmack = copy.copy(hmkey);
- def __repr__(self):
- return "(z:%s, c:%s, p:%s, r_n:%s, n_n:%s, hmack:%s)" % (self.z, self.c, self.p, self.r_n, self.n_n, self.hmack);
- def __str__(self): return self.__repr__();
- # RIFC Native Encrypt Class
- #____________________________________________________________
- class ct_rifc_encrypt:
- def __init__(self, skey):
- self.skey = skey; # mutable reference
- def __repr__(self):
- return "(skey:%s)" % (self.skey);
- def __str__(self): return self.__repr__();
- def process(self, z, c, p, ptxt):
- i = 0;
- for nit in ptxt:
- r = ct_croots(z - c, p);
- z = r[int(nit)];
- #print("encrypt_process:z[%s]:(%s):(%s)" % (i, nit, z));
- i = i + 1;
- return z;
- # RIFC Native Decrypt Class
- #____________________________________________________________
- class ct_rifc_decrypt:
- def __init__(self, skey):
- self.skey = skey; # mutable reference
- def __repr__(self):
- return "(skey:%s)" % (self.skey);
- def __str__(self): return self.__repr__();
- def process(self, z, c, p, n):
- ptxt = "";
- for i in range(n):
- f = z**p + c;
- r = ct_croots(f - c, p);
- nit = ct_froot(z, r);
- ptxt = ptxt + str(nit);
- #print("decrypt_process:z[%s]:(%s):(%s)" % (n - i - 1, nit, z));
- z = f;
- i = i + 1;
- ptxt = ptxt[::-1]; # reverse nits
- return [ptxt, z];
- # RIFC Block Compatible Nit Class
- #____________________________________________________________
- class ct_rifc_nitblock:
- def __init__(self, z, ptxt, ptxt_nits, ptxt_n_nits, rand_nits):
- self.z = z;
- self.ptxt = ptxt;
- self.ptxt_nits = ptxt_nits;
- self.ptxt_n_nits = ptxt_n_nits;
- self.rand_nits = rand_nits;
- def __repr__(self):
- return "(z:%s, ptxt:%s, ptxt_nits:%s, ptxt_n_nits:%s, rand_nits:%s)" % (self.z, self.ptxt, self.ptxt_nits, self.ptxt_n_nits, self.rand_nits);
- def __str__(self): return self.__repr__();
- def ct_rifc_nitblock_from_number(fskey, z, n):
- ptxt_nits = ct_base_10_to_nits(n, ct_npow(fskey.p[0]));
- ptxt_n_nits = ct_base_10_to_nits_pad(len(ptxt_nits), ct_npow(fskey.p[1]), fskey.n_n);
- rand_nits = ct_gen_rand_nits(fskey.r_n, ct_npow(fskey.p[2]));
- return ct_rifc_nitblock(z, n, ptxt_nits, ptxt_n_nits, rand_nits);
- # RIFC Block Cipher Class
- #____________________________________________________________
- class ct_rifc_block:
- def __init__(self, fskey):
- self.fskey = fskey;
- def __repr__(self):
- return "(fskey:%s)" % (self.fskey);
- def __str__(self): return self.__repr__();
- def encrypt(self, nblk):
- fencrypt = ct_rifc_encrypt(self.fskey);
- #print("\nBlock Encryption Process");
- #print("___________________________________________________");
- #print("nblk:%s" % (nblk));
- #print("--------------------------------------");
- #print("\nplaintext");
- #print("--------------------------------------");
- z = nblk.z;
- z = fencrypt.process(z, fskey.c[0], fskey.p[0], nblk.ptxt_nits);
- #print("\nlength of plaintext");
- #print("--------------------------------------");
- z = fencrypt.process(z, fskey.c[1], fskey.p[1], nblk.ptxt_n_nits);
- #print("\nrandom plaintext");
- #print("--------------------------------------");
- z = fencrypt.process(z, fskey.c[2], fskey.p[2], nblk.rand_nits);
- #print("___________________________________________________\n");
- #cpassert = hmac.new(fskey.hmack.encode());
- #cpassert.update(str(z).encode());
- #cphash = cpassert.hexdigest();
- return z;
- def decrypt(self, z):
- #dcpassert = hmac.new(self.fskey.hmack.encode());
- #dcpassert.update(str(z).encode());
- #dcphash = dcpassert.hexdigest();
- # Detect HMAC level threat...
- #if not hmac.compare_digest(zblk[1], dcphash):
- fdecrypt = ct_rifc_decrypt(self.fskey);
- #print("\nBlock Decryption Process");
- #print("___________________________________________________");
- #print("\nrandom plaintext");
- #print("--------------------------------------");
- z = fdecrypt.process(z, self.fskey.c[2], self.fskey.p[2], self.fskey.r_n);
- rand_nits = z[0];
- #print("\nlength of plaintext");
- #print("--------------------------------------");
- z = fdecrypt.process(z[1], self.fskey.c[1], self.fskey.p[1], self.fskey.n_n);
- dp_n = ct_nits_to_base_10(z[0], ct_npow(self.fskey.p[1]));
- ptxt_n_nits = z[0];
- #print("\nplaintext");
- #print("--------------------------------------");
- z = fdecrypt.process(z[1], self.fskey.c[0], self.fskey.p[0], dp_n);
- ptxt_nits = z[0];
- #print("___________________________________________________\n");
- nblk = ct_rifc_nitblock(z[1], ct_nits_to_base_10(ptxt_nits, ct_npow(self.fskey.p[0])), ptxt_nits, ptxt_n_nits, rand_nits);
- return nblk;
- # RIFC Ciphertext Class
- #____________________________________________________________
- class ct_rifc_ciphertext:
- def __init__(self, hmhash, ctxt):
- self.hmhash = hmhash;
- self.ctxt = ctxt;
- def __repr__(self):
- s = ("(%s)\n" % (self.hmhash));
- for cp in self.ctxt: s = s + ("%s\n" % (cp));
- return s;
- def __str__(self): return self.__repr__();
- def hmack_assert(self, hmhash_compare):
- return hmac.compare_digest(self.hmhash, hmhash_compare);
- # Main RIFC Cipher Class
- #____________________________________________________________
- class ct_rifc:
- def __init__(self, fskey):
- self.fskey = fskey;
- def __repr__(self):
- return "(fskey:%s)" % (self.fskey);
- def __str__(self): return self.__repr__();
- def encrypt(self, ptxt):
- i = 0;
- n = len(ptxt);
- rnmax = 2;
- nsum = 0;
- ciphertext = [];
- hmassert = hmac.new(self.fskey.hmack.encode());
- rifc_block = ct_rifc_block(self.fskey);
- z = self.fskey.z;
- while (i < n):
- r = random.randint(0, rnmax);
- o = i;
- s = o + r;
- if (s >= n): s = n - 1;
- ptn = ct_number_from_ptxt_bytes_n(ptxt, o, s);
- nblk = ct_rifc_nitblock_from_number(fskey, z, ptn);
- cipherpoint = rifc_block.encrypt(nblk);
- hmassert.update(str(cipherpoint).encode());
- ciphertext.append(cipherpoint);
- z = cipherpoint;
- i = s + 1;
- hmhash = hmassert.hexdigest();
- return ct_rifc_ciphertext(hmhash, ciphertext);
- def decrypt(self, ctxt):
- ptxt = "";
- rifc_block = ct_rifc_block(self.fskey);
- hmassert = hmac.new(self.fskey.hmack.encode());
- # check HMAC
- for cp in ctxt.ctxt:
- hmassert.update(str(cp).encode());
- hmhash = hmassert.hexdigest();
- if (not ctxt.hmack_assert(hmhash)):
- print("\n\n************** !! HMAC VOILATION !! ****************\n\n");
- return "Bob is 0xDEADBEEF!!!";
- for cp in ctxt.ctxt:
- nblk = rifc_block.decrypt(cp);
- ptxt = ptxt + ct_ptxt_bytes_from_number(nblk.ptxt);
- return ptxt;
- # Main RIFC Cipher Test
- #____________________________________________________________
- # For future use in a ciphertext plotter
- def ct_rifc_plot_gen_rand_ptxt(n):
- ptxt = "";
- for i in range(n):
- a = random.randint(32, 126);
- ptxt = ptxt + chr(a);
- return ptxt;
- # For future use in a ciphertext plotter
- def ct_rifc_plot_rand_test(rifc, n):
- for i in range(n):
- ptxt = ct_rifc_plot_gen_rand_ptxt(random.randint(8, 128));
- ciphertext = rifc.encrypt(ptxt);
- dptxt = rifc.decrypt(ciphertext);
- if (ptxt != dptxt):
- print("SHIT ************************ FAILED DECRYPT ********************* SHIT");
- return False;
- print("i:%s\r" % (i), end="");
- print("\r\n\n");
- return True;
- print("Chris M. Thomasson 8/20/2016");
- print("Reverse Iteration Fractal Cipher (RIFC)");
- print("Pre-Alpha Version: 0.0.0");
- print("In-Memory Byte Level Version");
- print("File based version is on its way");
- print("=======================================================\n\n\n");
- fskey = ct_rifc_skey((-0.74543+0.11301j), [(.677+.2j), (-.21-.751j), (-.0505+.64j)], [4, 3, 5], 2, 3, "hmac_skey_123");
- rifc = ct_rifc(fskey);
- print("rifc:%s\n\n" % (rifc));
- #original_plaintext = "My name is Chris M. Thomasson,\nthis was encrypted using\nmy cipher called (RIFC)!";
- original_plaintext = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
- #ct_rifc_plot_rand_test(rifc, 20000);
- print("original_plaintext, length: %s" % (len(original_plaintext)));
- print("______________________________________________");
- print(original_plaintext);
- print("______________________________________________");
- ciphertext = rifc.encrypt(original_plaintext);
- print("\n\nciphertext, length %s" % (len(ciphertext.ctxt)));
- print("______________________________________________");
- print(ciphertext);
- print("______________________________________________\n\n");
- # Eve can try to mess with the ciphertext,
- # but Bob will be alerted! :^D
- ciphertext.ctxt[0] = ciphertext.ctxt[0] + (.000000000-.000000000j);
- decrypted_plaintext = rifc.decrypt(ciphertext);
- print("decrypted_plaintext, length: %s" % (len(decrypted_plaintext)));
- print("______________________________________________");
- print(decrypted_plaintext);
- print("______________________________________________\n\n");
- # eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement