Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Chris M. Thomasson 9/24/2018
- # Experimental HMAC Cipher
- import random;
- import hmac;
- # Some Utilities
- #____________________________________________________________
- def ct_bytes_to_hex(origin, offset):
- hex = "";
- n = len(origin);
- t = "0123456789ABCDEF";
- for i in range(offset, n):
- c = ord(origin[i]);
- nibl = c & 0x0F;
- nibh = (c & 0xF0) >> 4;
- hex = hex + t[nibh];
- hex = hex + t[nibl];
- hex = hex + " ";
- if (not ((i + 1) % 16) and i != n - 1):
- hex = hex + "\r\n";
- return hex;
- # Generate n random bytes
- # These can be from a truly random, non-repeatable source.
- def ct_rand_bytes(n):
- rb = "";
- for i in range(n):
- rb = rb + chr(random.randint(0, 255));
- return rb;
- # The Secret Key
- #____________________________________________________________
- class ct_secret_key:
- def __init__(self, hmac_pword, pbyte_pword, pbyte, crand_bytes, rounds):
- self.hmac_pword = hmac_pword;
- self.pbyte_pword = pbyte_pword;
- self.pbyte = pbyte;
- self.crand_bytes = crand_bytes;
- self.rounds = rounds;
- def __repr__(self):
- return "hmac_pword:%s\npbyte_pword:%s\npbyte:%s\ncrand_bytes:%s\nrounds:%s" % (self.hmac_pword, self.pbyte_pword, self.pbyte, self.crand_bytes, self.rounds);
- def __str__(self): return self.__repr__();
- # The Plaintest
- #____________________________________________________________
- class ct_plaintext:
- def __init__(self, ptxt):
- self.bytes = ptxt;
- def __repr__(self):
- return "bytes:%s" % (ct_bytes_to_hex(self.bytes, 0));
- def __str__(self): return self.__repr__();
- # The Ciphertext
- #____________________________________________________________
- class ct_ciphertext:
- def __init__(self, crand, ctxt):
- self.crand = crand;
- self.bytes = ctxt;
- def __repr__(self):
- return "iv:%s\nbytes:%s" % (ct_bytes_to_hex(self.crand, 0), ct_bytes_to_hex(self.bytes, 0));
- def __str__(self): return self.__repr__();
- # Encrypt Round
- #____________________________________________________________
- def ct_encrypt_round(skey, crand, ptxt):
- ctxt = "";
- n = len(ptxt.bytes);
- pbyte_len = len(skey.pbyte_pword);
- h = hmac.new((crand + skey.hmac_pword).encode());
- h.update((skey.hmac_pword + crand).encode());
- i = 0;
- pbyte = ord(skey.pbyte_pword[skey.pbyte % pbyte_len]);
- while (i < n):
- # Grab HMAC buffer
- hbytes = h.digest();
- hn = len(hbytes);
- hi = 0;
- while (i < n and hi < hn):
- p = ord(ptxt.bytes[i]);
- c = p ^ hbytes[hi];
- c = c ^ pbyte;
- pbyte = c ^ ord(skey.pbyte_pword[i % pbyte_len]);
- h.update(ptxt.bytes[i].encode());
- h.update(chr(c).encode());
- ctxt = ctxt + chr(c);
- i = i + 1;
- hi = hi + 1;
- return ct_ciphertext(crand, ctxt);
- # Decrypt Round
- #____________________________________________________________
- def ct_decrypt_round(skey, ctxt):
- ptxt = "";
- n = len(ctxt.bytes);
- pbyte_len = len(skey.pbyte_pword);
- # Initialize HMAC for this session
- h = hmac.new((ctxt.crand + skey.hmac_pword).encode());
- h.update((skey.hmac_pword + ctxt.crand).encode());
- i = 0;
- pbyte = ord(skey.pbyte_pword[skey.pbyte % pbyte_len]);
- while (i < n):
- hbytes = h.digest();
- hn = len(hbytes);
- hi = 0;
- while (i < n and hi < hn):
- c = ord(ctxt.bytes[i]);
- p = c ^ hbytes[hi];
- p = p ^ pbyte;
- pbyte = c ^ ord(skey.pbyte_pword[i % pbyte_len]);
- h.update(chr(p).encode());
- h.update(chr(c).encode());
- ptxt = ptxt + chr(p);
- i = i + 1;
- hi = hi + 1;
- return ct_plaintext(ptxt);
- # Encrypt
- #____________________________________________________________
- def ct_encrypt(skey, ptxt):
- crand = ct_rand_bytes(skey.crand_bytes); # n bytes of random iv
- for i in range(skey.rounds):
- ctxt = ct_encrypt_round(skey, crand, ptxt);
- ptxt.bytes = ctxt.bytes[::-1];
- ctxt = ct_encrypt_round(skey, crand, ptxt);
- return ctxt;
- def ct_decrypt(skey, ctxt):
- for i in range(skey.rounds):
- ptxt = ct_decrypt_round(skey, ctxt);
- ctxt.bytes = ptxt.bytes[::-1];
- ptxt = ct_decrypt_round(skey, ctxt);
- return ptxt;
- # Main Program
- #____________________________________________________________
- print("Experimental HMAC Encryption");
- print("Chris M. Thomasson 9/24/2018");
- print("==============================================================\n\n");
- # Alice and Bob_II's secret key
- skey = ct_secret_key(
- "This is the Secret HMAC Key! BTW, this should be longer...",
- "Another Password Used With the Previous Byte...",
- 103,
- 32,
- 5
- );
- print("Secret Key:\n"
- "______________________________\n"
- "%s\n\n" % (skey)
- );
- # Our plaintext
- ptxt_bytes = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
- ptxt = ct_plaintext(ptxt_bytes);
- print("Original Plaintext:\n"
- "______________________________\n"
- "%s\n\n" % (ptxt)
- );
- # Encrypt the plaintext
- crand = ct_rand_bytes(32);
- ctxt = ct_encrypt(skey, ptxt);
- print("Ciphertext:\n"
- "______________________________\n"
- "%s\n\n" % (ctxt)
- );
- # Decrypt the ciphertext
- ptxt_decrypt = ct_decrypt(skey, ctxt);
- print("Decrypted Plaintext:\n"
- "______________________________\n"
- "%s\n\n" % (ptxt_decrypt)
- );
- # Check the data...
- if (ptxt_bytes != ptxt_decrypt.bytes):
- print("DATA CORRUPTED!!!!!");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement