Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Chris M. Thomasson Copyright 2018 (c)
- # Experimental HMAC Cipher
- # HACKED version for educational purposes ONLY!!!!
- # hack version (0.0.0)
- #____________________________________________________________
- # Our external libs
- #____________________________________________________________
- import random;
- import hashlib;
- 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 need should ideally be from a truly random, non-repeatable
- # source. TRNG!
- # The hacked version of this creates the same bytes on every call.
- # This is only for this demonstration. Period! Damn it.
- def ct_rand_bytes_hacked(n):
- rb = "";
- for i in range(n):
- #rb = rb + chr(random.randint(0, 255));
- rb = rb + chr(i);
- return rb;
- # The Secret Key
- # Contains all the parts of the secret key
- #____________________________________________________________
- class ct_secret_key:
- def __init__(self, hmac_key, hash_algo, rand_n):
- self.hmac_key = hmac_key;
- self.hash_algo = hash_algo;
- self.rand_n = rand_n;
- def __repr__(self):
- return "hmac_key:%s\nhash_algo:%s\nrand_n:%s" % (ct_bytes_to_hex(self.hmac_key, 0), self.hash_algo, self.rand_n);
- def __str__(self): return self.__repr__();
- # The Ciphertext or Plaintext
- # It holds the bytes of a ciphertext or a plaintext
- #____________________________________________________________
- class ct_bin:
- def __init__(self, ctxt):
- self.bytes = ctxt;
- def __repr__(self):
- return "%s" % (ct_bytes_to_hex(self.bytes, 0));
- def __str__(self): return self.__repr__();
- # The Crypt Round Function
- #____________________________________________________________
- def ct_crypt_round(SK, P, M):
- H = hmac.new(SK.hmac_key.encode(), None, SK.hash_algo);
- H.update(SK.hmac_key[::-1].encode());
- C = "";
- I_P = 0;
- I_P_N = len(P.bytes);
- digest_n = 0;
- while (I_P < I_P_N):
- D = H.digest();
- I_D = 0;
- I_D_N = len(D);
- print("digest[%s]:(%s)\n" % (digest_n, H.hexdigest()));
- digest_n = digest_n + 1;
- while (I_P < I_P_N and I_D < I_D_N):
- C_I_P = ord(P.bytes[I_P]) ^ D[I_D];
- C = C + chr(C_I_P);
- if (M == False):
- H.update(P.bytes[I_P].encode());
- H.update(chr(C_I_P).encode());
- else:
- H.update(chr(C_I_P).encode());
- H.update(P.bytes[I_P].encode());
- I_P = I_P + 1;
- I_D = I_D + 1;
- return ct_bin(C);
- # The Crypt Function
- #____________________________________________________________
- def ct_crypt(SK, P, M):
- if (M == False):
- R = ct_rand_bytes_hacked(SK.rand_n);
- print("prepended random bytes\n----------------------------");
- print("%s" % ct_bytes_to_hex(R, 0));
- print("----------------------------\n");
- P.bytes = R + P.bytes;
- print("plaintext after random bytes are prepended\n----------------------------");
- print("%s" % P);
- print("----------------------------\n");
- print("round 0\n----------------------------");
- C = ct_crypt_round(SK, P, M);
- print("----------------------------\n");
- C_1 = ct_bin(C.bytes[::-1]);
- print("round 1\n----------------------------");
- C = ct_crypt_round(SK, C_1, M);
- print("----------------------------\n");
- if (M == True):
- print("\nciphertext BEFORE random bytes are removed\n----------------------------");
- print("%s" % C);
- print("----------------------------\n");
- size = len(C.bytes) - SK.rand_n;
- C.bytes = C.bytes[SK.rand_n : SK.rand_n + size];
- return C;
- # The Main Program
- #____________________________________________________________
- # Alice and Bob's Secret Key
- #____________________
- SK = ct_secret_key(
- "This is the HMAC Key. It should be a crypto secure key! Damn it.",
- hashlib.sha384, # The hash function. It should be a crypto secure hash.
- 48 # The number of bytes. The should be generated by a TRNG
- );
- print("%s" % (SK));
- # Alice's Plaintext
- #____________________
- Original_Plaintext = "ABCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCDE";
- A_P = ct_bin(Original_Plaintext);
- print(
- "\n\nAlice's Plaintext Bytes:"
- "\n____________________\n%s\n" % (A_P)
- );
- # Encrypt
- #____________________
- print("\n\nENCRYPT\n________________________________________\n");
- C = ct_crypt(SK, A_P, False);
- print("________________________________________\n\n");
- print(
- "\n\nCiphertext Bytes:"
- "\n____________________\n%s\n" % (C)
- );
- # Decrypt
- #____________________
- print("\n\nDECRYPT\n________________________________________\n");
- B_P = ct_crypt(SK, C, True);
- print("________________________________________\n\n");
- print(
- "\n\nBob's Plaintext Bytes:"
- "\n____________________\n%s\n" % (B_P)
- );
- if (B_P.bytes != Original_Plaintext):
- print("DATA CORRUPTED!");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement